美文网首页我爱编程
Selenium回放脚本时timeout问题有效解决方案

Selenium回放脚本时timeout问题有效解决方案

作者: 吸血鬼日记 | 来源:发表于2015-09-21 09:04 被阅读1670次

    之前在简书中曾经说过碰到回放脚本时出现timeout的问题,下面就是总结了一下网上的解决方案,请参考:

    往往我们在录制的selenium脚本回放时会遇到timeout错误,这种问题往往是由于response返回时间较长超过了selenium的等待时间。如果我们认为只要等待足够的时间,系统总是能访问一个结果的,在这样的前提下考虑网络原因或被测系统的性能问题或者第一次访问其实是正常的cache过程稍微慢一点可以理解, 那么这种情况会干扰正常的功能测试。

    常用的解决方案有:

    1. 在option窗口增加timeout时间

    2. 调节录制回放速度Fast -> Slow

    3. 使用执行等候API

    waitForPageToLoad

    用click的地方改成 clickAndWait

    waitForPopUp

    当以上的方案不太奏效时,可以考虑下面的方案

    1. 加入waitForElementPresent或waitForText, Selenium IDE 默认设置的等待时间为 30 秒,超过 30 秒目标对象仍然没有找到就会报错。加上“waitForElementPresent”之后,脚本会再等待额外的 30 秒

    2. 在某些耗时command执行之后或其他需要确保页面load完成做某个验证之前的地方再加一个

    pause(waitTime)

    Arguments:

    waitTime - the amount of time to sleep (in milliseconds)

    Wait for the specified amount of time (in milliseconds)

    调试录制的脚本,根据需要选择加入pause的位置

    3. 同一个test case执行两次,第一个test case的测试结果可以丢弃,只看第二次测试结果。这种适用于web系统第一次访问其实是建立缓存的过程。

    webdriver的适用代码

    1. 在5秒时间内找目标元素,找到就针对目标元素执行click

    (new WebDriverWait(driver, 5)).until(new ExpectedCondition() {

    public WebElement apply(WebDriver d) {

    return d.findElement(By.cssSelector("#quick-links > ul > li.nav-tabs-item.mc-ql-tab-item-bm > a"));

    }

    }).click();

    2. 等候页面上出现目标文本,对应IDE的waitForText, waitForElementPresent

    //跟据定位类型和定位字符串定位页面元素

    protected WebElement findElement(String locatorType, String locatorString) {

    if (locatorType.equals(this.CssLocator)) {

    return driver.findElement(By.cssSelector(locatorString));

    } else if (locatorType.equals(this.IdLocator)) {

    return driver.findElement(By.id(locatorString));

    } else if (locatorType.equals(this.XpathLocator)) {

    return driver.findElement(By.xpath(locatorString));

    } else if (locatorType.equals(this.NameLocator)) {

    return driver.findElement(By.name(locatorString));

    } else if (locatorType.equals(this.ClassNameLocator)) {

    return driver.findElement(By.className(locatorString));

    } else if (locatorType.equals(this.TagNameLocator)) {

    return driver.findElement(By.tagName(locatorString));

    } else if (locatorType.equals(this.LinkTextLocator)) {

    return driver.findElement(By.linkText(locatorString));

    } else {

    return driver.findElement(By.partialLinkText(locatorString));

    }

    }

    /**

    * Wait 60 seconds till text is present otherwise give timeout error, increase the waiting time if necessary

    * @param text

    * @param locatorType

    * @param locatorString

    */

    protected void waitForText(String text, String locatorType, String locatorString){

    for (int second = 0;; second++) {

    if (second >= 60) fail("timeout");

    try {

    if (text.equals(findElement(locatorType, locatorString).getText())) break;

    } catch (Exception e) {

    logger.error("waitForText exception:"+e);

    }

    try{

    Thread.sleep(1000);

    } catch (InterruptedException e) {

    logger.error("waitForText Thread.sleep exception:"+e);

    e.printStackTrace();

    }

    }

    }

    执行完某步耗时操作之后,调用waitForText()直到出现目标文本(或者超时)再执行下步动作。

    相关文章

      网友评论

        本文标题:Selenium回放脚本时timeout问题有效解决方案

        本文链接:https://www.haomeiwen.com/subject/nlxpcttx.html