多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium回放脚本时timeout问题有效解决方案-自动化测试

更新时间:2022-04-24 09:12:11 作者:多测师 浏览:207

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

Selenium回放脚本时timeout问题有效解决方案-自动化测试

  常用的解决方案有:

  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.aichudan.com/xwzx/

联系电话

17727591462

返回顶部