分析:https://github.com/页面Li

 <div class="header header-logged-out">
  <div class="container clearfix">

      <a class="header-logo-wordmark" href="https://github.com/">Github</a>

      <ul class="top-nav">
          <li class="explore"><a href="https://github.com/explore">Explore GitHub</a></li>
        <li class="search"><a href="https://github.com/search">Search</a></li>
        <li class="features"><a href="https://github.com/features">Features</a></li>
          <li class="blog"><a href="https://github.com/blog">Blog</a></li>
      </ul>

    <div class="header-actions">
        <a class="button primary" href="https://github.com/signup">Sign up for free</a>
      <a class="button" href="https://github.com/login">Sign in</a>
    </div>

  </div>

 

 WebElement element1 = webdriver.findElement(By.id("header")); 
 WebElement element2 = webdriver.findElement(By.name("name")); 
 WebElement element3 = webdriver.findElement(By.tagName("a")); 
 WebElement element4 = webdriver.findElement(By.xpath("//a[@title='logo']")); 
 WebElement element5 = webdriver.findElement(By.cssSelector(".feautures")); 
 WebElement element6 = webdriver.findElement(By.linkText("Blog")); 
 WebElement element7 = webdriver.findElement(By.partialLinkText("Ruby")); 
 WebElement element8 = webdriver.findElement(By.className("login"));
List<WebElement> webElements = webdriver.findElements(By 
            .xpath("//ul[@class='nav logged_out']/li"));

13使用 CSS 选择器 (By.cssSelector()) 来检索LI标记。

List<WebElement> webElements = webdriver.findElements(By 
            .cssSelector("ul.nav li"));

可在所检索的项数量上生成断言,如 清单 14所示。

 

Assert.assertEquals(5, webElements.size());
前面的步骤验证了LI标记数量等于 5。

下一步是检索每个LI标记中的每个锚点(A标记)。

展示了如何在第一个LI中获取锚点。此用例使用了 tagName (By.tagName()) 策略。

WebElement anchor1 = webElements.get(0).findElement(By.tagName("a"));

您可以使用类似的方法收集到所有的 5 个锚点,如 清单 16所示。

WebElement anchor1 = webElements.get(0).findElement(By.tagName("a")); 
 WebElement anchor2 = webElements.get(1).findElement(By.tagName("a")); 
 WebElement anchor3 = webElements.get(2).findElement(By.tagName("a")); 
 WebElement anchor4 = webElements.get(3).findElement(By.tagName("a")); 
 WebElement anchor5 = webElements.get(4).findElement(By.tagName("a"));

在这一阶段,您可以验证,锚点内的文本是否与所期望的字符串一致。要检索标记内的文本,WebDriver 提供了getText()方法。清单 展示了完整的测试方法,以及测试底部的断言。

Assert.assertEquals("Signup and Pricing", anchor1.getText()); A

ssert.assertEquals("Explore GitHub", anchor2.getText());

Assert.assertEquals("Features", anchor3.getText());

Assert.assertEquals("Blog", anchor4.getText());

Assert.assertEquals("Login", anchor5.getText());

posted on 2015-04-09 13:24  郭文华  阅读(2251)  评论(0编辑  收藏  举报