Utility to automatically manage all web element waits and enables to write wait-free selenium tests.

Overview

selenium-auto-wait

selenium-auto-wait automatically manages all weblement waits and makes you to write wait free selenium tests.

Features

  1. Waits till element found when using findElement method. Unlike Webdriver's implicit wait method, you can control this behaviour using annotations.
  2. Waits for the element to become intractable before performing any action on it.
  3. If you using pageobject model and wants to ignore auto wait for certain methods, you can use @IgnoreWait annotation.
  4. You can use @WaitProperties annotation to control the behaviour of auto wait for a specific method in the page object method.

Installation

Maven

<dependency>
    <groupId>io.github.sudharsan-selvaraj</groupId>
    <artifactId>selenium-auto-wait</artifactId>
    <version>1.0.2</version>
</dependency> 

Gradle

implementation group: 'io.github.sudharsan-selvaraj', name: 'selenium-auto-wait', version: '1.0.2'

Also while downloading selenium, make sure to exclude net.bytebuddy:byte-buddy library by using

Maven

<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>3.141.59</version>
   <exclusions>
      <exclusion>
         <groupId>net.bytebuddy</groupId>
         <artifactId>byte-buddy</artifactId>
      </exclusion>
   </exclusions>
</dependency>

Gradle

implementation (group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59') {
   exclude group: 'net.bytebuddy', module: 'byte-buddy'
 }

Quickstart

Initialize the wait plugin using

SeleniumWaitOptions options = SeleniumWaitOptions.builder()
                .parseAnnotations(true)
                .defaultWaitTime(Duration.ofSeconds(30))
                .build();
SeleniumWaitPlugin<ChromeDriver> seleniumWaitPlugin = new SeleniumWaitPlugin<ChromeDriver>(new ChromeDriver(), options);
WebDriver driver =  seleniumWaitPlugin.getDriver();

That's it. Now the driver object can be used in the test.

Available options

  • defaultWaitTime (Duration) - Used as a timeout while waiting for element.
  • excludedMethods (List) - List of method names that will be ignored in auto wait.
  • parseAnnotations (Boolean) - If true, the plugin will look for @IgnoreWait or @WaitProperties annotation and manages wait based on it. Default value is false
  • packageToBeParsed (String) - if parseAnnotations is true, the plugin will parse all the methods from the stacktrace looking for annotations. If you want to search the annotations only on a specific package then you can mention it here.

Annotation Example:

public class WaitTest {
    
    public WebDriver getDriver() {
        SeleniumWaitOptions options = SeleniumWaitOptions.builder()
                .parseAnnotations(true)
                .defaultWaitTime(Duration.ofSeconds(30))
                .build();
      
        SeleniumWaitPlugin seleniumWaitPlugin = new SeleniumWaitPlugin(new ChromeDriver(), options);
        return seleniumWaitPlugin.getDriver();
    }
    
    @Test
    public void test() {
        WebDriver driver = getDriver();
        searchAmazon(driver);
        searchAmazonWithoutWait(driver);
        searchAmazonWithCustomWait(driver);
    }

    public void searchAmazon(WebDriver driver) {
        driver.get("https://www.amazon.in");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("oneplus 7");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys(Keys.ENTER);
        driver.findElement(By.partialLinkText("OnePlus 7 Pro")).click();
        driver.switchTo().window(driver.getWindowHandles().toArray(new String[]{})[1]);
        driver.findElement(By.id("add-to-cart-button")).click();
        driver.findElement(By.id("attach-view-cart-button-form")).click();
    }
    
    @IgnoreWait // will not automatically wait for any element interaction
    public void searchAmazonWithoutWait(WebDriver driver) {
        driver.get("https://www.amazon.in");
        new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("twotabsearchtextbox")));
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("oneplus 7", Keys.ENTER);
        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.presenceOfElementLocated(By.partialLinkText("OnePlus 7 Pro")));
        driver.findElement(By.partialLinkText("OnePlus 7 Pro")).click();
        driver.switchTo().window(driver.getWindowHandles().toArray(new String[]{})[1]);
        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.presenceOfElementLocated(By.id("add-to-cart-button")));
        driver.findElement(By.id("add-to-cart-button")).click();
        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.elementToBeClickable(By.id("attach-view-cart-button-form")));
        driver.findElement(By.id("attach-view-cart-button-form")).click();
    }

    @WaitProperties(
            timeout = 10, //custom wait time in seconds
            exclude = {"sendKeys"} // will not automatically wait for sendKeys method
    )
    public void searchAmazonWithCustomWait(WebDriver driver) {
        driver.get("https://www.amazon.in");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("oneplus 7");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys(Keys.ENTER);
        driver.findElement(By.partialLinkText("OnePlus 7 Pro")).click();
        driver.switchTo().window(driver.getWindowHandles().toArray(new String[]{})[1]);
        driver.findElement(By.id("add-to-cart-button")).click();
        driver.findElement(By.id("attach-view-cart-button-form")).click();
    }
}
You might also like...

Never debug a test again: Detailed failure reports and hassle free assertions for Java tests - Power Asserts for Java

Never debug a test again: Detailed failure reports and hassle free assertions for Java tests - Power Asserts for Java

Scott Test Reporter for Maven and Gradle Get extremely detailed failure messages for your tests without assertion libraries, additional configuration

Nov 17, 2022

🔌 Simple library to manipulate HTTP requests/responses and capture network logs made by the browser using selenium tests without using any proxies

🔌 Simple library to manipulate HTTP requests/responses and capture network logs made by the browser using selenium tests without using any proxies

Simple library to manipulate HTTP requests and responses, capture the network logs made by the browser using selenium tests without using any proxies

Oct 23, 2022

🤖 Unleash the full power of test.ai into your Java Selenium tests

🤖 Unleash the full power of test.ai into your Java Selenium tests

The test.ai selenium SDK is a simple library that makes it easy to write robust cross-browser web tests backed by computer vision and artificial intelligence.

Jul 15, 2022

This repository includes selenium tests examples using cucumber-jvm framework.

Cucumber Selenium Tests This repository includes cucumber selenium tests examples using wikipedia.org. Run tests To run tests on your local machine, y

Nov 27, 2022

Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Check performance metrics by running Selenium 4 tests with JUnit on LambdaTest cloud.

Check performance metrics by running Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

A sample repo to help you capture JavaScript exception for automation test in Java-TestNG on LambdaTest. Run Selenium tests with TestNG on LambdaTest platform.

How to capture JavaScript exception for automation test in Java-TestNG on LambdaTest Environment Setup Global Dependencies Install Maven Or Install Ma

Jul 13, 2022
Comments
  • [Feature Request] : Add annotation at class level

    [Feature Request] : Add annotation at class level

    Hi,

    Since lot of UI automation engineers follow the Page object model, is it possible to add annotations at class level? This way we do not need to add annotations at each method

    opened by ssachdevatwist 0
Releases(v1.0.0)
Owner
Sudharsan Selvaraj
Senior Test Engineer | Automation enthusiast | TestNinja
Sudharsan Selvaraj
A FREE Selenium course that takes you step-by-step through building a custom Selenium Framework from scratch.

Selenium For Everyone The book and code repo for the FREE Selenium For Everyone book by Kevin Thomas. FREE Book Download Chapter 1: Getting Started Th

Kevin Thomas 5 May 10, 2022
A sample repo to help you handle basic auth for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to handle basic auth for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - htt

null 12 Jul 13, 2022
A sample repo to help you clear browser cache with Selenium 4 Java on LambdaTest cloud. Run your Java Selenium tests on LambdaTest platform.

How to clear browser cache with Selenium 4 Java on LambdaTest cloud Prerequisites Install and set environment variable for java. Windows - https://www

null 12 Jul 13, 2022
A sample repo to help you run automation test in incognito mode in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to run automation test in incognito mode in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - htt

null 12 Jul 13, 2022
A sample repo to help you handle cookies for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to handle cookies for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - https:

null 13 Jul 13, 2022
A sample repo to help you set geolocation for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to set geolocation for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Windows - https

null 12 Jul 13, 2022
A sample repo to help you capture JavaScript exception for automation test in Java-selenium on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to capture JavaScript exception for automation test in Java-selenium on LambdaTest Prerequisites Install and set environment variable for java. Wi

null 12 Jul 13, 2022
A sample repo to help you emulate network conditions in Java-selenium automation test on LambdaTest. Run your Java Selenium tests on LambdaTest platform.

How to emulate network conditions in Java-selenium automation test on LambdaTest Prerequisites Install and set environment variable for java. Windows

null 12 Jul 13, 2022
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Testcontainers Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium we

null 6.7k Jan 9, 2023
All prs are automatically merged - star the mod to become a collaborator

community-mod is now archived. thank you to everyone to contributed to this masterpiece. The Community Mod A Mod by The Community for the Community! M

null 66 Nov 25, 2022