0

I'm using Selenium with Java from the Mavenproject.

My code is working, I'm just wondering if it can be improved. In the code below you can see I'm looking for a few elements and if they are displayed or not.

The issue is that I'm looking for tons of elements +- 50. So I have about 50 of these lines. I'm struggling to find a more efficient way. Isn't a easier way of writing this down in 1 line searching for multiple elements and checking if all are displayed?

Like Find.... A,B,C,D,...,Y,Z .isDisplayed?

    boolean function_detail_breadcrumb_1_displayed = driver.findElement(By.cssSelector("[data-core2='403']")).isDisplayed();
    boolean function_detail_breadcrumb_2_displayed = driver.findElement(By.cssSelector("[data-core2='405']")).isDisplayed();
    boolean function_detail_breadcrumb_3_displayed = driver.findElement(By.cssSelector("[data-core2='407']")).isDisplayed();
    boolean function_detail_breadcrumb_4_displayed = driver.findElement(By.cssSelector("[data-core2='410']")).isDisplayed();
    boolean function_detail_breadcrumb_5_displayed = driver.findElement(By.cssSelector("[data-core2='413']")).isDisplayed();

2 Answers 2

1

If you only want to check certain breadcrumbs:

String[] breadCrumbs = new String[]{"403", "405", "407", "410", "413"};
for (String breadCrumb : breadCrumbs) {
    String selector = String.format("[data-core2='%s']", breadCrumb);
    boolean breadCrumbDisplayed = driver.findElement(By.cssSelector(selector)).isDisplayed();
}
Sign up to request clarification or add additional context in comments.

Comments

1

Note: I am assuming, you are checking whether all elements are displayed or not.

First, You find elements of the kinds:

List<WebElement> breadCrumbList = driver.findElements(By.cssSelector("Your selector"));

Then iterate through your breadcrumbs and check:

        List<WebElement> breadCrumbList = driver.findElements(By.cssSelector("Your selector"));
        boolean isAllDisplayed = true;
        for(WebElement breadCrumb : breadCrumbList){
            if(breadCrumb.isDisplayed() == false){
                isAllDisplayed = false;
                break;
            }
        }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.