C# 使用Class=";等属性值验证HTML元素的顺序;组0-Item1“;类别=“类”;组0-Item2“;类别=“类”;组1-项目1“;
在Selenium/C#/NUNIT项目中,我需要找到一种方法来验证一组HTML元素(以及这些组中包含的元素)的顺序(页面HTML的自上而下层次结构)。这些是我的元素,显示在我的页面的HTML中C# 使用Class=";等属性值验证HTML元素的顺序;组0-Item1“;类别=“类”;组0-Item2“;类别=“类”;组1-项目1“;,c#,visual-studio,selenium,nunit,C#,Visual Studio,Selenium,Nunit,在Selenium/C#/NUNIT项目中,我需要找到一种方法来验证一组HTML元素(以及这些组中包含的元素)的顺序(页面HTML的自上而下层次结构)。这些是我的元素,显示在我的页面的HTML中 <div class="gapBanner-banner-order1-group0"></div> <div class="gapBanner-banner-order1-group1"></div> <div class="gapBanner-b
<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>
Bug#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());
}
///5。验证元素在HTML中的显示顺序是否正确
列出CustomPageHTMLComponents=浏览器。
FindElements(By.XPath(“//div[contains(@class,'group')))).ToList();
List uniqueGroups=新列表();
//获取唯一的组
对于(int i=0;i
可能有很多方法可以做到这一点。这是我想到的第一种方法
顺序
s或组
s,排序将不是您想要的,因为它是字母顺序而不是数字顺序,例如1、10、2,…而不是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");