【问题标题】:How to validate the single column values are in alphabetical order of webtable in c# seleniumc# selenium中如何验证单列值是否按webtable的字母顺序排列
【发布时间】:2021-03-04 13:40:52
【问题描述】:

前提:

我有一个三列的网络表格。我需要验证第二列中所有员工的名字是否按字母顺序排列。

此外,在 web 表的顶部,我有一个搜索选项,我需要验证当我搜索一个名称并单击搜索全部时,名称是否以搜索的那个开头。

我在 c#、selenium、spec flow NUnit 中做。

问题:

谁能告诉我怎么做?

【问题讨论】:

标签: c# selenium nunit specflow


【解决方案1】:

您需要遍历所有行并将文本收集到列表中。

List<String> li = new List<String>();

for (int i=0; i<driver.findelement(By.xpath("/table/tbody[1]/tr")).size();i++)
{
li.add(driver.findelement(By.xpath("/table/tbody[1]/tr["+i+"]/td[2]")).getText());
}
List<String> li2 = new List<String>();
li2 = li.Sort();  //you need to have deep copy here (not sure that this is)

之后,您可以在 for 循环中比较这两个列表的每个元素。

【讨论】:

  • 一旦名字出现在列表中,您可以简单地使用Assert.That(li, Is.Ordered);验证列表是否有序
【解决方案2】:

假设这是您的下表。

添加您的 using 调用:

        using System.Linq;
        using System.Collections.Generic;

然后你想为每一行找到你的元素。在这个我们找到名字。这会将带有名字元素的所有值添加到您的列表中。然后它将原始与排序进行比较,它们应该相等。如果您更改元素以查找姓氏,则会失败,因为“Smith”在第一行,“Abbot”在第 2 行,并且不会按字母顺序排列。

        List<String> item = new List<string>();
        // grab the cells that contain the value you want to sort
        IReadOnlyList<IWebElement> cells = Driver.FindElements(By.XPath("//td[@id='fn']"));
        // loop through the and assign the name into the 
        foreach (IWebElement cell in cells)
        {
            item.Add(cell.Text);
        }
        List<string> listDefault = item.ToList();
        List<string> listSorted = listDefault.OrderBy(x => x).ToList(); //sort list asc to compare
        Assert.IsTrue(listDefault.SequenceEqual(listSorted));

桌子:

        <html>
        <head>
        <style>
        table, th, td {
          border: 1px solid black;
          border-collapse: collapse;
        }
        </style>
        </head>
        <body>
        <table style="width:25%">
          <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Age</th>
          </tr>
          <tr>
            <td id="fn">Robert</td>
            <td id="ln">Smith</td>
            <td id="age">25</td>
          </tr>
          <tr>
            <td id="fn">Sally</td>
            <td id="ln">Abbot</td>
            <td id="age">24</td>
          </tr>
        </table>

        </body>

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多