在查看了这里的所有答案和评论之后,我用一些代码对它们进行了总结,以测试同时使用隐式和显式等待。
我已经在link2中获取了代码并对其进行了重构,使其简短并提供了摘要。该代码显示了同时使用隐式和显式等待时的实际等待时间。
下面的代码转到一个网站并尝试查找有效元素和无效元素。它同时使用隐式和显式等待。在无效元素搜索的情况下,它会尝试隐式/IW 和显式/EW 等待时间的不同组合 - IW = EW、IW > EW 和 IW < EW。
首先,输出:
WHEN ELEMENT IS FOUND WITHOUT ANY DELAY :
>>> WITH implicit = 30, explicit = 20 ::::: Wait time = 0
WHEN ELEMENT IS NOT FOUND :
a. When implicit wait = explicit wait.
>>> WITH implicit = 10, explicit = 10 ::::: Wait time = 10. ***WITH EXCEPTION*** : NoSuchElementException
b. When implicit wait > explicit wait.
>>> WITH implicit = 30, explicit = 10 ::::: Wait time = 30. ***WITH EXCEPTION*** : NoSuchElementException
c. When implicit wait < explicit wait.
>>> WITH implicit = 10, explicit = 30 ::::: Wait time = 10. ***WITH EXCEPTION*** : NoSuchElementException
编码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/*
* Facing this chromedriver error after opening browser - [SEVERE]: Timed out receiving message
* from renderer: 0.100.
* */
public class TimeTest {
static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
static final String URL = "https://www.redbus.in/";
static final String TIME_ZONE_NAME = "Europe/Madrid";
static final By validLoc = By.id("src");
static final By inValidLoc = By.id("invalid locator");
static WebDriver driver;
public static void main(String[] args) {
dateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_NAME));
//>>> Open chrome browser
System.setProperty("webdriver.chrome.driver", "C:/drivers/chromedriver.exe");
TimeTest.driver= new ChromeDriver();
driver.manage().window().maximize();
//>>> Test waiting logic.
System.out.println("\n\nWHEN ELEMENT IS FOUND WITHOUT ANY DELAY : ");
//mixing of implicit wait and explicit wait will not impact on webdriver behavior.
testWait(30, 20, validLoc, "");
System.out.println("\n\nWHEN ELEMENT IS NOT FOUND : ");
//Run the method multiple times. Wait time generally = 10 seconds, but sometimes = 20 seconds.
testWait(10, 10, inValidLoc, "a. When implicit wait = explicit wait.");
//Wait time always = implicit wait. Generally ?
testWait(30, 10, inValidLoc, "b. When implicit wait > explicit wait.");
//Wait time always = implicit wait. Generally ?
testWait(10, 30, inValidLoc, "c. When implicit wait < explicit wait.");
//>>> Close the browser.
driver.quit();
}
public static void testWait(int implicitWait, int explicitWait, By locator, String comment){
// setting implicit time
driver.manage().timeouts().implicitlyWait(implicitWait, TimeUnit.SECONDS);
// Loading a URL
driver.get(URL);
// defining explicit wait
WebDriverWait wait= new WebDriverWait(driver, explicitWait);
// Locating and typing in From text box.
Date start = new Date();
String waitStats = comment + "\n>>> WITH implicit = " + implicitWait + ", explicit = " + explicitWait +
" ::::: " ;//+ "Wait start = " + dateFormat.format(start)
String exceptionMsg = "";
try {
WebElement fromTextBox = wait.until(ExpectedConditions.visibilityOf(driver.findElement(locator)));
}catch (Exception ex){
exceptionMsg = ". ***WITH EXCEPTION*** : " + ex.getClass().getSimpleName();
}
Date end = new Date();
//waitStats += ", Wait end = " + dateFormat.format(end)
waitStats += "Wait time = " +
TimeUnit.SECONDS.convert(end.getTime() - start.getTime(), TimeUnit.MILLISECONDS)
+ exceptionMsg + "\n";
System.out.println(waitStats);
}
}