Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit Wait for findElements in Selenium Webdriver

After login, the page is redirecting to one page (I want to wait for page load), where I am finding elements by tagName,

By inputArea = By.tagName("input");
 List <WebElement> myIput = driver.findElements(inputArea);

Here I want to give Explicit Wait for findElements, I want to wait for its all its visibility or presence. There are only two inputs in my web page. If I give Implicit Wait for a long time, the code will work. But it varies. So i decided to give Explicit Wait, How can i give explicit Wait for findElements?. Or How Can I check the Visibility of the second one from the List(List myIput). ie, myIput.get(1). When I give visibilityOfAllElements() like below it throws error.

WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30);      
By inputArea = By.tagName("input");
List <WebElement> myIput = driver.findElements(inputArea);
waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput));
myIput.get(1).sendKeys("Test");

Here is the list of code I am using in my automation program.

package mapap;
import java.util.ArrayList;
import java.util.List;

import lib.ReadExcellData;
import lib.WriteExcellData;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class EditForm {
    public static WebDriver driver;
    static String excelName         = "D:\\xlsx\\map2.xlsx";
    ReadExcellData readData         = new ReadExcellData(excelName);
    WriteExcellData writeData       = new WriteExcellData(excelName); 
    String baseUrl                   = readData.getExcellData("base", 0, 0);    
    By colRadio;
    ExtentReports  report;
    ExtentTest logger;

    @BeforeClass
    public void browserOpen() throws Exception{

        report = new ExtentReports("D:\\driver\\extentRepo\\Edit Map Forms.html", true);
        logger = report.startTest("Map Forms Automation Testing", "Adding Forms");

        driver = new FirefoxDriver();       
        driver.get(baseUrl);
        String username = readData.getExcellData("user", 1, 0);
        String password = readData.getExcellData("user", 1, 1); 
        WebDriverWait waitForUserName = new WebDriverWait(driver, 250);
        By usernameField = By.name("username");
        waitForUserName.until(ExpectedConditions.elementToBeClickable(usernameField)).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.xpath("//input[contains(@src,'/images/signin.png')]")).click();

    }

    @Test(priority = 1)
    public void addingForm() throws Exception{      
            driver.navigate().to(baseUrl+"/anglr/form-builder/dist/#/forms");
        driver.manage().window().maximize();       
        WebDriverWait waitForFormLabel = new WebDriverWait(driver, 30);      
        By inputArea = By.tagName("input");
        List <WebElement> myIput = driver.findElements(inputArea);
        waitForFormLabel.until(ExpectedConditions.visibilityOfAllElements(myIput));
        myIput.get(1).sendKeys("Test");


    }

}

Please note: if i gave Thread.sleep for a long time after the code "driver.navigate().to(baseUrl+"/anglr/form-builder/dist/#/forms");", I will get all WebElements. But I want to avoid this, I want to just wait for WebElements to load ().

Anyone Please help.

like image 633
Shino Avatar asked Oct 24 '25 03:10

Shino


1 Answers

You can do something like this:

//explicit wait for input field field
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("input")));

ExpectedConditions class can be useful in a lot of cases and provides some set of predefined condition to wait for the element. Here are some of them:

  • alertIsPresent : Alert is present
  • elementSelectionStateToBe: an element state is selection.
  • elementToBeClickable: an element is present and clickable.
  • elementToBeSelected: element is selected
  • frameToBeAvailableAndSwitchToIt: frame is available and frame selected.
  • invisibilityOfElementLocated: an element is invisible
  • presenceOfAllElementsLocatedBy: present element located by.
  • textToBePresentInElement: text present on particular an element
  • textToBePresentInElementValue: and element value present for a particular element.
  • visibilityOf: an element visible.
  • titleContains: title contains
like image 69
Cosmin Avatar answered Oct 26 '25 17:10

Cosmin