0

I have 3 classes , First class in which we write methods, second we call the methods, third in which we use it for our execution.

In the First Class i have written below c# code to retrieve all the row text in webpage through 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);
    }
}

I am confused how to call it in second class and then use it in third class which looks as below

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

It's not working, please help how to proceed. because the method in the first class is void then it should be called as void only in second class, but then how should i use it to compare in third class

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • You will need to change the method in the first class. Right now it does not return anything, it looks like it just prints the text to the console. – Cory Mar 30 '18 at 04:29

1 Answers1

0

All you need is to return IEnumerable<string> from 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();  

     }}

Notes

  • Assert.AreEqual won't check if two ienumerable has the same elements

  • It's good practice to name collections name in plural (WorkPapers instead of WorkPaper

  • reportNames and actualRecords aren't modified inside foreach loop, so you can calculate difference and equality once, instead of on each iteration
  • Use foreach instead of for whenever possible
tchelidze
  • 8,050
  • 1
  • 29
  • 49
  • its giving me error for GetUsedChecklistRecords() as "Element should have been “select” but was “div”"....... Does it means my path is incorrect? – Nidhii Singh Mar 30 '18 at 10:11
  • In fact you get error on `SelectElement records = new Sel...` line, yes, that means that `//div[contains(text(),'Benefit Checklist')]` is invalid – tchelidze Mar 30 '18 at 10:24
  • i modified my code as below for all the three classes public IEnumerable GetUsedChecklistRecords() { IList options = reportsDr.FindElements(By.XPath("//div[contains(text(),'Benefit Checklist')]")); List records = new List(); foreach (IWebElement element in options) { records.Add(element.ToString()); } return options.Select(it => it.Text); } – Nidhii Singh Mar 30 '18 at 11:40
  • class reportpage.cs as below public IEnumerable GetUsedChecklistRecords() { return reportsHelper.GetUsedChecklistRecords(); } – Nidhii Singh Mar 30 '18 at 11:43