更改后验证动态Web表的顺序

时间:2019-06-27 20:51:23

标签: selenium-webdriver

我正在使用Selenium Web驱动程序在网站上编写自动化测试脚本。更改顺序后,我需要验证动态Web表的顺序。到目前为止,我已经编写了将所有元素放入每个行和列的列表的代码。单击网站上该行的更改顺序后,如何验证显示的顺序正确?

例如如果我将第一行向下移动,使其成为第二行,第二行成为第一行,则需要确认它们已被交换。

这是Web表的html代码。

<table class="favTable">
  <caption><h2 class="hideCaption">My Favorite Reports</h2></caption>
<tbody>
  <tr class="headerRow">
    <th scope="col" class="colCO">Change Order</th>
    <th scope="col" class="colFN">Favorite Name</th>
    <th scope="col" class="colRF">Remove Favorite</th>
    <th scope="col" class="colEFN">Edit Favorite Name</th>
   </tr>
  <tr> 
    <td class="colCO">
      <a href="#" onclick="return UpSort(this);">
        <img alt="Increase Order" src="Resources/Images/up_sort.png">
       </a>
      <a href="#" onclick="return DownSort(this);">
        <img alt="Decrease Order" src="Resources/Images/down_sort.png">
       </a>
     </td>
    <td class="colFN editableCell">
      <a id="150219030000EditLink" href="/app/FDR2019/FormularySearch.aspx?PI=150219030000">CY 2019 Search Formulary
      </a>
       <label for="150219030000EditBox" class="HiddenText" style="display:none;">CY 2019 Search Formulary Favorite Name</label><input id="150219030000EditBox" class="favNameEditBox" style="display:none;" maxlength="50" type="text" value="">
       <input id="150219030000SaveNameBtn" style="display:none;" type="button" value="Save" onclick="return SaveFavoriteTitle(this, '150219030000');">
       <input id="150219030000CancelNameBtn" class="favTitleCancelBtn" style="display:none;" type="button" value="Cancel" onclick="return CancelEditFavorites(this);">
     </td>
    <td class="colRF">
      <a href="#" onclick="return RemoveFavorite(this, '150219030000');">
        <img alt="Remove From Favorites" src="Resources/Images/remove.png">
        </a>
      </td>
    <td class="colEFN">
      <a href="#" onclick="return EditFavorites(this, '150219030000', 'true');">
      <img alt="Edit Favorite Name" src="Resources/Images/edit.png">
      </a>
     </td>
    <td style="display:none">
      <span class="pageIDCol">150219030000</span>
    </td>
  </tr>
  <tr>...</tr>
  <tr>...</tr>
  <tr>...</tr>
</tbody>

这是我编写的将值放入两个列表的代码。

@Keyword

public getWebTable(){

  WebDriver driver = DriverFactory.getWebDriver();

  WebElement table = driver.findElement(By.xpath("//table/tbody"));

  List <WebElement> rowsTable = table.findElements(By.tagName('tr'));

  int rowsCount = rowsTable.size();

  System.out.println('Number of rows in the table are ' + rowsCount);

  for (int i = 0; i < rowsCount; i++) {

    List <WebElement> rowColumns = rowsTable.get(i).findElements(By.tagName('td'));

    int colsCount = rowColumns.size();

    System.out.println((('Number of columns in row number ' + i) + ' are ') + colsCount);

    for (int j = 0; j < colsCount; j++) {

      String content = rowColumns.get(j).getText();

      System.out.println((((('Element text in row number ' + i) + ' and column number ') + j) + ' is ') + content);

    }

  }

}

这是网站上Web表的图片。任何帮助将不胜感激。谢谢。

picture of web table with increase and decrease order button

1 个答案:

答案 0 :(得分:0)

您可以在indexOf()中直接检查WebElementList<WebElement>或检查文本值的索引,如下所示:

List<WebElement> elements = driver.findElements(By.id("someId")); // dummy locator
List<String> texts = new ArrayList<String>();
for (WebElement element: elements) {
    texts.add(element.getText());
}
int positionOfFoo1Text = texts.indexOf("Foo1") + 1; // indexed from 0
int positionOfFoo2Text = texts.indexOf("Foo2") + 1;

// clear texts before changing DOM
texts.clear();