Selenium with C# 35 - Synchronization using Thread.Sleep and implicit wait explained in detail

preview_player
Показать описание
Synchronization in selenium using Thread.Sleep and implicit wait explained in detail.

Synchronization
Thread Sleep method
Implicit Wait

What is Synchronization in selenium?
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.

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.

What is Implicit Wait in selenium?
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().ImplicitWait = TimeSpan.FromSeconds(20);

What is Synchronization?
What is Thread.Sleep wait?
What is implicit wait?
What is the difference between Thread.Sleep And ImplicitWait?

Code :
[TestClass]
public class Synchronization
{
[TestMethod]
public void SynchronizationUsingSleep()
{
IWebDriver driver = new FirefoxDriver();
driver.FindElement(By.PartialLinkText("This is")).Click();
Thread.Sleep(12000);
string result = driver.FindElement(By.ClassName("ContactUs")).Text;
Console.WriteLine(result);
Assert.IsTrue(result.Contains("Python"));

Thread.Sleep(1000);
driver.Quit();

}
[TestMethod]
public void SynchronizationUsingImplicitWait()
{
IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
driver.FindElement(By.PartialLinkText("This is")).Click();
//Thread.Sleep(12000);
string result = driver.FindElement(By.ClassName("ContactUs")).Text;
Assert.IsTrue(result.Contains("Python"));

driver.Quit();

}
Рекомендации по теме
Комментарии
Автор

Implicit wait is it used for page loading purpose only or can we use it to wait for an element to be displayed too and can we have multiple implicit waits.

beenashaheer