简体   繁体   中英

Iterate through a list of webelements within a table and assert equal each string

My goal is to iterate through a list of webelements (generated upon using a filter) within a table, spread across multiple pages and assert equal each string within those webelements with a provided string in a Cucumber step.

  • These webelements consist of strings (1 webelement = 1 string) placed within a column within the table.

  • All these strings equal.

  • Their data-testid is the same.

  • These webelements are spread across a number of pages (dynamic).

  • The loop would end when reached the last page, which contains a button of which attribute text becomes disabled (when last page is displayed).

    Here's what I started writing, but I'm a bit stuck at the moment. If you can advise me how to continue further, I'm really greateful. At the moment, I get this error, when I execute the test.

1. Tests in error:

  stale element reference: element is not attached to the page document
 2.  Not sure how to integrate the assert.

Actual code:

    By nextPageBtn = By.xpath("//*[@data-testid='asd']");

    By disabledNextPageBtn = By.xpath("//*[@data-testid='asdf']");

    By filterValue = By.xpath("//*[@data-testid='asdf1']");
    

    public List<String> sameFilterResultIsDisplayedForAllRows() {

        List<WebElement> filterResultsList = new ArrayList<WebElement>();
        List<String> stringsFromFilterResultsList = new ArrayList<String>();

        boolean disabledNext = false;

        do {
            click(driver, nextPageBtn);

            filterResultsList.addAll(driver.findElements(filterValue));

            try {
                getValueFromSomeAttribute(driver, disabledNextPageBtn,
                        "randomElementAttribute");
                disabledNext = true;

            } catch (Exception e) {

            }


        } while (disabledNext == false);


        for (WebElement filterResultsList) {
            System.out.println(a.getText());

            try {
                stringbookings.add(a.getText());
            } catch (Exception e) {

            }

        }

        return stringsFromFilterResultsList;
    }


The assert would be something like this:

    @Then("the results filtered for all rows contain value {string}")
    public void expectedFilteredValues(String expectedFilteredValueString) {

    String expectedFilteredResult;
    expectedFilteredResult = 'randomString';

    List<String> actualFilteredValues = javaMethods.sameFilterResultIsDisplayedForAllRows();

    Assert.assertEquals(expectedFilteredResult, actualFilteredValues);

The issue resided with addAll(), prolly because of this:

"The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)"

I had to use add(), and create a secondary array within a for loop.

for (WebElement element : elements) { stringsFromFilterResultsList.add(bookingTypeElement.getText()); }

Then, the assert is like this:

List list = javaMethods. sameFilterResultIsDisplayedForAllRows();

 for (int i = 0; i < list.size(); i++) { Assert.assertEquals(expectedValue, list.get(i)); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM