testngjavaselenium。Thread.sleep()的替代方案是什么
根据我看过的许多文章,使用Thread.sleep()似乎是不受欢迎的。我经常在我的测试类中使用它,在测试类中需要等待加载某些内容。我曾尝试使用此方法来告诉我加载何时完成,但这没有帮助。这不是一种可靠的方法testngjavaselenium。Thread.sleep()的替代方案是什么,java,selenium-webdriver,Java,Selenium Webdriver,根据我看过的许多文章,使用Thread.sleep()似乎是不受欢迎的。我经常在我的测试类中使用它,在测试类中需要等待加载某些内容。我曾尝试使用此方法来告诉我加载何时完成,但这没有帮助。这不是一种可靠的方法 try { return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded") || ((JavascriptExe
try {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("loaded")
|| ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
} catch (Exception e) {
System.err.println(thisClass + " Exception caught: " + e.getMessage());
}
我可以使用什么替代方法
这个解决方案奏效了:
String className = "gwt-Button form-control btn back-button ripple-container";
String textToFind = "back-button-icon";
String htmlElement = "button";
boolean isReady = Common.FluentWait(driver, 60, className, textToFind, htmlElement);
public static boolean FluentWait(WebDriver driver, int timeOut, String className, String textToFind,
String htmlElement) {
// Waiting timeOut seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Common.myPrint(thisClass + " FluentWait. ");
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeOut, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
Common.myPrint(thisClass + " run returnWebElement. ");
return returnWebElement(className, textToFind, htmlElement, driver);
}
});
if (foo != null) {
return true;
}
return false;
String className = "gwt-Button form-control btn back-button ripple-container";
String textToFind = "back-button-icon";
String htmlElement = "button";
boolean isReady = Common.FluentWait(driver, 60, className, textToFind, htmlElement);
public static boolean FluentWait(WebDriver driver, int timeOut, String className, String textToFind,
String htmlElement) {
// Waiting timeOut seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Common.myPrint(thisClass + " FluentWait. ");
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeOut, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
Common.myPrint(thisClass + " run returnWebElement. ");
return returnWebElement(className, textToFind, htmlElement, driver);
}
});
if (foo != null) {
return true;
}
return false;
public静态布尔FluentWait(WebDriver驱动程序、int超时、字符串className、字符串textToFind、,
字符串(HtmleElement){
//等待元素出现在页面上的超时秒数,正在检查
//每5秒检查一次。
Common.myPrint(thisClass+FluentWait.);
Wait Wait=new FluentWait(驱动程序)。withTimeout(超时,TimeUnit.SECONDS)
.pollingEvery(5,TimeUnit.SECONDS)。忽略(NoSuchElementException.class);
WebElement foo=wait.until(新函数(){
公共WebElement应用(WebDriver){
Common.myPrint(thisClass+“运行returnWebElement”);
返回returnWebElement(类名、textToFind、HtmleElement、驱动程序);
}
});
如果(foo!=null){
返回true;
}
返回false;
}public static WebElement returnWebElement(String className, String textToFind, String htmlElement,
WebDriver driver) {
List<WebElement> elements = Common.findElementsUsingHtmlXpathClass(driver, htmlElement, className);
Common.myPrint(thisClass + " elements count: " + elements.size());
String text = "";
for (WebElement element : elements) {
// select an element
if (element.isDisplayed()) {
text = element.getAttribute("innerHTML");
if (text != "") {
text = text.trim();
if (text.contains(textToFind)) {
Common.myPrint(thisClass + " innerHTML: " + text);
Common.myPrint(thisClass + " returning element found. ");
return element;
}
}
}
}
Common.myPrint(thisClass + " element not found. ");
return null;
}
publicstaticwebelement返回WebElement(stringclassname、stringtexttofind、stringhtmlelement、,
网络驱动程序(驱动程序){
列表元素=Common.findElementsUsingHtmlXpathClass(驱动程序、htmlElement、类名);
Common.myPrint(thisClass+“元素计数:”+elements.size());
字符串文本=”;
for(WebElement:elements){
//选择一个元素
if(element.isDisplayed()){
text=element.getAttribute(“innerHTML”);
如果(文本!=“”){
text=text.trim();
if(text.contains(textToFind)){
Common.myPrint(thisClass+“innerHTML:+文本);
Common.myPrint(thisClass+“找到返回元素”);
返回元素;
}
}
}
}
Common.myPrint(thisClass+“未找到元素”);
返回null;
}
您可以使用FluentWait
每个FluentWait实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在搜索页面上的元素时忽略NoTouchElementExceptions
另一个等待也是您的代码只会尝试等待页面加载,而不是等待特定元素等加载。这对于静态HTML页面很好,但是一旦开始向页面添加动态部分(使用AJAX等),它就无法实现您想要的功能 您可以使用
WebDriverWait
。有关更多信息,请参阅
举个简单的例子
// create an instance that can be reused
WebDriverWait wait = new WebDriverWait(driver, 10);
// wait for an element to be clickable and store the return
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("someId"));
// click the returned button
button.click();
ExpectedConditions
提供了一系列标准条件。有关更多信息,请参阅。我找到了另一种方法,尽管它与Thread.sleep()非常相似。
。我一直在寻找另一种等待,因为Selenium的隐式等待和显式等待对我来说不够长
我使用的替代方案是:
TimeUnit.SECONDS.sleep(整数时间单位)
这提供了与
Thread.sleep()相同的功能。我希望这会有所帮助。很有趣,但是它在哪里定义了@Roushan的最大等待时间?。通过超时(30秒)
您可以在这里指定,这是evry在5秒后检查的轮询间隔,在99%的情况下确实不需要使用FluentWait
WebDriverWait
工作正常FluentWait
是指当您需要自定义等待的所有方面时,大多数人只需要等待X秒,等待某个条件为真,例如等待某个元素可单击、可见等。正如@Stevestuple所提到的sleep()
方法的实现是一种代码气味。根据,这基本上与Thread.sleep()
相同。如果Selenium没有为您等待足够长的时间,我确信您的问题是您的等待条件,而不是Selenium。正如OK所认为的那样,这只是另一个实现Thread.sleep()
。谢谢你的澄清。