4 ms·
The most annoying thing I found with Selenium was that it wouldn't wait for the browser to respond to click events and rerender. The approach in the blog post
by paulddraper 11y ago
The most annoying thing I found with Selenium was that it wouldn't wait for the browser to respond to click events and rerender.
The approach in the blog post (and I think elsewhere ... not sure) is to poll the DOM with a timeout.
Is there a better solution to be add with something like `executeScript`? You could run `requestAnimationFrame`, and then poll for an indicator that the click, etc. handler has indeed finished. That way if it fails, you know about it pretty soon, without the need for long timeouts. This is all just a guess though.
- jakejake 11y agoThere's some utility methods like FluentWait, but ultimately they're just convenience wrappers for polling the DOM and waiting.
- glenk 11y agoI have had some good results using the F# canopy library(http://lefthandedgoat.github.io/canopy/ http://lefthandedgoat.github.io/canopy/) for working with selenium. It handles (most) all the waits for you so you don't have to scatter a bunch of sleeps in your tests and is pretty easy to work with.
- pmontra 11y agoRuby's Capybara encapsulates Selenium and waits until elements appear on the page (the default timeout is 2 seconds). So you can write simple sequential code like click_link('bar') expect(page).to have_content('baz') and it will work even if the baz element is injected into the page by an Ajax request to the server triggered by clicking on bar. I've been using it for many years but I didn't check how they implement it. Maybe a callback from a MutationObserver? https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver https://developer.mozilla.org/en-US/docs/Web/API/MutationObs... Documentation at https://github.com/jnicklas/capybara#asynchronous-javascript-ajax-and-friends https://github.com/jnicklas/capybara#asynchronous-javascript...
- drothlis 11y agoAccording to that documentation you linked, it just polls until `default_max_wait_time` (which defaults to 2 seconds).
- crdoconnor 11y ago>Is there a better solution Yes. And it's pretty simple: WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement"))); From : http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
- drothlis 11y agoI'm not sure this satisfies your parent poster's requirement of: "if it fails, you know about it pretty soon, without the need for long timeouts."
- crdoconnor 11y agoWell, you need to have a timeout. You can make the timeout shorter when running the test on a dev environment, though, so you get quicker feedback about errors.