验证具有属性值的HTML元素的顺序,例如Class =“ Group0-Item1” Class =“ Group0-Item2” Class =“ Group1-Item1”

问题描述 投票:0回答:1

在我的Selenium / C#/ NUNIT项目中,我需要找到一种方法来验证一组HTML元素(以及这些组中包含的元素)的顺序(页面HTML的自上而下的层次结构)。这些是显示在页面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>

我要执行的验证应该能够捕获以下错误:

错误1:群组在页面HTML中的顺序不正确。 group1中的元素之一在HTML中首先出现在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>

错误#2:每个组中的元素在页面的HTML中排列不正确。在HTML中,“ Group2-Order2”出现在“ Group2-Order1”之前

<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>

下面是我到目前为止编写的代码,但是绝对不会完成这项工作,更不用说了,这很混乱。我无法弄清楚对此需要哪种逻辑

            /// 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());
        }

在我的Selenium / C#/ NUNIT项目中,我需要找到一种方法来验证一组HTML元素(以及这些组中包含的元素)的顺序(页面HTML的自上而下的层次结构)。 ...

c# visual-studio selenium nunit
1个回答
0
投票

可能有多种方法可以做到这一点。这是我想出的第一种方法...

© www.soinside.com 2019 - 2024. All rights reserved.