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 3 in Selenium by Revathy
604 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 = "http://www.vpl.ca"; 
	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 by Anvi
• 13,960 points

Related Questions In Selenium

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

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,430 points
1,179 views
0 votes
1 answer
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,170 points
1,263 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,170 points
1,021 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 in Selenium by Anvi
• 13,960 points
387 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 12 in Selenium by Anvi
• 13,960 points
95 views