簡體   English   中英

遍歷表中的 webelement 列表並斷言每個字符串相等

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

我的目標是遍歷表中的 webelement 列表(使用過濾器生成),分布在多個頁面中,並在 Cucumber 步驟中使用提供的字符串斷言這些 webelement 中的每個字符串相等。

  • 這些 webelement 由放置在表中的列中的字符串(1 個 webelement = 1 個字符串)組成。

  • 所有這些字符串都相等。

  • 他們的數據-testid 是一樣的。

  • 這些網絡元素分布在多個頁面上(動態)。

  • 當到達最后一頁時,循環將結束,其中包含一個按鈕,該按鈕的屬性文本被禁用(顯示最后一頁時)。

    這是我開始寫的內容,但此刻我有點卡住了。 如果你能告訴我如何繼續下去,我真的很高興。 目前,我在執行測試時收到此錯誤。

1. Tests in error:

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

實際代碼:

    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);

問題出在 addAll() 上,原因很簡單:

“如果在操作進行時修改了指定的集合,則此操作的行為是未定義的。(請注意,如果指定的集合是此列表,並且它是非空的,則會發生這種情況。)”

我必須使用 add(),並在 for 循環中創建一個輔助數組。

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

然后,斷言是這樣的:

列表列表 = javaMethods. 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