Do You Know That Class ExpectedConditions is Not To Just Use With WebDriverWait?

If I ask you to verify if title of a web page is as expected or not in Selenium WebDriver – Java then probably you will use below lines of code:-

WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
String actualTitle = driver.getTitle();
Assert.assertEquals("Google", actualTitle);

Or if I ask you to verify if any web element is present or not then probably you will write below lines of codes:-

public void verifyElementIspresent(WebDriver driver, By loc) {
try {
	driver.findElement(loc);
	System.out.println("Element is not present.");
} catch (NoSuchElementException excepion) {
	System.out.println("Element is not present.");
    }
}

Or if I ask you to check if an alert is present then accept it then probably you will write below lines of code :-

public void verifyPresenceOfAlertThenAccept(WebDriver driver) {
try {
	Alert alert = driver.switchTo().alert();
	alert.accept();
} catch (NoAlertPresentException excepion) {
	System.out.println("Alert is not present.");
    }
}

But do you know we have ready made methods available to check assertions quickly? Yes those are defined in ExpectedConditions class. This class contains Canned ExpectedCondions which are generally useful within webdriver tests. They have not mentioned anywhere that you can use it with explicit wait i.e. WebDriverWait and FluentWait.

Let’s rewrite above lines of code using methods of ExpectedConditions class.

To verify title

ExpectedConditions.titleIs("Google").apply(driver);

To verify if a web element is present

ExpectedConditions.presenceOfElementLocated(By.name("q1")).apply(driver);

To verify if an alert is present and then accept it

ExpectedConditions.alertIsPresent().apply(driver).accept();

To verify if a text box is present and type in to it

ExpectedConditions.presenceOfElementLocated(By.name("q1")).apply(driver).sendKeys("Make Selenium Easy");

To switch to a frame if it is available

ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")).apply(driver);

To switch to an iframe and click on an element

ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")).apply(driver).findElement(By.xpath("//button[text()='Try it']")).click();

Complete Code :-

package SpecialConcepts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class UseExpectedConditions {


	@Test
	public void checkPresenceOfElement() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		ExpectedConditions.presenceOfElementLocated(By.name("q")).apply(driver);
		driver.quit();
	}
	
	@Test
	public void checkPresenceOfTextBoxThenType() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		ExpectedConditions.presenceOfElementLocated(By.name("q")).apply(driver).sendKeys("Make Selenium Easy");
		driver.quit();
	}

	@Test
	public void assertTitle() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.google.com");
		ExpectedConditions.titleIs("Google").apply(driver);
		driver.quit();
	}

	@Test
	public void verifyPresenceOfAlertThenAccept() {
		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
		// Switch and accept
		ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")).apply(driver).findElement(By.xpath("//button[text()='Try it']")).click();
		ExpectedConditions.alertIsPresent().apply(driver).accept();
		driver.switchTo().defaultContent();
		// Switch , get alert text then accept
		ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")).apply(driver).findElement(By.xpath("//button[text()='Try it']")).click();
		String alertText = ExpectedConditions.alertIsPresent().apply(driver).getText();
		System.out.println("Alert text is : "+alertText);
		ExpectedConditions.alertIsPresent().apply(driver).accept();
		driver.quit();
	}
}

For negative cases, you can easily handle using try catch.

You can download/clone above sample project fromĀ here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Leave a Reply

Your email address will not be published. Required fields are marked *