1

I have a task to collect all the links of site, go to and check on each page, there is no error. The problem is that if there is an error on any page, the test is terminated. I want to have checked all the pages and at the end of the test to get the result of all the pages.

public static void OpenAllLinksAndCheckOnErrors()
{
    var collectionReportsLinks = CollectionReportsLinks();
    foreach (string linkToReportPage in collectionReportsLinks)
    {
        Driver.Instance.Navigate().GoToUrl(linkToReportPage);
        Assert.That(string.IsNullOrWhiteSpace(FindErrorsInReportPages()), "Error on page {0}", FindErrorsInReportPages()));
    }
}


public static List<string> CollectionReportsLinks()
{
    List<string> links = new List<string>();
    var collectionReportsItems = Driver.Instance.FindElements(By.TagName("a")).ToList();
    for (int i = 0; i <= collectionReportsItems.Count - 1; i++)
    {
        var menuItemHref = collectionReportsItems[i].GetAttribute("href".ToString());
        links.Add(menuItemHref);
    }
    return links;
}

public static string FindErrorsInReportPages()
{
    string errorText = string.Empty;
    var reportItem = Driver.Instance.FindElements(Selectors.AnyPage.GetReportPageError()).FirstOrDefault();
    if (reportItem == null)
        return errorText;
    if (reportItem.Displayed)
        errorText = reportItem.Text;
    return errorText;            
}
BCR
  • 71
  • 4

2 Answers2

0

Do not do the assert at first place; instead create another collection/list and log every error on console and for the sake of testing fail the test at the end if any error occurs.

private static List<string> _errorList;

public static void OpenAllLinksAndCheckOnErrors()
{
    _errorList = new List<string>();

    var collectionReportsLinks = CollectionReportsLinks();
    foreach (string linkToReportPage in collectionReportsLinks)
    {
        Driver.Instance.Navigate().GoToUrl(linkToReportPage);
        _errorList.AddRange(new[]{FindErrorsInReportPages()});

         //add a console message if you want
        //Assert.That(string.IsNullOrWhiteSpace(FindErrorsInReportPages()), "Error on page {0}", FindErrorsInReportPages()));
    }

    //Just failed the test if error occurs.
    foreach(string error in _errorList)
    {
        if (string.IsNullOrWhiteSpace(error))
        {
            Assert.Fail("Failure message");
        }
    }
}
Saifur
  • 16,081
  • 6
  • 49
  • 73
  • Thanks for answer! I tried to apply your decision , but the test still falls as soon as it finds the first error :( – BCR Mar 19 '15 at 19:49
  • Then your failure is somewhere else. Can you provide me more info? – Saifur Mar 19 '15 at 20:10
  • Hmm ... Do not know even what additional information you provide. I used the code exactly as you wrote. The only thing that I call the method OpenAllLinksAndCheckOnErrors () in a separate method. And falls just at checkout foreach (string error in _errorList) {if (string.IsNullOrWhiteSpace (error)) {Assert.Fail ("Failure message"); }} That is, as soon as it finds an error on this page, and test assertions triggered stops executing, but I want to work out the whole loop of program – BCR Mar 19 '15 at 20:35
  • Ya I guess that's right then `foreach (string linkToReportPage in collectionReportsLinks) { Driver.Instance.Navigate().GoToUrl(linkToReportPage); _errorList.AddRange(new[]{FindErrorsInReportPages()}); //Assert.That(string.IsNullOrWhiteSpace(FindErrorsInReportPages()), "Error on page {0}", FindErrorsInReportPages())); }` meaning page navigation is done and now you want to fail test if it is a real failure and since selenium found some error it's failing. Isn't it correct? – Saifur Mar 19 '15 at 20:43
  • I would like to test were conducted on all pages and at the end of the test would be represented by the general result – BCR Mar 20 '15 at 04:46
  • Then you should write some tests that go through all the links and click load and looks for any error. The code above is too generic – Saifur Mar 20 '15 at 04:57
  • Here the situation is a little unusual. I need to check several similar sites in which different number of reports. For each site, write a separate test did not want to. I thought that there was some way to avoid it. Whatever it was, thank you very much for your help! – BCR Mar 20 '15 at 06:24
0

You can check all the pages for errors and collect the results in a list then assert that the list of errors contains all null entries. You should get a decent message on failure also.

public static void OpenAllLinksAndCheckOnErrors()
{  var errorsList as new List<string>
   var collectionReportsLinks = CollectionReportsLinks();
   foreach (string linkToReportPage in collectionReportsLinks)
    {
       Driver.Instance.Navigate().GoToUrl(linkToReportPage);
       errorsList.Add(FindErrorsInReportPages())
    }  
   Assert.That(errorsList, Is.All.Null)
}

If you are uncomfortable with the list and collection assert you could use a stringbuilder...