How to verify if all values are true and present on a List

Lewis, Kelcey 1 Reputation point
2021-05-04T21:11:25.217+00:00

I have a list of 12 string values in the MaterialUsedList that I pull from a row of text from a website I am testing. I need to be able to go through the list and verify that each value is actually included on the list . The way my code is set up now I will have to amke12 separate foreach statements to cover all the values. Is there a way I can loop through all the values on the list and verify if they are present without make 12 different for each statements.

Values in List:
AB
1
EACH
8.62
Classrooms
Fumigation
Bazooka
Beetles
Employee
Date
Save
Exit

        IList<IWebElement> MaterialsUsedList = UIServices._workOrderCompletedService.GetMaterials();
        bool x = false;
        foreach(IWebElement e in MaterialsUsedList)
        {
            if (e.Text.Equals(Employee))
            {
                x = true;
            }
        }
        Assert.IsTrue(x);
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,031 Reputation points
    2021-05-05T01:17:29.567+00:00

    Hello,

    Unsure how this task is to be carried out as you mentioned string list but are working with if (e.Text.Equals(Employee)) were Employee does not appear to be a string, seems more like an instance of a class???

    Now for comparing strings in a list you can consider using SequenceEqual.

    Be cognitive that if using SequenceEqual

    • Order matters
    • Casing matters

    Example test

    [TestMethod]  
    [TestTraits(Trait.GeneralComparing)]  
    public void Compare_string_ArraySequenceEqual_LoweredCase()  
    {  
        /*  
         * Equal as param 2  
         */  
        Assert.IsTrue(MonthNamesArray().SequenceEqual(  
            MonthNamesLowerCasedArray(),   
            StringComparer.CurrentCultureIgnoreCase));  
          
        /*  
         * Unequal as case matters  
         */  
        Assert.IsFalse(MonthNamesArray().SequenceEqual(MonthNamesLowerCasedArray()));  
    }  
    

    In a test base class

    public static string[] MonthNamesLowerCasedArray() => MonthNamesList().Select(month => month.ToLower()).ToArray();  
      
    public static string[] MonthNamesArray() => MonthNamesList().ToArray();  
    

    If I'm off base my apologies.

    0 comments No comments

  2. Timon Yang-MSFT 9,571 Reputation points
    2021-05-05T06:05:11.637+00:00

    Please try this code:

                List<string> strs = new List<string>() { "AB", "1", "EACH", "Employee" };  
                IList<IWebElement> MaterialsUsedList = UIServices._workOrderCompletedService.GetMaterials();  
                List<string> texts = MaterialsUsedList.Select(s => s.Text).ToList() ;  
                bool re = strs.All(texts.Contains);  
    

    First extract the property IWebElement.Text that needs to be compared, and then if you need the result list to be the same as the previous list elements and the order of the elements is also the same, please follow Karen's suggestion to use SequenceEqual, if the order may be different, you can use Enumerable. All.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments