查看页面元素的属性

  测试网址

  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();
    }
  }

WebDriverAPI(7)的更多相关文章

  1. WebDriverAPI(10)

    操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...

  2. WebDriverAPI(9)

    操作JavaScript的Alert窗口 测试网址代码 <html> <head> <title>你喜欢的水果</title> </head> ...

  3. WebDriverAPI(4)

    单击某个元素 采用元素id.click()方法即可 双击某个元素id.doubleClick 操作单选下拉列表 测试网页HTML代码 <html> <body> <sel ...

  4. WebDriverAPI(2)

    操作浏览器窗口 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com" ...

  5. WebDriverAPI(8)

    判断页面元素是否存在 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void testIsElementPresent(){ driver. ...

  6. WebDriverAPI(6)

    在指定元素上方进行鼠标悬浮 测试网址 http://www.baidu.com Java语言版本实例 @Test public void roverOnElement() { driver.manag ...

  7. WebDriverAPI(5)

    将当前浏览器截屏 测试网址 http://www.baidu.com Java语言版本实例 @Test public void captureScreenInCurrentWindows() { dr ...

  8. WebDriverAPI(3)

    获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...

  9. WebDriverAPI(1)

    访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...

随机推荐

  1. 2018.07.31cogs2964. 数列操作η(线段树)

    传送门 线段树基本操作. 给出一个排列b,有一个初始值都为0的数组a,维护区间加1,区间统计区间∑(ai/bi)" role="presentation" style=& ...

  2. yii2 内置事件

    1.yii2系统登录   const EVENT_BEFORE_LOGIN = 'beforeLogin';  //登录前    const EVENT_AFTER_LOGIN = 'afterLog ...

  3. hdu -1114(完全背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 思路:求出存钱罐装全部装满情况下硬币的最小数量,即求出硬币的最小价值.转换为最小背包的问题. # ...

  4. c语言学生信息管理系统-学习结构体

    #include<stdio.h> #include<stdlib.h> //结构体可以存放的学生信息最大个数,不可变变量 ; //学生信息结构体数组,最多可以存放100个学生 ...

  5. 进度条ProgressBar

    在本节中,作者只写出了进度条的各种样式,包括圆形.条形,还有自定义的条形,我想如果能让条形进度条走满后再继续从零开始,于是我加入了一个条件语句.作者的代码中需要学习的是handler在主线程和子线程中 ...

  6. PRId64的正确用法

    #include <inttypes.h> #include <stdint.h> #include <stdio.h> // g++ -g -o x x.cpp ...

  7. x+y+z=n的正整数解

    题:x+y+z=n,其中(n>=3),求x,y,z的正整数解的个数根据图象法:x>=1,y>=1,x+y<=n-1

  8. 20170908工作日记--Volley源码详解

    Volley没有jar包,需要从官网上下载源码自己编译出来,或者做成相关moudle引入项目中.我们先从最简单的使用方法入手进行分析: //创建一个网络请求队列 RequestQueue reques ...

  9. (转载)从Java角度理解Angular之入门篇:npm, yarn, Angular CLI

    本系列从Java程序员的角度,带大家理解前端Angular框架. 本文是入门篇.笔者认为亲自动手写代码做实验,是最有效最扎实的学习途径,而搭建开发环境是学习一门新技术最需要先学会的技能,是入门的前提. ...

  10. hdu2602 Bone Collector(01背包) 2016-05-24 15:37 57人阅读 评论(0) 收藏

    Bone Collector Problem Description Many years ago , in Teddy's hometown there was a man who was call ...