查看页面元素的属性

  测试网址

  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.06.30 BZOJ1026: [SCOI2009]windy数(数位dp)

    1026: [SCOI2009]windy数 Time Limit: 1 Sec Memory Limit: 162 MB Description windy定义了一种windy数.不含前导零且相邻两 ...

  2. Linux服务器部署系列之二—MySQL篇

    MySQL是linux环境中使用最广泛的数据库之一,著名的“LAMP黄金组合”就要用到MySQL.关于MySQL的优点及作用,我就不多讲了,网上很多这样的文章. 今天我们要谈的是MySQL服务器的部署 ...

  3. C#基础:在using中创建对象

    在using中创建的对象的类必须是实现了IDispose接口的类,示例代码如下: static void Main(string[] args) { Method(); Console.WriteLi ...

  4. Eclipse中显示line number

  5. Java实现浏览器端大文件分片上传

    版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  6. 三)mybatis 二级缓存,整合ehcache

    mybatis-config.xml <setting name="cacheEnabled" value="true" /> PersonMapp ...

  7. (连通图 Tarjan)Caocao's Bridges --HDU --4738

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:曹操有很多岛屿,然后呢需要建造一些桥梁将所有的岛屿链接起来,周瑜要做的是就是不让曹操将所 ...

  8. NoSQL: Cassandra, HBase, RocksDB

    转自: http://www.linkedin.com/pulse/nosql-cassandra-hbase-rocksdb-siddharth-anand I've had the pleasur ...

  9. hdu 1348 凸包模板

    http://acm.hdu.edu.cn/showproblem.php?pid=1348 造城墙问题,求出凸包加上一圈圆的周长即可 凸包模板题 #include <cstdio> #i ...

  10. java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)

    编写过程中需要注意的问题: 1.建立eclipse动态web工程时,需要改写编译后class文件的位置,通常情况下是这个位置:/WebContent/WEB-INF/classes 2.配置的页面链接 ...