Verify Order Of HTML Elements With Attribute Values Such as Class="Group0-Item1" Class="Group0-Item2" Class="Group1-Item1"

2020-02-15 c# visual-studio selenium nunit

In my Selenium/C#/NUNIT project, I need to find a way to validate the order (top down hierarchy of a page's HTML) for a group of HTML elements (as well as the elements contained within those groups). These are my elements that show inside my page's HTML...

<div class="gapBanner-banner-order1-group0"></div>
<div class="gapBanner-banner-order1-group1"></div>
<div class="gapBanner-banner-order1-group2"></div>
<div class="gapBanner-banner-order2-group2"></div>

The validation I want to perform should be able to catch the following bugs:

Bug 1: The groups are not in order within the page's HTML. One of the elements that is in group1 appears first in the HTML before group0...

<div class="gapBanner-banner-order1-group1"></div>
<div class="gapBanner-banner-order1-group0"></div>
<div class="gapBanner-banner-order1-group2"></div>
<div class="gapBanner-banner-order2-group2"></div>

Bug #2: The elements WITHIN each group are not in order within the page's HTML. Group2-Order2 appears before Group2-Order1 within the HTML

<div class="gapBanner-banner-order1-group0"></div>
<div class="gapBanner-banner-order1-group1"></div>
<div class="gapBanner-banner-order2-group2"></div>
<div class="gapBanner-banner-order1-group2"></div>

The below is what I have coded so far, but it is definitely not going to do the job, not to mention, it is very messy. I cant figure out what kind of logic I need for this

            /// 5. Verify the correct order of elements in which they appear inside the HTML
            List<IWebElement> CustomPageHTMLComponents = Browser.
            FindElements(By.XPath("//div[contains(@class, 'group')")).ToList();            
            List<IWebElement> uniqueGroups = new List<IWebElement>();
            // Get the unique groups
        for (int i = 0; i < CustomPageHTMLComponents.Count; i++)
        {
            IWebElement currentComponent = Browser.FindElements(By.XPath("//div[contains(@class, 'group')"))[i];
            string toBeSearched = "group";
            string currentComponenetClassAttributeValue = currentComponent.GetAttribute("class");
            int x = currentComponenetClassAttributeValue.IndexOf(toBeSearched);
            string groupNumber = currentComponenetClassAttributeValue.Substring(x + toBeSearched.Length);

            if (groupNumber == i.ToString())
            {
                uniqueGroups.Add(currentComponent);
            }
        }

             // Some kind of logic to verify everything???
        for (int i = 0; i < Page.CustomPageHTMLComponents.Count; i++)
        {
            IWebElement currentComponent = Browser.FindElements(By.XPath("//div[contains(@class, 'group')"))[i];

            string toBeSearched = "group";
            string currentComponenetClassAttributeValue = currentComponent.GetAttribute("class");
            int x = currentComponenetClassAttributeValue.IndexOf(toBeSearched);
            string groupNumber = currentComponenetClassAttributeValue.Substring(x + toBeSearched.Length);

            Assert.AreEqual(groupNumber, i.ToString());
        }

Answers

There are probably a number of ways to do this. This is the first way I came up with...

  1. Grab all the class names from the desired elements and store them in string array #1
  2. Make a copy of string array #1 and sort it
  3. Compare the two arrays and if they are equal, then they were sorted to start with

I've checked the HTML you provided for the bugs you'd like to catch and it catches them all. The one issue I can think of is if you get more than 9 orders or groups the sorting will not be what you want because it's alpha order not numerical order, e.g. 1, 10, 2, ... instead of 1, 2, ... 10.

// capture the class names from the desired classes
string[] elements = _driver.FindElements(By.CssSelector("div[class^='gapBanner-banner-']")).Select(e => e.GetAttribute("class")).ToArray();
// make a copy of the array
string[] sortedElements = new string[elements.Length];
elements.CopyTo(sortedElements, 0);
// sort the copy
Array.Sort(sortedElements);
// compare the arrays for order using NUnit CollectionAssert
CollectionAssert.AreEqual(elements, sortedElements, "Verify ordering of elements");

Related