获取List <string>中的网页文本,并将其与其他硬编码List <string>进行比较

时间:2018-03-30 04:19:46

标签: c#

我有3个类,第一个用于编写方法,第二个用于调用方法,第三个用于执行。

在First Class中,我在下面写了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);
}}

它无法正常工作,请帮助您继续操作。因为第一个类中的方法是void,那么它应该只在第二个类中被调用为void,但是我应该如何在第三个类中使用它进行比较

1 个答案:

答案 0 :(得分:0)

您只需从IEnumerable<string>

返回GetUsedChecklistRecords即可
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不会检查两个无数人是否有the same elements

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

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