zoukankan      html  css  js  c++  java
  • selenium java Web页面获取Toast及页面截图

    //Web页面截图

    public class ScreenShot {
      private WebDriver driver;
      public String projectpath=System.getProperty("user.dir");//获取项目地址
      public String scrpath=projectpath+"\screenShot\";

      public ScreenShot(WebDriver driver) {
        this.driver=driver;
      }

      public void getScreenShot(){
        Date currentTime=new Date();//获取系统当前时
        SimpleDateFormat dataFormat=new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");//设置日期格式
        String dataString=dataFormat.format(currentTime);
        System.out.println("当前时间是:"+dataString);
        try{
          File srcFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
          File srcseenshot=new File(scrpath+dataString+".jpg");
          Files.copy(srcFile, srcseenshot);
        }catch(Exception e){
          System.out.println("保存截图失败");
          e.printStackTrace();
        }finally{
          System.out.println("截图已保存至:"+scrpath+dataString+".jpg");
        }
      }
    }

    //获取Toast信息(ScreenShot类和Toast类结合使用)

    public class Toast {
      public ScreenShot sc;
      public WebDriver driver;

      public Toast(WebDriver driver) {
        this.driver=driver;
        sc=new ScreenShot(driver);
      }

      public void getToast(String toastLoc,String expectValue) {
        try {
          WebElement elementText=driver.findElement(By.xpath(toastLoc));
          Thread.sleep(1000);
          tring actualValue = elementText.getText();
          System.out.println(actualValue);
          Assert.assertEquals(actualValue, expectValue);
          System.out.println("Toast提示信息校验成功");
        } catch (Exception e) {
          e.printStackTrace();
          System.out.println("未获取到Toast提示信息,断言失败");
          sc.getScreenShot();//截图
        }
      }
    }

     说明:获取toast之前,最好先判断页面上的元素是否存在,调用显示等待WebDriverWait()方法,结合ExpectedConditions使用,可以更好的提高代码的健壮性;

  • 相关阅读:
    “菜鸟”程序员和“大神”程序员差距在哪里?别告诉我你连菜鸟都不算!
    Android开发:为什么你的学习效率如此低,为什么你很迷茫?
    Android架构师吐槽腾讯王者荣耀的程序员,排位匹配算法怎么搞的,每次都输
    程序员如何回答面试官“请介绍一下自己”这类问题
    Android程序员事件分发机制学习笔记
    面试时,问哪些问题能试出一个 Android 应用开发者真正的水平?
    List、Set、Map的区别
    在Eclipse中使用JUnit4进行单元测试(图文教程一)
    1
    2016、11、17
  • 原文地址:https://www.cnblogs.com/xiule/p/11714665.html
Copyright © 2011-2022 走看看