0

当我运行我的测试时,我想确定是否选择了正确的类别(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>
4

2 回答 2

0

考虑按类名选择元素,如下所示:

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
于 2013-07-11T07:47:00.877 回答
0

不确定我是否正确理解了您,我的理解是您知道Text 6href="?bn=10719"应该是活动选项卡。所以你可以使用 text 或 href 找到它,然后检查它的父<li>标签是否真的有 class 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")
于 2013-07-11T07:55:26.500 回答