如何在Selenium 2中实现wait_for_page_to_load ?

时间:2022-03-08 21:13:47

I am new to automated web testing and I am currently migrating from an old Selenium RC implementation to Selenium 2 in Ruby. Is there a way to halt the execution of commands until the page gets loaded, similar to "wait_for_page_to_load" in Selenium RC?

我是自动化web测试的新手,目前正在从旧的Selenium RC实现迁移到Ruby中的Selenium 2。是否有一种方法可以停止执行命令直到页面加载,类似于Selenium RC中的“wait_for_page_to_load”?

3 个解决方案

#1


2  

Try using Javascript to inform you!

尝试使用Javascript来通知您!

I created a couple methods that checks via our javascript libraries and waits to see if the page has finished loading the DOM and that all ajax requests are complete. Here's a sample snippet. The javascript you will need to use is just going to depend on your library.

我创建了两个方法,它们通过javascript库进行检查,并等待查看页面是否已加载DOM,以及所有ajax请求是否都已完成。下面是一个示例代码片段。您需要使用的javascript将取决于您的库。

Selenium::WebDriver::Wait.new(:timeout => 30).until { @driver.execute_script("[use javascript to return true once loaded, false if not]"}

I then wrapped these methods in a clickAndWait method that clicks the element and calls the waitForDomLoad and waitForAjaxComplete. Just for good measure, the very next command after a clickAndWait is usually a waitForVisble element command to ensure that we are on the right page.

然后我将这些方法封装在clickAndWait方法中,该方法单击元素并调用waitForDomLoad和waitForAjaxComplete。为了更好地衡量,clickAndWait之后的下一个命令通常是waitForVisble元素命令,以确保我们在正确的页面上。

# Click element and wait for page elements, ajax to complete, and then run whatever else
def clickElementAndWait(type, selector)
    @url = @driver.current_url
    clickElement(type, selector)
    # If the page changed to a different URL, wait for DOM to complete loading
    if @driver.current_url != @url
      waitForDomLoad
    end
    waitForAjaxComplete
    if block_given?
      yield
    end
end

#2


4  

I fixed a lot of issues I was having in that department adding this line after starting my driver

我解决了很多我在那个部门遇到的问题,在我启动我的驱动程序后增加了这条线

driver.manage.timeouts.implicit_wait = 20

This basically makes every failed driver call you make retry for maximum 20 seconds before throwing an exception, which is usually enough time for your AJAX to finish.

这基本上使每一个失败的驱动程序调用您在抛出异常之前都要重试20秒,这通常足够让您的AJAX完成。

#3


1  

If you are using capybara, whenever you are testing for page.should have_content("foo"), capybara will not fail instantly if the page doesn't have the content (yet) but will wait for a while to see if an ajax call will change that.

如果您正在使用capybara,那么当您在测试page时。如果页面没有内容(但是),capybara不会立即失败,但是会等待一段时间,看看ajax调用是否会改变它。

So basically: after you click, you want to check right away for have_content("some content that is a consequence of that click").

因此,基本上:单击之后,您希望立即检查have_content(“一些内容是单击的结果”)。

#1


2  

Try using Javascript to inform you!

尝试使用Javascript来通知您!

I created a couple methods that checks via our javascript libraries and waits to see if the page has finished loading the DOM and that all ajax requests are complete. Here's a sample snippet. The javascript you will need to use is just going to depend on your library.

我创建了两个方法,它们通过javascript库进行检查,并等待查看页面是否已加载DOM,以及所有ajax请求是否都已完成。下面是一个示例代码片段。您需要使用的javascript将取决于您的库。

Selenium::WebDriver::Wait.new(:timeout => 30).until { @driver.execute_script("[use javascript to return true once loaded, false if not]"}

I then wrapped these methods in a clickAndWait method that clicks the element and calls the waitForDomLoad and waitForAjaxComplete. Just for good measure, the very next command after a clickAndWait is usually a waitForVisble element command to ensure that we are on the right page.

然后我将这些方法封装在clickAndWait方法中,该方法单击元素并调用waitForDomLoad和waitForAjaxComplete。为了更好地衡量,clickAndWait之后的下一个命令通常是waitForVisble元素命令,以确保我们在正确的页面上。

# Click element and wait for page elements, ajax to complete, and then run whatever else
def clickElementAndWait(type, selector)
    @url = @driver.current_url
    clickElement(type, selector)
    # If the page changed to a different URL, wait for DOM to complete loading
    if @driver.current_url != @url
      waitForDomLoad
    end
    waitForAjaxComplete
    if block_given?
      yield
    end
end

#2


4  

I fixed a lot of issues I was having in that department adding this line after starting my driver

我解决了很多我在那个部门遇到的问题,在我启动我的驱动程序后增加了这条线

driver.manage.timeouts.implicit_wait = 20

This basically makes every failed driver call you make retry for maximum 20 seconds before throwing an exception, which is usually enough time for your AJAX to finish.

这基本上使每一个失败的驱动程序调用您在抛出异常之前都要重试20秒,这通常足够让您的AJAX完成。

#3


1  

If you are using capybara, whenever you are testing for page.should have_content("foo"), capybara will not fail instantly if the page doesn't have the content (yet) but will wait for a while to see if an ajax call will change that.

如果您正在使用capybara,那么当您在测试page时。如果页面没有内容(但是),capybara不会立即失败,但是会等待一段时间,看看ajax调用是否会改变它。

So basically: after you click, you want to check right away for have_content("some content that is a consequence of that click").

因此,基本上:单击之后,您希望立即检查have_content(“一些内容是单击的结果”)。