• Selenium常用API的使用java语言之10-获取断言信息


    不管是在做功能测试还是自动化测试,最后一步需要拿实际结果与预期进行比较。这个比较的称之为断言。
    我们通常可以通过获取title 、URL和text等信息进行断言。text方法在前面已经讲过,它用于获取标签对之间的文本信息。

    • getTitle(): 用于获得当前页面的title。
    • getCurrentUrl() : 用户获得当前页面的URL。
    • getText() 获取页面文本信息。

    下面同样以百度为例,介绍如何获取这些信息。

    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
     
     
    public class AssertDemo {
     
      public static void main(String[] args) throws InterruptedException {
     
          WebDriver driver = new ChromeDriver();
          driver.get("https://www.baidu.com");
     
          System.out.println("Search before================");
     
          //获取当前的 title 和 url
          System.out.printf("title of current page is %s
    ", driver.getTitle());
          System.out.printf("url of current page is %s
    ", driver.getCurrentUrl());
     
          //百度搜索
          WebElement search = driver.findElement(By.id("kw"));
          search.sendKeys("Selenium");
          search.sendKeys(Keys.ENTER);
          Thread.sleep(2000);
     
          System.out.println("Search after================");
     
          //获取当前的 title 和 url
          System.out.printf("title of current page is %s
    ", driver.getTitle());
          System.out.printf("url of current page is %s
    ", driver.getCurrentUrl());
     
          //获取第一条搜索结果的标题
          WebElement result = driver.findElement(By.xpath("//div[@id='content_left']/div/h3/a"));
          System.out.println(result.getText());
     
          driver.quit();
      }
    }
    

    打印结果:

    Search before================
    title of current page is 百度一下, 你就知道
    url of current page is https://www.baidu.com/
     
    Search after================
    title of current page is Selenium_百度搜索
    url of current page is
    https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=Selenium&rsv_pq=9be
    4680700a485c1&rsv_t=e925U%2F%2B9SBTqmRI%2BuARg0%2BTCzrrZWn4jOBJkb1OS2vUjMrZsq5VblQ7toD8
    &rqlang=cn&rsv_enter=1&rsv_sug3=8&rsv_sug2=0&inputT=155&rsv_sug4=155
    Selenium - Web Browser Automation
    
  • 相关阅读:
    scala IDE for Eclipse开发Spark程序
    RedHat6.5安装Spark单机
    RedHat6.5安装Spark集群
    Spark任务提交jar包依赖解决方案
    本地开发spark代码上传spark集群服务并运行
    Maven3.5.0安装与配置+Eclipse应用
    MAVEN Scope使用
    Maven下载项目依赖jar包和使用方法
    maven中把依赖的JAR包一起打包
    Maven+Eclipse+SparkStreaming+Kafka整合
  • 原文地址:https://www.cnblogs.com/zhizhao/p/11303202.html
Copyright © 2020-2023  润新知