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;
}
}