zoukankan      html  css  js  c++  java
  • selenium 利用testNG对异常进行自动截图

      哈哈哈,很久没写博客了,懒了。

      因为一些原因最近需要把监听事件重新整理一下,开始没细想,直接copy网上的,其实结果发现报错很多,或者是达不到效果,然后把之前的代码翻出来,仔细看了一下。下面给一些需要的小伙伴整理一下思路:

      1、首先我们用到的是testng里面的监听,所以这个毋庸置疑

      2、我们需要重新他的监听事件

      3、用例中肯定需要加入监听事件

    这三点是网上公认的,但是怎么做确没有说明白。

      首先看一下我的目录结构:

    图片中我有4个类,

    1、driverBase是一个基类,里面是driver的初始化,截图的方法也在里面。

    2、selectDriver是根据浏览器不同返回不同的driver

    3、testngListenerScreen是testng的监听类,他继承了TestListenerAdapter类,把里面的方法进行了重写。主要他会去监听你测试类是否报错,如果报错他就会去调用我们driverbase下面的截图方法。

    4、testlogin 不用说,我们的测试类,他需要继承我们的driverBase基类,去拿里面的driver。

    这个说完了先看代码,首先从我们基类开始:

    package com.mushishi.selenium.base;
    
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    
    /**
     * @author mushishi
     * */
    public class driverBase {
    	public WebDriver driver;
    	
    	/**
    	 * 获取driver
    	 * */
    	public WebDriver getDriver() {
            return driver;
        }
        
    	/**
    	 * 自动截图
    	 * */
        public void takeScreenShot() {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            String dateStr = sf.format(date);
        //上面几行代码的意思都是获取时间,并且格式化,用来作为图片的名称
        //下面这个是获取当前运行的类名称和时间的组合一起命名图片 String path = this.getClass().getSimpleName() + "_" + dateStr + ".png";
        //因为我们截图是需要用到driver的,所以这里需要获取driver,这个driver是获取的当前对象的driver takeScreenShot((TakesScreenshot) this.getDriver(), path); } /** * 传入参数截图 * */ public void takeScreenShot(TakesScreenshot drivername, String path) { String currentPath = System.getProperty("user.dir"); // get current work File scrFile = drivername.getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(scrFile, new File(currentPath + "\" + path)); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("截图成功"); } } }

      上面的代码,可能有那么一两个地方看不懂,都没关系,我写备注了。主要是以类名字和时间组合起来命名,并且把当前运行的对象的driver获取到,用来截图操作。

    下面看selectDriver的类:

    package com.mushishi.selenium.base;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class SelectDriver {
    	public WebDriver driverName(String browser){
    		if(browser.equalsIgnoreCase("fireFox")){
                  //设置环境变量,并且返回driver System.setProperty("webdriver.firefox.marionette","D:\java\geckodriver\geckodriver-v0.14.0-win64\geckodriver.exe"); return new FirefoxDriver(); }else{ System.setProperty("webdriver.chrome.driver", "D:\java\chromedriver_win32\chromedriver.exe"); return new ChromeDriver(); } } }

      上面的没什么可看的,就是根据传入的对象生成一个driver,这个我用的3.0的selenium,所以需要这么设置。这个根据自己情况。

    下面看监听类的: 1 package com.mushishi.selenium.base;

     2 
     3 import org.testng.ITestContext;
     4 import org.testng.ITestResult;
     5 import org.testng.TestListenerAdapter;
     6 
     7 public class TestNGListenerScreen extends TestListenerAdapter{
     8 
     9     @Override
    10     public void onTestSuccess(ITestResult tr) {
    11         super.onTestSuccess(tr);
    12     }
    13 
        //主要是用到这个方法了,当你报错时他会监听到,然后就会执行截图操作,这里的 ITestresult tr是testng里的,获取到的是当前运行对象,你这样理解,他就是
    //我当前运行的类这个对象 14 @Override 15 public void onTestFailure(ITestResult tr) { 16 super.onTestFailure(tr); 17 System.out.println(tr); 18 takeScreenShot(tr); 19 }
    20 //所以这里我执行截图的时候就获取到了我运行过程中实力对象的driver了,懂了吧。所以当我再回去调用我基类的对象时,那么driver就是同一个了 21 private void takeScreenShot(ITestResult tr) { 22 driverBase b = (driverBase) tr.getInstance(); 23 // driver = b.driver; 24 b.takeScreenShot(); 25 26 } 27 28 @Override 29 public void onTestSkipped(ITestResult tr) { 30 super.onTestSkipped(tr); 31 } 32 33 @Override 34 public void onTestStart(ITestResult result) { 35 super.onTestStart(result); 36 } 37 38 @Override 39 public void onStart(ITestContext testContext) { 40 super.onStart(testContext); 41 } 42 43 @Override 44 public void onFinish(ITestContext testContext) { 45 super.onFinish(testContext); 46 } 47 }

    最后一个类,测试类:

     1 package com.mushishi.selenium.testCase;
     2 
     3 import org.openqa.selenium.By;
     4 import org.openqa.selenium.WebElement;
     5 import org.openqa.selenium.interactions.Actions;
     6 import org.testng.annotations.Listeners;
     7 import org.testng.annotations.Test;
     8 
     9 import com.mushishi.selenium.ProUtil;
    10 import com.mushishi.selenium.base.SelectDriver;
    11 import com.mushishi.selenium.base.TestNGListenerScreen;
    12 import com.mushishi.selenium.base.driverBase;
    13 
    14 @Listeners({ TestNGListenerScreen.class })
    15 public class testLogin extends driverBase {
    16     SelectDriver select = new SelectDriver();
    17     public testLogin(){
    18         if(driver == null){
    19         driver = select.driverName("chrome");
    20         }
    21     }
    22     public void loginScript(String username,String userpass) throws Exception{
    23         String url = "http://www.imooc.com";
    24         driver.get(url);
    25         driver.manage().window().maximize();
    26         driver.findElement(By.id("js-signin-btn")).click();
    27         Thread.sleep(2000);
    28         WebElement user = this.element(this.byStr("username"));
    29         user.isDisplayed();
    30         WebElement password = this.element(this.byStr("userpass"));
    31         password.isDisplayed();
    32         WebElement loginButton = this.element(this.byStr("loginbutton"));
    33         loginButton.isDisplayed();
    34         user.sendKeys(username);
    35         password.sendKeys(userpass);
    36         loginButton.click();
    37         Thread.sleep(3000);
    38         WebElement header = this.element(this.byStr("header"));
    39         header.isDisplayed();
    40         Actions actions = new Actions(driver);
    41         actions.moveToElement(header).perform();
    42         String userInfo = this.element(this.byStr("nameInfo")).getText();
    43         System.out.println(userInfo);
    44         if(userInfo.equals("xxxx")){
    45             System.out.println("登陆成功");
    46         }else{
    47             System.out.println("登陆失败");
    48             
    49         }
    50         driver.close();
    51     }
    52     
    53     /**
    54      * 封装By by
    55      * */
    56     public By byStr(String username){
    57         ProUtil properties = new ProUtil("element.properties");
    58         String locator = properties.getPro(username);
    59         String locatorType = locator.split(">")[0];
    60         String locatorValue = locator.split(">")[1];
    61         if(locatorType.equals("id")){
    62             return By.id(locatorValue);
    63         }else if(locatorType.equals("name")){
    64             return By.name(locatorValue);
    65         }else if(locatorType.equals("className")){
    66             return By.className(locatorValue);
    67         }else{
    68             return By.xpath(locatorValue);
    69         }
    70     }
    71     
    72     /**
    73      * 封装Element
    74      * */
    75     public WebElement element(By by){
    76         WebElement ele = driver.findElement(by);
    77         return ele;
    78     }
    79     
    80     @Test
    81     public void loginpage() throws Exception{
    82         this.loginScript("xxxx", "xxxx");
    83     }
    84 }

    这个是测试类,不知道你懂了么?因为我很多都封装了的,所以看不到,但是不影响大家理解testng自动截图思想。动手操作吧。

      

  • 相关阅读:
    ckeditor详解
    c#实现生产者消费者模式
    逻辑思维题01
    关于nginx的安装
    一些关于python的小感想
    关于linux上pdf阅读器
    将python2.7+django1.10部署到SAE上
    配置github上的SSH key及上传自己的项目到github
    解决ubuntu15 下没有声音
    linux小倒腾
  • 原文地址:https://www.cnblogs.com/Mushishi_xu/p/6831474.html
Copyright © 2011-2022 走看看