0

At one of my test cases, if based on some configurations, determined values on a table should not be displayed.

I've made a code to show up a message if a predicted value not appears, as following:

public void AssertRecord(string recordExpected)
{
    try
    {
        Assert.AreEqual(recordExpected, RecordGridCell.GetGridCellAtPosition1BasedOnAnyValue(recordExpected));
    }
    catch (Exception)
    {
        Console.WriteLine(recordExpected + " not found.");
    }
}

By the way, the function above GetGridCellAtPosition1BasedOnAnyValue is here:

public static string GetGridCellAtPosition1BasedOnAnyValue(this IWebElement table, string value)
{
    UtilFunctions.WaitForLoadingBar();
    IList<IWebElement> tableRow = table.FindElements(By.XPath(".//tr//td[text()='" + value + "']//ancestor::tr//td[1]"));
    return tableRow.First().GetAttribute("innerHTML");
}

So, with the code I got today, I get an output saying, for example, "Chocolate Bar not found.". If the value exists, it throws nothing.

My problem is: if is there any value on the table that shouldn't be there and that can't be predicted, it will not be reported.

I need help to find a way to be reported if there's any value on the table that shouldn't be there.

2
  • Do you have a pre-defined list of what records are expected in the table? Commented Jul 21, 2017 at 19:58
  • @Bᴜᴅɪ Yep, It's defined by the test case. Commented Jul 21, 2017 at 20:01

1 Answer 1

1

Since you have a pre-defined list of what records are expected in the table, I would use that list and check it against the actual list of records in the table via Enumerable.Except.

Here's some code to give you an idea:

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

var expectedRecords = new List<string> { "A", "B", "C", "D" };
var actualRecords   = new List<string> { "A", "C", "E" };

//var actualRecords = table.FindElements(".//tr//td[1]")
//                         .Select(e => e.GetAttribute("innerHTML"))
//                         .ToList();

var notFoundRecords = expectedRecords.Except(actualRecords); // [ "B", "D" ]
var invalidRecords  = actualRecords.Except(expectedRecords); // [ "E" ]

This will give you lists of both what's not in the actual list but should be (notFoundRecords), and what's in the actual list but should not be (invalidRecords).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.