[英]Selenium webdriver explicit wait
我正在编写一些有关使用硒铬驱动程序的自动化测试。 我试图编写一个可重用的方法,该方法将显式等待元素出现,然后在其他类中调用此方法。 似乎很简单,但是它没有按照我想要的去做。 这是我的方法。
public String waitForElement(String item) {
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(
ExpectedConditions.elementToBeClickable(By.id(item)));
return item;
}
然后,我调用该方法并将其传递给这样的参数:
waitForElement("new-message-button");
这似乎没有用,有人可以提供一些见解吗?
您可以使用“显式等待”或“ Fluent等待”
显式等待的示例-
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));
流利等待的示例-
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(20, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("about_me"));
}
});
查看此教程以获取更多详细信息。
public static void clickOn(WebDriver driver, WebElement locator, int timeout)
{
new WebDriverWait(driver,timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.elementToBeClickable(locator));
locator.click();
}
在main方法中调用上述方法,我们将获得显式等待功能。
我使用Selenium构建了一个程序包,而等待是我遇到的最大问题之一。 最后,如上所述的方法将不起作用。 我不得不求助于一个简单的隐式等待任何动态元素,如下所述
隐式等待是告诉WebDriver在尝试查找不立即可用的一个或多个元素时,在一定时间内轮询DOM。 默认设置为0。设置后,将在WebDriver对象实例的生存期内设置隐式等待。
码:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
希望能有所帮助。
我以以下方式编写了明确的硒测试等待:
我声明将通过添加ID的@FindBy注释找到我的WebElement,如下所示:
@FindBy(how = How.ID, using = "home")
private WebElement home;
然后,我的等待元素加载的方法编写如下:
public WebElement isElementLoaded(WebElement elementToBeLoaded) {
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.visibilityOf(elementToBeLoaded));
return element;
}
这样,无论使用哪种@FindBy方法,我都可以使用查找依据注释我正在等待的任何元素。
您的问题是您将String传递给了方法参数:
public String waitForElement(String item){
您必须传递您的WebElement,例如:
public boolean visibilityOfElementWait(WebElement webElement) {
if (webElement != null) {
try {
WebDriverWait wait = new WebDriverWait(Driver.getCurrentDriver(), 20);
wait.until(ExpectedConditions.visibilityOf(wrappedElement));
highlightElement(webElement);
return true;
} catch (Exception e) {
return false;
}
} else
Logger.logError("PageElement " + webElement.getText() + " not exist");
return false;
}
public void highlightElement(WebElement element) {
if (!Config.getProperty(Config.BROWSER).equalsIgnoreCase("ANDROIDHYBRID")) {
String bg = element.getCssValue("backgroundColor");
for (int i = 0; i < 4; i++) {
Driver.getDefault()
.executeScript("arguments[0].style.backgroundColor = 'red'", element);
Driver.getDefault()
.executeScript("arguments[0].style.backgroundColor = '" + bg + "'", element);
}
// String highlightElementScript = "arguments[0].style.backgroundColor = 'red';";
// Driver.getDefault().executeScript(highlightElementScript, element);
}
}
我们可以自己开发隐式等待。
使用此代码; 它也应与隐式等待相同。
//=== Start of Implicit Wait Statement ===
public void implicit_Wait_ID(String str) throws Exception{
for(int i=0;i<60;i++){
try{
driver.findElement(By.id(str)).isDisplayed();
break;
}catch(Exception e){Thread.sleep(2000);
}
}
}
//=== End of Implicit Wait Statement ===
通过传递ID值来使用此方法:
public void loginGmail() throws Exception
{
driver.findElement(By.id("Email")).sendKeys("Mail ID");
driver.findElement(By.id("next")).click();
implicit_Wait_ID("Passwd");
driver.findElement(By.id("Passwd")).sendKeys("Pwd value");
driver.findElement(By.id("signIn")).click();
}
如果它是Xpath,LinkText,则只需为所有定位器类型创建上述方法之一,然后在脚本中重复使用n
次。
只要使用这种方法,我希望它会完美地工作。
public void waitForElement(String item) {
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("item")));
}
然后调用方法:
waitForElement("new-message-button");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.