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.

ExpectedConditions.titleIs("Google").apply(driver);
ExpectedConditions.presenceOfElementLocated(By.name("q1")).apply(driver);
ExpectedConditions.alertIsPresent().apply(driver).accept();
ExpectedConditions.presenceOfElementLocated(By.name("q1")).apply(driver).sendKeys("Make Selenium Easy");
ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")).apply(driver);
ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")).apply(driver).findElement(By.xpath("//button[text()='Try it']")).click();
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