Synchronization using Implicit wait and Thread.sleep method explained
Like our facebook page www.facebook.com/ankprotraining
Synchronization
Thread.sleep method
Implicit Wait
What is synchronization?
Process of matching automation tool speed with application tool speed is called synchronization.
Whenever Webdriver try to perform action on the element is not loaded in UI or application, in such cases Webdriver throws ‘No such element exception’ and stops the execution because of the synchronization issue.
What is Thread Sleep method?
Thread Wait causes the currently executing thread to sleep for the specified number of milliseconds.
It always waits for specified amount of time, even though element is available early.
It is also called hard coded wait.
Usage: Thread.sleep(2000)
Avoid using Thread.sleep method in the production code.
Implicit Wait commands :
On implementing implicit wait, if WebDriver cannot find an element in the Document Object Model(DOM), it will wait for a defined amount of time for the element to appear in the DOM
Driver will look for the element by polling every 500 milli second
Driver will wait specified amount time or element is available in the DOM
If the element is not available even after the specified time, driver will throw ElementNotFound exception
Usage: driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
Possible Interview Questions on selenium synchronization :
What is Synchronization .
What is Thread.sleep wait.
What is implicit wait
What is difference between Thread.sleep And implicitWait
Code:
@Test
public void SynchronizationUsingThreadWait() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get( "[ Ссылка ]");
driver.findElement(By.partialLinkText("This is")).click();
Thread.sleep(12000);
String result = driver.findElement(By.className("ContactUs")).getText();
Assert.assertTrue(result.contains("Python"));
driver.quit();
}
@Test
public void SynchronizationUsingImplicitWait()
{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get( "[ Ссылка ]");
driver.findElement(By.partialLinkText("This is")).click();
String result = driver.findElement(By.className("ContactUs")).getText();
Assert.assertTrue(result.contains("Python"));
driver.quit();
}
Ещё видео!