Selenium 中的等待命令是什么?
在執(zhí)行 selenium 自動(dòng)化測(cè)試時(shí),我們使用等待來(lái)使我們的測(cè)試可靠且健壯。最常見(jiàn)的是,在運(yùn)行自動(dòng)化測(cè)試時(shí),如果在加載 Webdriver 想要交互的特定元素時(shí)出現(xiàn)延遲,我們會(huì)看到“ElementNotVisibleException”。
等待和超時(shí)可幫助用戶(hù)在執(zhí)行某些操作或在應(yīng)用程序中的不同頁(yè)面之間導(dǎo)航后在頁(yè)面上加載元素時(shí)克服各種問(wèn)題。
Selenium 4 中的隱式等待
讓我們看看升級(jí)到 Selenium 4 后如何定義隱式等待。
在 Selenium 4 之前 -
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
現(xiàn)在將其視為已棄用
@Deprecated
WebDriver.Timeouts implicitlyWait(long time, TimeUnit unit);
在Selenium 4 之后 -
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
與scriptTimeout和pageLoadTimeout等其他等待相同:-
driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
Selenium 4 中的顯式等待
WebDriverWait 現(xiàn)在也期待“持續(xù)時(shí)間”而不是長(zhǎng)時(shí)間的超時(shí),以秒和毫秒為單位。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-438380.html
該方法現(xiàn)在在 selenium 中已棄用文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-438380.html
public WebDriverWait(@NotNull org.openqa.selenium.WebDriver driver, long timeoutInSeconds)
在 Selenium 4 之前 -
//Old syntax
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classlocator")));
在 Selenium 4 之后 -
//Selenium 4 syntax
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classlocator")));
Selenium 4 中的 FluentWait
在 Selenium 4 之前 -
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
在 Selenium 4 之后 -
Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
到了這里,關(guān)于Java Selenium中的等待和超時(shí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!