TA的每日心情 | 无聊 2024-11-5 10:03 |
---|
签到天数: 77 天 连续签到: 1 天 [LV.6]测试旅长
|
testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listene
r的onTestFailure方法
那么首先新建一个Listener 类,继承TestListenerAdapter
复制代码
- <p>package com.dbyl.libarary.utils;</p><p>
- </p><p>import org.openqa.selenium.WebDriver;</p><p>import org.testng.ITestContext;</p><p>import org.testng.ITestResult;</p><p>import org.testng.TestListenerAdapter;</p><p>
- </p><p>/**</p><p> * </p><p> * @author Young</p><p> *</p><p> */</p><p>public class TestNGListener extends TestListenerAdapter {</p><p> Log log = new Log(this.getClass());</p><p>
- </p><p> @Override</p><p> public void onTestSuccess(ITestResult tr) {</p><p> log.info("Test Success");</p><p> super.onTestSuccess(tr);</p><p> }</p><p>
- </p><p> @Override</p><p> public void onTestFailure(ITestResult tr) {</p><p> log.error("Test Failure");</p><p> super.onTestFailure(tr);</p><p> takeScreenShot(tr);</p><p> }</p><p>
- </p><p> private void takeScreenShot(ITestResult tr) {</p><p> UITest b = (UITest) tr.getInstance();</p><p> WebDriver currentDirver = b.getDriver();</p><p> System.out.println(currentDirver.getTitle());</p><p> b.takeScreenShot();</p><p>
- </p><p> }</p><p>
- </p><p> @Override</p><p> public void onTestSkipped(ITestResult tr) {</p><p> log.error("Test Skipped");</p><p> super.onTestSkipped(tr);</p><p> }</p><p>
- </p><p> @Override</p><p> public void onTestStart(ITestResult result) {</p><p> log.info("Test Finsh");</p><p> super.onTestStart(result);</p><p> }</p><p>
- </p><p> @Override</p><p> public void onStart(ITestContext testContext) {</p><p> log.info("Test Start");</p><p> super.onStart(testContext);</p><p> }</p><p>
- </p><p> @Override</p><p> public void onFinish(ITestContext testContext) {</p><p> log.info("Test Finish");</p><p> super.onFinish(testContext);</p><p> }</p><p>
- </p><p>}</p><p>复制代码</p><p>我这里主要重写OnTestFailure的方法</p><p>
- </p><p>添加了一个takeScreenShot的方法</p><p>
- </p><p>接下来在UITest类中添加截图的具体实现方法</p><p>
- </p><p>复制代码</p><p>/**</p><p> * </p><p> */</p><p>package com.dbyl.libarary.utils;</p><p>
- </p><p>import java.io.File;</p><p>import java.text.SimpleDateFormat;</p><p>import java.util.Calendar;</p><p>import java.util.Date;</p><p>
- </p><p>import org.apache.commons.io.FileUtils;</p><p>import org.openqa.selenium.OutputType;</p><p>import org.openqa.selenium.TakesScreenshot;</p><p>import org.openqa.selenium.WebDriver;</p><p>
- </p><p>/**</p><p> * @author Young</p><p> *</p><p> */</p><p>public class UITest {</p><p> WebDriver driver;</p><p> Log log = new Log(this.getClass());</p><p>
- </p><p> public WebDriver getDriver() {</p><p> return driver;</p><p> }</p><p>
- </p><p> /**</p><p> * init test case</p><p> * </p><p> * @param driver</p><p> */</p><p> public void setDriver(WebDriver driver) {</p><p> this.driver = driver;</p><p> }</p><p>
- </p><p> public void init(WebDriver driver) {</p><p> log.info("Start WebDriver");</p><p> setDriver(driver);</p><p> }</p><p>
- </p><p> /**</p><p> * stop webdriver</p><p> * </p><p> * @param driver</p><p> */</p><p> public void stop() {</p><p> log.info("Stop WebDriver");</p><p> driver.quit();</p><p>
- </p><p> }</p><p>
- </p><p> /**</p><p> * @author Young</p><p> */</p><p> public void takeScreenShot() {</p><p> SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");</p><p> Calendar cal = Calendar.getInstance();</p><p> Date date = cal.getTime();</p><p> String dateStr = sf.format(date);</p><p> String path = this.getClass().getSimpleName() + "_" + dateStr + ".png";</p><p> takeScreenShot((TakesScreenshot) this.getDriver(), path);</p><p> }</p><p>
- </p><p> /**</p><p> * @author Young</p><p> * @param drivername</p><p> * @param path</p><p> */</p><p> public void takeScreenShot(TakesScreenshot drivername, String path) {</p><p> // this method will take screen shot ,require two parameters ,one is</p><p> // driver name, another is file name</p><p> String currentPath = System.getProperty("user.dir"); // get current work</p><p> log.info(currentPath);</p><p> File scrFile = drivername.getScreenshotAs(OutputType.FILE);</p><p> // Now you can do whatever you need to do with it, for example copy</p><p> try {</p><p> log.info("save snapshot path is:" + currentPath + path);</p><p> FileUtils.copyFile(scrFile, new File(currentPath + "\\" + path));</p><p> } catch (Exception e) {</p><p> log.error("Can't save screenshot");</p><p> e.printStackTrace();</p><p> } finally {</p><p> log.info("screen shot finished");</p><p> }</p><p> }</p><p>
- </p><p>}</p>
复制代码
复制代码
接下来在case中使用这个Listener,有两种办法, 第一种直接在case类中添加注解@Listeners({ TestN
GListener.class })
case代码:
View Code
第二种方法是在eclipse run config 添加如下参数-listener com.dbyl.libarary.utils.TestNGListener
这样就能实现case失败自动截图
这样,这个框架能够实现一些基本操作,下一步还需要实现失败重试 ,配合虚拟机
|
|