I want to create a Custom ExpectedCondition to test a specific condition in Selenium is it possible to achieve the same

0 votes
I want to create a Custom ExpectedCondition to test a specific condition in Selenium. is it possible to achieve the same?
Jul 4, 2019 in Selenium by Revathy
4,816 views

1 answer to this question.

0 votes

Hey @Revathy, yes it is possible to create Custom ExpectedCondition with Selenium Webdriver.  A Custom ExpectedCondition is a class that has a constructor with the parameters of the expected condition and it implements the ExpectedCondition interface and overrides the apply method. Following code sample will show you how to create a custom expectedcondition:

public class TestClass {

	WebDriver driver;	
	WebDriverWait wait;

	By searchFieldXpath = By.id("globalQuery");
	By searchButtonXpath = By.className("search_button");

	By resultLinkLocator = By.xpath("(//a[@testid='bib_link'])[1]");		
	
	String homeUrl = "https://www.edureka.co/"; 
	String homeTitle = "Vancouver Public Library - Home";

	String resultsTitle = "Search | Vancouver Public Library | BiblioCommons";
	String resultsUrl = "https://vpl.bibliocommons.com/search";

	@Before
	public void setUp() }
		driver = new FirefoxDriver();
		wait = new WebDriverWait(driver, 10);
	}
	
	@After
	public void tearDown() {
		driver.quit();
	}
	
	@Test
	public void test1() {
		driver.get(siteUrl);
				
		if (!wait.until(new PageLoaded(homeTitle, homeUrl)))
			throw new RuntimeException("home page is not displayed");
				
		WebElement searchField = wait.until(elementToBeClickable(searchFieldXpath));
		searchField.click();           
		searchField.sendKeys(keyword);
			
		WebElement searchButton = wait.until(elementToBeClickable(searchButtonXpath));
		searchButton.click();	
			
		if (!wait.until(new PageLoaded(resultsTitle, resultsUrl)))
			throw new RuntimeException("results page is not displayed");
	}
}

public class PageLoaded implements ExpectedCondition {		
	String expectedTitle;
	String expectedUrl;
	
	public PageLoaded(String expectedTitle, String expectedUrl) {
		this.expectedTitle = expectedTitle;	
		this.expectedUrl = expectedUrl;
	}
	
	@Override
	public Boolean apply(WebDriver driver) {		
		Boolean isTitleCorrect = driver.getTitle().contains(expectedTitle);
		Boolean isUrlCorrect = driver.getCurrentUrl().contains(expectedUrl);
				
		return isTitleCorrect && isUrlCorrect;
	} 
}
answered Jul 4, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

+1 vote
2 answers
0 votes
1 answer
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,500 points
13,673 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,190 points
9,142 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
0 votes
1 answer

How to create a Base Class in Selenium and use it with test scripts?

Hi Donna, you can use following code ...READ MORE

answered Jul 18, 2019 in Selenium by Anvi
• 14,150 points
12,614 views
0 votes
1 answer

How can I create a POM test case in Selenium Webdriver?

Hey Akshay, follow these steps to create ...READ MORE

answered May 13, 2019 in Selenium by Anvi
• 14,150 points
1,471 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP