简体   繁体   English

遍历表中的 webelement 列表并断言每个字符串相等

[英]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.我的目标是遍历表中的 webelement 列表(使用过滤器生成),分布在多个页面中,并在 Cucumber 步骤中使用提供的字符串断言这些 webelement 中的每个字符串相等。

  • These webelements consist of strings (1 webelement = 1 string) placed within a column within the table.这些 webelement 由放置在表中的列中的字符串(1 个 webelement = 1 个字符串)组成。

  • All these strings equal.所有这些字符串都相等。

  • Their data-testid is the same.他们的数据-testid 是一样的。

  • 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:问题出在 addAll() 上,原因很简单:

"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.我必须使用 add(),并在 for 循环中创建一个辅助数组。

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

Then, the assert is like this:然后,断言是这样的:

List list = javaMethods.列表列表 = javaMethods. sameFilterResultIsDisplayedForAllRows(); sameFilterResultIsDisplayedForAllRows();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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