WebDriverAPI(7)

 

  查看页面元素的属性

  测试网址

  http://www.baidu.com

  Java语言版本API实例 

  @Test
  public void getWebElementAttribute() {
    driver.manage().window().maximize();
    driver.navigate().to(url);
    String inptString = "seleium测试内容";
    WebElement input = driver.findElement(By.id("kw"));
    input.sendKeys(inptString);
    //取输入框中的值
    String inputText = input.getAttribute("value");
    Assert.assertEquals(inputText, "seleium测试内容");
  }

  获取页面元素的Css值

  测试网址

  http://www.baidu.com

  Java语言版本API实例 

  @Test
  public void getElenmentCssValue() {
    driver.manage().window().maximize();
    driver.get(url);
    WebElement input = driver.findElement(By.id("kw"));
    //获取元素宽度
    String inputwidth = input.getCssValue("width");
    //断言判断
    Assert.assertEquals("500px", inputwidth);
  }

  隐式等待

  测试网址

  http://www.baidu.com

  Java语言版本API实例 

  @Test
  public void testImplictWait() {
    driver.manage().window().maximize();
    driver.navigate().to(url);
    /*使用implicitlyWait方法设定查找页面元素的等待时间,调用findElement方法时没有立刻找到
    * 定位元素会等待设定的等待时长10秒,如果还没找到则抛出NoSuchElementException
    * */
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    try {
      WebElement seartchInputBox = driver.findElement(By.id("kw"));
      WebElement seartchButton = driver.findElement(By.id("su"));
      seartchInputBox.sendKeys("找到搜索框元素");
      seartchButton.click();
    } catch (NoSuchElementException e) {
      Assert.fail("没有找到搜索框");
      e.printStackTrace();
    }
  }

  显示等待

       

  

等待条件 WebDriver方法
页面元素是否在在页面上可用(enabled)和可被单击 elementToBeClickable(By locator)
页面元素处于被选中状态 elementToBeSelected(WebElement element)
页面元素在页面中存在 presenceOfElementLocated(By locator)
在页面元素中是否包含特定的文本 textToBePresentInElement(By locator)
页面元素值 textToBePresentInElementValue(By locator,java.lang.String text)
标题(title) titleContains(java.lang.String title)

  测试HTML代码

  <html>
    <title>你喜欢的水果</title>
    <body>
      <p>请选择你爱吃的水果</p>
      <br>
      <select name='fruit'>
        <option id='peach' value='taozi'>桃子</option>
        <option id='watermelon' value='xigua'>西瓜</option>
      </select>
      <br>
      <input type='checkbox'>是否喜欢吃水果?</input>
      <br><br>
      <input type="text" id="text" value="今年夏天西瓜相当甜">文本框</input>
    </body>
  </html>

  Java语言版本API实例 

  @Test
  public void testExplicitWait() {
    driver.manage().window().maximize();
    driver.get(url);
    //声明一个WebDriverWait对象,设定触发条件的最长等待时间为10秒
    WebDriverWait wait = new WebDriverWait(driver,10);
    //调用ExpectedConditions的titleContains方法判断标题中是否包含水果两字
    wait.until(ExpectedConditions.titleContains("水果"));
    System.out.println("网页标题出现了“水果的关键字”");
    //获取桃子选项对象
    WebElement select = driver.findElement(By.xpath("//option[@id='peach']"));
    //调用ExpectedConditions的elementToBeSelected方法判断桃子是否处于选中状态
    wait.until(ExpectedConditions.elementToBeSelected(select));
    System.out.println("下拉列表框“桃子属于选中状态”");
    //调用ExpectedConditions的elementToBeClickable方法判断复选框是否处于可见及是否可被单击
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='checkbox']")));
    System.out.println("页面复选框处于显示和可被单击状态");
    //调用ExpectedConditions的presenceOfElementLocated方法判断p是否存在页面中
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//p")));
    System.out.println("页面的p元素标签已显示");
    //获取页面p标签元素
    WebElement p = driver.findElement(By.xpath("//p"));
    //调用ExpectedConditions的textToBePresentInElement方法判断p标签中是否包含爱吃的水果这几个字
    wait.until(ExpectedConditions.textToBePresentInElement(p, "爱吃的水果"));
    System.out.println("页面p标签元素包含爱吃的水果");
  }

  自定义的显示等待

  被测试HTML代码

  同上一个

  Java语言版本API实例 

  @Test
  public void testExplicitWait() {
    driver.manage().window().maximize();
    driver.navigate().to(url);
    try {
      //显示等待判断是否可以从页面获取文字输入框对象,如果可以获取则执行后面测试用例
      WebElement textInputBox = (new WebDriverWait(driver,10)).until(new ExpectedCondition<WebElement>() {
      @Override
      public WebElement apply(WebDriver d){
        return d.findElement(By.xpath("//*[@type='text']"));
      }
     });
    //断言判断输入框中是否包含这几个字
    Assert.assertEquals("今年夏天西瓜相当甜!", textInputBox.getAttribute("value"));
    //显示等待判断p标签中是否包含爱吃两个字,若包含则继续执行后面的测试同理
    Boolean containTextFlag = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver d){
      return d.findElement(By.xpath("//p")).getText().contains("爱吃");
      }
    });
    //断言判断是否包含爱吃关键字
    Assert.assertTrue(containTextFlag);
    //显示等待判断文本框是否可见,若可见继续执行后面的测试用例
    Boolean inputTextVisibleFlag = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver d){
      return d.findElement(By.xpath("//*[@type='text']")).isDisplayed();
      }
    });
    //断言判断文本框是否可见
    Assert.assertTrue(inputTextVisibleFlag);
    } catch (NoSuchElementException e) {
      //若显示等待条件未被满足则执行
      Assert.fail("页面上的输入框元素未未找到!");
      e.printStackTrace();
    }
  }

posted @ 2019-03-01 09:22  心生意动  阅读(128)  评论(0编辑  收藏  举报