How to create Custom wait library for handling synchronization in Selenium WebDriver?

How to create Custom wait library for handling synchronization in Selenium WebDriver?

Hi Brett,

To solve the Synchronization issue in Selenium WebDriver, you can create a Custom Wait library in the following manner:

public class Utility
{

public static WebElement isElementPresnt(WebDriver driver,String xpath,int time)
{

WebElement element = null;
for(int i=0;i<time;i++)
{
try{
element=driver.findElement(By.xpath(xpath));
break;
}
catch(Exception e)
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e1)
{
System.out.println("Waiting for element to appear on DOM");
}
}
}
return element;
}
}
Now you can use above method in your test scripts:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

import Library.Utility;

public class SearchBus
{

@Test
public void checkBuses()
{
WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.redbus.in/");

Utility.isElementPresnt(driver,".//*[@id='txtSource']", 20).sendKeys("Bangalore");

Utility.isElementPresnt(driver,".//*[@id='txtDestination']", 20).sendKeys("Chennai");
}
}