Home > Back-end >  How to iterate webelements based on Xpath contains in Java Selenium?
How to iterate webelements based on Xpath contains in Java Selenium?

Time:01-19

I need to create function which need to validate all column titles on Web UI which are Name, Age, Gender and Height:

I got xpath of column title ready:

List<Webelement> allLinks= driver.findElements(By.xpath("//button[@class='sort-header-btn']"));

individual working xpath is:

//button[@class='sort-header-btn' and contains(text(),'Name')]

I can't store every xpath individually like this. I decide to iterate every xpath:


Iterator<WebElement> itr = allLinks.iterator();
while(itr.hasNext()) {
if(itr.next().getText().contains("Name")){
    System.out.println("Name column is validated");
break;
}
else{
System.out.println("Name column is not validated");
break;
}
if(itr.next().getText().contains("Gender")){
    System.out.println("Gender column is validated");
break;
}
else{
System.out.println("Gender column is not validated");
break;
}
//same way I follow remaining columns
}

Is this is the correct way to do it?

CodePudding user response:

The approach you've described for iterating through the list of WebElements and checking the text of each element to validate the column titles is generally correct from my point of view, folks can have different opinion.

However, there are a couple of improvements you can make to your code:

Use a for loop instead of an iterator. This will make the code more readable and easier to understand.

for (WebElement element : allLinks) {
    if (element.getText().contains("Name")) {
        System.out.println("Name column is validated");
    } else {
        System.out.println("Name column is not validated");
    }
    if (element.getText().contains("Gender")) {
        System.out.println("Gender column is validated");
    } else {
        System.out.println("Gender column is not validated");
    }
    // same way for remaining columns
}

Instead of using a series of if-else statements for each column, you can use a switch case statement to make the code more readable and maintainable.

for (WebElement element : allLinks) {
    String columnTitle = element.getText();
    switch (columnTitle) {
        case "Name":
            System.out.println("Name column is validated");
            break;
        case "Age":
            System.out.println("Age column is validated");
            break;
        case "Gender":
            System.out.println("Gender column is validated");
            break;
        case "Height":
            System.out.println("Height column is validated");
            break;
        default:
            System.out.println(columnTitle   " is not a valid column title");
            break;
    }
}

It's also a good practice to have a boolean variable to check if any column title is not validated, then you can fail the test if any column title is not validated.

Additionally, you can also use the assertTrue() method to assert that the column title is present in the list of WebElements. This will make the code more robust and will allow you to handle any exceptions in a more graceful manner.

for (String title : new String[]{"Name", "Age", "Gender", "Height"}) {
    boolean isValidated = false;
    for (WebElement element : allLinks) {
        if (element.getText().contains(title)) {
            System.out.println(title   " column is validated");
            isValidated = true;
            break;
        }
    }
    assertTrue(isValidated);
}
  • Related