zoukankan      html  css  js  c++  java
  • selenium1.0和selenium2.0页面等待处理详解

    一、selenium1.0页面等待

    1、……AndWait

          经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令:

    click命令:点击操作后, 直接进入下一个动作, 不做等待;

    clickAndWait 命令,则是在click之后,自动执行一次waitForPageToLoad,等待直到当前窗口载入一个新页面, 然而,这种等待只适合那些会引起页面刷新的,如果页面不是在当前窗口载入(比如是在一个弹出窗口,或者在一个iframe里载入),那么用AndWait是无法做到正确等待的,它会一直等待当前窗口的页面载入直到超时,这种情况造成白白等待+timeout警告,另外常见的Ajax效果的界面, 使用……AndWait来等待也不起效

    还有typeAndWait  selectAndWait等等

    2、waitFor……

    比较好的等待方式是使用waitFor……来等待需要的元素出现,例如,waitForElementPresent, 默认等待30秒, 在30秒内,如果元素已经出现, 马上响应,否则报超时,可以通过IDE里options->options……中General tab来设置默认的timeout时间。

    这里摘录了下官网文档中对 “不做等待”,AndWait, waitFor三种方式的阐述:

    //1)The difference between a command and its AndWait alternative is that the regular command (e.g. click) will do the action and continue with the following command as fast as it can, while the AndWait alternative (e.g. clickAndWait) tells Selenium to wait for the page to load after the action has been done.
    //2)The AndWait alternative is always used when the action causes the browser to navigate to another page or reload the present one.
    //3)In AJAX driven web applications, data is retrieved from server without refreshing the page. Using andWait commands will not work as the page is not actually refreshed,,,The best approach would be to wait for the needed element in a dynamic period and then continue the execution as soon as the element is found.as waitForElementPresent orwaitForVisible


     

    二、selenium2.0等待机制

    1、显性等待

    第一种最简单的方法是: Thread.sleep(1000)

    这种设定了固定时间,若时间设置短了, 内容来不及更新, 设置长了延长了脚本运行时间,所以不推荐

    第二种方式:采用 WebDriverWait类+ExpectedConditions接口,具体用法例子:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement e1 = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
           
    WebElement e2=wait.until(new ExpectedCondition<WebElement>() {
         public WebElement apply(WebDriver d){
             return d.findElement(By.id("id locator"));
         }            
    });

    说明:这种方式类似seleniu1.0中的waitFor……, 上面代码表示默认等待1-10s, 10内,若ExpectedConditions找到元素立刻返回,超过是10s报超时。

    2、隐性等待

    WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    说明:
    执行driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);后

    当要查找元素,而这个元素没有马上出现时,会告诉WebDriver查询Dom一定时间(设置了10s)。默认值是0, 设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用

    三、附录

    这里列出常用的waitFor命令:

    waitForElementPresent
    locator
     
    等待直到某个元素在页面中存在
    waitForElementNotPresent
    locator
     
    等待直到某个元素在页面中不存在
     
     
    waitForTextPresent
    text
     
    等待直到某个文本在页面中存在
    waitForTextNotPresent
    text
     
    等待直到某个文本在页面中不存在
     
     
    waitForText
    locator
    text
    等待直到locator指定元素内的文本符合text
    waitForNotText
    locator
    text
    等待直到locator指定元素内的文本不符合text
     
     
    waitForVisible
    locator
     
    等待直到locator指定元素在页面中可见
    waitForNotVisible
    locator
     
    等待直到locator指定元素在页面中不可见
     
     
    waitForTitle
    title
     
    等待直到页面标题符合所指定的title
    waitForNotTitle
    title
     
    等待直到页面标题不符合所指定的title
     
     
    waitForLocation
    url
     
    等待直到页面的地址符合所指定的url
    waitForNotLocation
    url
     
    等待直到页面的地址不符合所指定的url
     
     
    waitForValue
    locator
    value
    等待直到locator指定的元素的value值符合指定的值
    waitForNotValue
    locator
    value
    等待直到locator指定的元素的value值不符合指定的值
     
     
    waitForAttribute
    value
    等待直到locator指定的元素的attr属性值符合指定的值
    waitForNotAttribute
    value
    等待直到locator指定的元素的attr属性值不符合指定的值
     
     
     
    waitForChecked
    locator
     
    等待直到locator指定的radio或checkbox成为选中状态
    waitForNotChecked
    locator
     
    等待直到locator指定的radio或checkbox成为非选中状态
     
     
     
    waitForEditable
    locator
     
    等待直到locator指定的input或textarea元素可编辑
    waitForNotEditable
    locator
     
    等待直到locator指定的input或textarea元素不可编辑
     
     
    waitForXpathCount
    xpath
    count
    等待直到页面符合xpath的数量为count
    waitForNotXpathCount
    xpath
    count
    等待直到页面符合xpath的数量不为count
     
     
    waitForEval
    script
    pattern
    等待直到script的执行结果符合pattern
    waitForNotEval
    script
    pattern
    等待直到script的执行结果不符合pattern

      

  • 相关阅读:
    最近纠结致死的一个java报错java.net.SocketException: Connection reset 终于得到解决
    分享:fastjson 1.1.28 发布,Java 的 JSON 库
    Google Code Jam
    firefox 17.0 编包记(fedora 17)
    中文分词文献列表 Bibliography of Chinese Word Segmentation
    ThriftUsageC++ Thrift Wiki
    try thrift
    SENNA
    Ghostscript: Ghostscript Downloads
    DHCP简介
  • 原文地址:https://www.cnblogs.com/jenniferhuang/p/3435246.html
Copyright © 2011-2022 走看看