How to create Custom wait library for handling synchronization in Selenium Webdriver

0 votes
How to create Custom wait library for handling synchronization in Selenium Webdriver?
Jul 22, 2019 in Selenium by Sushmita
531 views

1 answer to this question.

0 votes

Hi Sushmita, to solve Synchronization issue in Selenium Webdriver, you can create 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");
}
}
answered Jul 22, 2019 by Anvi
• 14,110 points

Related Questions In Selenium

0 votes
2 answers
0 votes
1 answer

How to wait for the webpage to load in selenium using java

Firstly, when you launch a web application, ...READ MORE

answered Aug 28, 2018 in Selenium by bug_seeker
• 15,550 points
733 views
0 votes
1 answer

How to do session handling in Selenium Webdriver?

Hey Lalita, you can perform following steps ...READ MORE

answered Jun 20, 2019 in Selenium by Subhash
6,422 views
0 votes
1 answer

How to apply wait for new window using Selenium WebDriver?

As sometimes during web automation, when you ...READ MORE

answered Jul 9, 2019 in Selenium by Vaishali Walia
3,635 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
4,349 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,270 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
3,148 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
3,672 views
0 votes
1 answer

How to add Junit library in Eclipse to use with Selenium Webdriver?

Hi Jacky, to add JUnit library in ...READ MORE

answered Jun 11, 2019 in Selenium by Anvi
• 14,110 points
214 views
0 votes
1 answer