当我运行我的测试时,我想确定是否在页面上选择了正确的类别(bn)并激活了它。这是由<li class="active">
完成的。在我的测试数据中,我将所有产品都匹配到每个类别,所以我知道选项卡何时应该是活动的,但我无法弄清楚如何断言这是正确的。谁能帮助我朝正确的方向发展? :)
以下是HTML代码:
<div class="tabs">
<h3><span>Hovedkategorier</span></h3>
<div class="content">
<ul class="shortcuts">
<li><a href=""><span class="one"><span class="two">Text 1</span></span></a></li>
<li class="last"><a href="?tag=weekly1"><span class="one"><span class="two">Text 2</span></span></a></li>
</ul>
<ul class="main">
<li><a href="?bn=10000"><span class="one"><span class="two">Text 3</span></span></a></li>
<li><a href="?bn=10306"><span class="one"><span class="two">Text 4</span></span></a></li>
<li><a href="?bn=10723"><span class="one"><span class="two">Text 5</span></span></a></li>
<li class="active"><a href="?bn=10719"><span class="one"><span class="two">Text 6</span></span></a></li>
<li><a href="?bn=10190"><span class="one"><span class="two">Text 7</span></span></a></li>
<li><a href="?bn=10444"><span class="one"><span class="two">Text 8</span></span></a></li>
<li><a href="?bn=10431"><span class="one"><span class="two">Text 9</span></span></a></li>
<li class="last"><a href="?bn=10560"><span class="one"><span class="two">Text 10</span></span></a></li>
</ul>
</div>
</div>
答案 0 :(得分:0)
考虑选择您的元素by class name,如下所示:
List<WebElement> activeListElements = driver.findElements(By.className("active"));
接下来,您应该在<a>
:
<li class="active">
List<WebElement> activeLinks = activeListElements.findElements(By.tagName("a"));
之后,您可以使用WebElement.getText()
检查其内容:
String url = element.getAttribute("href");
assertThat(url, is("?bn=10000")); // for recent JUnit 4.x versions
assertEquals("?bn=10000", url); // for older versions of JUnit
答案 1 :(得分:0)
不确定我是否理解正确,我的理解是您知道Text 6
或href="?bn=10719"
应该是有效标签。因此,您可以使用text或href找到它,然后检查其父<li>
标记是否实际上具有类active
。
// using text is not applicable in your case, as they will be in different languages.
// WebElement tab = driver.findElements(By.xpath("//a[text()='Text 6']/.."));
int number = 10719;
WebElement liActive = driver.findElement(By.xpath(".//a[@href='?bn=" + number + "']/..")); // use '/..' to get <a>'s parent, which is the <li>
String classNames = liActive.getAttribute("class");
// using whatever testing framework you prefer
// assert true that
// classNames.toLowerCase().contains("active")
// if you are sure that active will be the only class for li, you can check equality
// classNames.toLowerCase().Equals("active")