如何在 Selenium 2 中实现 wait_for_page_to_load? [英] How can I implement wait_for_page_to_load in Selenium 2?

查看:27
本文介绍了如何在 Selenium 2 中实现 wait_for_page_to_load?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是自动化 Web 测试的新手,我目前正在从旧的 Selenium RC 实现迁移到 Ruby 中的 Selenium 2.有没有办法在页面加载之前停止命令的执行,类似于 Selenium RC 中的wait_for_page_to_load"?

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?

推荐答案

尝试使用 Javascript 来通知您!

Try using Javascript to inform you!

我创建了几个方法,它们通过我们的 javascript 库进行检查,并等待查看页面是否已完成加载 DOM 以及所有 ajax 请求是否已完成.这是一个示例片段.您需要使用的 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.

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

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

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.

# 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

这篇关于如何在 Selenium 2 中实现 wait_for_page_to_load?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆