Selenium Webdriver Turbolinks Selenium Webdriver Turbolinks selenium selenium

Selenium Webdriver Turbolinks


Waiting for the page elements to finish loading is a classic Selenium issue with no single best solution. Working with Turbolinks simply compounds this problem.

When I need to know that Turbolinks has finished, listening for the Turbolinks turbolinks:load event (formerly page:load) is the best way I can think of.

If we know the exact line(s) of code that trigger the page load, we can pass this code as a block to a wrapper method that handles all of the wait logic for us, as follows:

def wait_for_page_load(driver)  # listen for Turbolinks to start and stop  driver.execute_script('document.addEventListener("turbolinks:before-visit", function() { window.turbolinks_is_busy = true; });')  driver.execute_script('document.addEventListener("turbolinks:load", function() { window.turbolinks_is_busy = false; });')  # execute the code that causes the page load  yield  # optional short sleep, just to be safe  sleep(1)  # wait for the various 'page is loading' indicators, including our custom Turbolinks indicator  wait = Selenium::WebDriver::Wait.new(:timeout => 60)  wait.until { driver.execute_script("return document.readyState == 'complete' && jQuery.active == 0 && !window.turbolinks_is_busy;") }end

Example usage:

wait_for_page_load(@driver) { @driver.find_element(id: 'a#logout').click }

References:


According to turbolinks docs, the progress bar has class turbolinks-progress-bar you could check for the existence of this and wait for it to go?