Synchronization or wait helps to handle dependencies while executing the script because sometime tools execution speed does not match with the application speed or some web elements response time does not match with script actions.
So to handle synchronization, selenium webdriver provides two effective ways and those are-
- Implicitly Wait
- Explicitly Wait
An implicitly Wait tell to WebDriver to hold for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Here we go with sample code to achieve Implicitly Wait-
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
namespace TestProject
{
[TestClass]
public class UnitTest1
{
IWebDriver driver;
[TestMethod]
public void TestMethod1()
{
driver = new ChromeDriver(@”D:\Download\VisualStudio_Projects”);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(10));
driver.Navigate().GoToUrl(“http://www.bing.com”);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id(“sb_form_q”)).SendKeys(“Mayank”);
driver.FindElement(By.Id(“sb_form_go”)).Click();
driver.Quit();
}
}
}
Explicit Wait is intelligent wait and for a particular Web Element. Using explicit wait we basically instruct the wait to x unit of time span before giving up and along with it we can include condition that if particular webelement appear with in x unit of time span then move to next line of code.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
namespace TestProject
{
[TestClass]
public class UnitTest1
{
IWebDriver driver;
[TestMethod]
public void TestMethod1()
{
driver = new ChromeDriver(@”D:\BackUp\MyData\VisualStudio_Projects”);
driver.Navigate().GoToUrl(“http://www.bing.com”);
driver.Manage().Window.Maximize();
driver.FindElement(By.Id(“sb_form_q”)).SendKeys(“Mayank”);
WebDriverWait load = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
load.Until(ExpectedConditions.ElementExists(By.Id(“sb_form_go”)));
driver.FindElement(By.Id(“sb_form_go”)).Click();
driver.Quit();
}
}
}
Hope above code snippets will help to resolve any synchronization issue.
12.971599
77.594563