Consistent Selenium Testing in Python
The best thing you can do if you are developing Selenium tests in ruby (but these lessons are probably applicable no matter what environment you&#x27;re using) is learn how they changed Capybara.  There was a big shift in Capybara around the time that &quot;should&quot; went out of the vocabulary.<p>What that means syntactically for developers is less important than the semantic change that came with this shift.  Tons of the old cheat sheets are still based on the old syntax, and many of them if you dig deep will also give the bad advice!<p>So many people try browser testing and give up because in some ways it&#x27;s hard to make the testing consistent.  Especially if you use onClick events that load another page, it&#x27;s so easy to forget that you might or might not yet have loaded that page when the next line of code runs!  That browser-side JavaScript has made your integration now multi-threaded.<p>The hardest one to figure out is when you grab a reference to something on the current page that looks like the element you want, but it&#x27;s on the previous page... it&#x27;s not what you want.<p>Then when you get around to trying to finding nodes within it, or clicking on that node.  You&#x27;ve loaded the page you want, but your reference is pointing to a node that is no longer present in the active DOM, because you&#x27;re on the new page now!<p>If you&#x27;re composing reusable steps (like in Cucumber) Always find a way to make sure you&#x27;ve already loaded the DOM of the page you think you&#x27;re on, before you get any references to nodes on the page.  Even if you have to put #some-target-page-node on the target page so you can<p>expect(page).to have_selector(&#x27;#some-target-page-node&#x27;)<p>That will prove you&#x27;ve landed on the target page, and if you find it sometimes takes a long time, or if it&#x27;s longer than the timeout, set &#x27;wait&#x27; to a longer number of seconds:<p>expect(page).to have_selector(&#x27;.some-node&#x27;, wait: 90)<p>If your page has waits that long of course, it&#x27;s most likely going to start affecting your conversion rates, so do something about that...