【问题标题】:Get webpage text in List<string> and compare it with other hard codded List<string>获取 List<string> 中的网页文本并将其与其他硬编码 List<string> 进行比较
【发布时间】:2018-03-30 04:19:46
【问题描述】:

我有 3 个类,第一个类我们编写方法,第二个我们调用方法,第三个我们使用它来执行。

在第一堂课中,我编写了下面的 c# 代码来通过 selenium 检索网页中的所有行文本。

public void GetUsedChecklistRecords()
{
    SelectElement records  = new SelectElement(reportsDr.FindElement(By.XPath("//div[contains(text(),'Benefit Checklist')]")));
    IList<IWebElement> options = records.Options;
    foreach(IWebElement option in options)
    {
        System.Console.WriteLine(option.Text);
    }
}

我很困惑如何在二等舱中调用它,然后在三等舱中使用它,如下所示

public void ChecklistUsedReportsTest()
{
    reportsPage.SelectRE("Reporting Entity 02");
    List<String> WorkPaperName = new List<String>(new String[] {"Entertainment","Loan" });
    List<String> reportName = new List<string>(new String[] {"Entertainment Benefit Checklist","Loan Benefit Checklist"});
    for (int a = 0; a < WorkPaperName.Count; a++)
    {
        reportsPage.NavigateToChecklist(WorkPaperName[a]);
        var ActualRecords= reportsPage.GetUsedChecklistRecords();
        List<string> difference = ActualRecords.Except(reportName).ToList();
        foreach (var value in difference)
        {
            Console.WriteLine(value);
        }

        Console.ReadLine();
        Assert.AreEqual(reportName,ActualRecords);
}}

它不起作用,请帮助如何继续。因为第一类中的方法是无效的,所以它应该只在第二类中被称为无效,但是我应该如何使用它来比较第三类

【问题讨论】:

  • 您将需要更改第一堂课中的方法。现在它没有返回任何东西,看起来它只是将文本打印到控制台。

标签: c#


【解决方案1】:

您只需从GetUsedChecklistRecords返回IEnumerable&lt;string&gt;

public IEnumerable<string> GetUsedChecklistRecords()
{
    SelectElement records  = new SelectElement(reportsDr.FindElement(By.XPath("//div[contains(text(),'Benefit Checklist')]")));
    IList<IWebElement> options = records.Options;
    return options.Select(it => it.Text);
 }

  public void ChecklistUsedReportsTest()
  {
     reportsPage.SelectRE("Reporting Entity 02");
     List<String> WorkPaperNames = new List<String>(new String[] {"Entertainment","Loan" });
     List<String> reportNames = new List<string>(new String[] {"Entertainment Benefit Checklist","Loan Benefit Checklist"});
     var actualRecords= reportsPage.GetUsedChecklistRecords();  
     // assume reportNames and actualRecords has no duplicate elements
     var areEqual = reportNames.All(it => actualRecords.Any(ti => it == ti));   
     List<string> difference = actualRecords.Except(reportNames).ToList();

     foreach(var workPaperName in WorkPaperNames)
     {
         reportsPage.NavigateToChecklist(workPaperName);
         foreach (var value in difference)
         {
             Console.WriteLine(value);
         }

         Console.ReadLine();  

     }}

注意事项

  • Assert.AreEqual 不会检查两个 ienumerable 是否有 the same elements

  • 最好以复数形式命名集合名称(WorkPapers 而不是 WorkPaper

  • reportNamesactualRecords 不会在 foreach 循环内修改,因此您可以计算一次 difference 和相等性,而不是在每次迭代时计算
  • 尽可能使用foreach 而不是for

【讨论】:

  • GetUsedChecklistRecords() 给我一个错误,因为“元素应该是“选择”但是“div”......这是否意味着我的路径不正确?
  • 事实上你在SelectElement records = new Sel... 行得到错误,是的,这意味着//div[contains(text(),'Benefit Checklist')] 无效
  • 我为所有三个类修改了如下代码 public IEnumerable GetUsedChecklistRecords() { IList options = reportsDr.FindElements(By.XPath("//div[contains(text (),'福利清单')]"));列表 记录 = 新列表(); foreach(选项中的 IWebElement 元素){ records.Add(element.ToString()); } return options.Select(it => it.Text); }
  • class reportpage.cs 如下 public IEnumerable GetUsedChecklistRecords() { return reportsHelper.GetUsedChecklistRecords(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 2013-06-09
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
相关资源
最近更新 更多