问题
Explicit wait example
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement myDynamicElement= wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Implicit wait example
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"));
Let say myDynamicElement is visible at 6th second, So in both the cases driver will wait till 6th seconds and control will move to the consecutive written statement, I want to understand that how implicit and explicit wait are different from each other in this case? how do they work internally?
回答1:
Implicit Wait :
Implicit Wait
is the way to configure the WebDriver
instance to poll the HTML DOM
(DOM Tree
) for a configured amount of time when it tries to find an element or find a group/collection of elements if they are not immediately available. As per the current W3C
specification the default time is configured to 0
. We can configure the time for the Implicit Wait
any where within our script/program and can reconfigure it as per our necessity. Once we set Implicit Wait
it will be valid for the lifetime of the WebDriver
instance.
You can find a more detailed discussion here and the documentation here.
Explicit Wait :
Explicit Wait
is a code block you define, configure and implement for the WebDriver
instance to wait for a certain condition to be met before proceeding for the next line of code. WebDriverWait along with certain methods/clauses of ExpectedCondition
is one way to implement Explicit Wait
.
You can find a more detailed discussion here and the documentation here.
Getting Granular :
As per your query Let say myDynamicElement is visible at 6th second, So in both the cases driver will wait till 6th seconds and control will move to the consecutive written statement
.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Implicit Wait
would poll the HTML DOM
(DOM Tree
) for the entire 10 secs irrespective of whether the myDynamicElement
(or multiple elements matching your locator) is visible at 4th / 6th / 8th second. So, in this case, your script gets delayed by 4 secs.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement myDynamicElement= wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Explicit Wait
would wait for maximum of 10 secs for the element someid
to turn clickable (Displayed and Enabled). The WebElement
is returned back as soon as the ExpectedConditions
is met. If the ExpectedConditions
is not met for the entire duration of the configured timeline, you see the proper Exception
.
回答2:
Implicit waits are used to provide a waiting time (say 30 seconds) between each consecutive test steps across the entire test script or program. Next step only executed when the 30 Seconds (or whatever time is given is elapsed) after execution of previous step
Syntax:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time which is defined , has elapsed. Implicit wait has applied between each consecutive test steps across the entire test script or programs while Explicit waits are applied for a particular instance only.
Syntax:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable("Locator"));
来源:https://stackoverflow.com/questions/46365040/what-is-the-internal-working-difference-between-implicit-wait-and-explicit-wait