question

LewisKelcey-4047 avatar image
0 Votes"
LewisKelcey-4047 asked LewisKelcey-4047 action

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

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);
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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.








5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.