Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 10: What Is Package Tag And How To Use In TestNG.XML?
Next Post: Selenium Framework 3: Types Of Selenium Frameworks

Related Posts

Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern Uncategorized
image – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
TestNG Tutorials 24: Annotations In TestNG – A Quick Guide | Make Selenium Easy Uncategorized
angular-icon-1-logo-png-transparent – Make Selenium Easy Uncategorized
Make Selenium Easy – Page 46 of 47 – Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark