您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源功能測(cè)試工具 > Selenium
Selenium回放腳本時(shí)timeout問(wèn)題有效解決方案
作者:吸血鬼日記 發(fā)布時(shí)間:[ 2016/5/12 13:50:35 ] 推薦標(biāo)簽:功能測(cè)試 功能測(cè)試工具

  webdriver的適用代碼
  1. 在5秒時(shí)間內(nèi)找目標(biāo)元素,找到針對(duì)目標(biāo)元素執(zhí)行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. 等候頁(yè)面上出現(xiàn)目標(biāo)文本,對(duì)應(yīng)IDE的waitForText, waitForElementPresent
//跟據(jù)定位類型和定位字符串定位頁(yè)面元素
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();
}
}
}
  執(zhí)行完某步耗時(shí)操作之后,調(diào)用waitForText()直到出現(xiàn)目標(biāo)文本(或者超時(shí))再執(zhí)行下步動(dòng)作。

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd