0

I have created a new class called "FluentWaitClass" and added fluent wait like below: And I want to use this wait functionality to check whether my login page has loaded in one of my below test case "TC_LoginTest_001.java". Can anyone please help letting me know how to do that ?


package com.internetBanking.utilities;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import com.google.common.base.Function;

public class FluentWaitClass {
    
        WebDriver driver;
    
    // Waiting 30 seconds for an element to be present on the page, checking
       // for its presence once every 5 seconds.
       @SuppressWarnings("deprecation")
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, TimeUnit.SECONDS)
           .pollingEvery(5, TimeUnit.SECONDS)
           .ignoring(NoSuchElementException.class);

       WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
       {
         public WebElement apply(WebDriver driver) {
           return driver.findElement(By.id("foo"));
         }
       });
}

**TC_LoginTest_001.java**

package com.internetBanking.testCases;
import org.testng.annotations.Test;
import com.internetBanking.pageObjects.LoginPage;
import com.internetBanking.utilities.*;

public class TC_LoginTest_001 extends BaseClass {
    
    @Test
    public void LoginTest() {
                
        driver.get(baseURL);
        logger.info("URL is opened");
        
        FluentWaitClass fluentwait = new FluentWaitClass();
        
        LoginPage loginPage = new LoginPage(driver);
        
        loginPage.setUsrname(username);
        logger.info("Username is entered");
        
        
        
        loginPage.setPwd(password);
        logger.info("Password is entered");
        
        loginPage.login();                                         
        logger.info("Login button is clicked");
        
    }
}```

1 Answer 1

0

This is the implementation:

// Waiting 30 seconds for an element to be present on the page, checking
       // for its presence once every 5 seconds.
       @SuppressWarnings("deprecation")
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, TimeUnit.SECONDS)
           .pollingEvery(5, TimeUnit.SECONDS)
           .ignoring(NoSuchElementException.class);

This is usage:

 WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
       {
         public WebElement apply(WebDriver driver) {
           return driver.findElement(By.id("foo"));
         }
       });

you just copy pasted the code from the example and added it in the FluentWaitClass ,

In your code just return "wait" from that class and use it in your test

But if you are going to just ignore nosuchelement then you can use normal wait , don't make it fancy with fluent wait

    WebDriverWait wait = new WebDriverWait((WebDriver) driver, Duration.ofSeconds(20));
    WebElement element = wait.until(
            ExpectedConditions.presenceOfElementLocated((By.id("someid"))));

3
  • Thanks @PDHide. I used the normal wait and it worked. Can I define this normal wait in another separate class and implement this wait in my test cases wherever necessary ?
    – Maha
    Commented Feb 17, 2021 at 17:29
  • yes you can you can return wait using a method or define method as public variable in base class and access it from test class
    – PDHide
    Commented Feb 17, 2021 at 17:32
  • Please accept the answer by clicking the tick sgin
    – PDHide
    Commented Feb 17, 2021 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.