WebDriver - 添加失败截图
WebDriver - 添加失败截图
作者: Max.Bai
时间: 2015/01
WebDriver失败截图可以通过两种方式实现:
1. Use WebdriverEventListener
第一步:创建自己的WebDriverEventListener
创建自己的WebDriverEventListener 重写Onexception 方法, 当webdriver 遇到异常的时候执行截图动作。
import java.io.File; import java.io.IOException; import java.io.FileOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import org.apache.log4j.LogManager; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.internal.Base64Encoder; import org.openqa.selenium.remote.ScreenshotException; //import org.openqa.selenium.support.events.AbstractWebDriverEventListener; import org.openqa.selenium.support.events.WebDriverEventListener; public class CustomWebDriverEventListener implements WebDriverEventListener { @Override public void onException(Throwable paramThrowable, WebDriver paramWebDriver) { // TODO Auto-generated method stub Throwable cause = paramThrowable.getCause(); if (cause instanceof ScreenshotException) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String dateString = formatter.format(new Date()); File of = new File(dateString + "-exception.png"); FileOutputStream out = null; try { out = new FileOutputStream(of); out.write(new Base64Encoder().decode(((ScreenshotException) cause) .getBase64EncodedScreenshot())); } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
当然还有其他好多事件可以重写,比如:beforeClickOn, 当click的时候打印click对象名称,对象的识别路径
事件列表如下:
public abstract void beforeNavigateTo(String paramString, WebDriver paramWebDriver); public abstract void afterNavigateTo(String paramString, WebDriver paramWebDriver); public abstract void beforeNavigateBack(WebDriver paramWebDriver); public abstract void afterNavigateBack(WebDriver paramWebDriver); public abstract void beforeNavigateForward(WebDriver paramWebDriver); public abstract void afterNavigateForward(WebDriver paramWebDriver); public abstract void beforeFindBy(By paramBy, WebElement paramWebElement, WebDriver paramWebDriver); public abstract void afterFindBy(By paramBy, WebElement paramWebElement, WebDriver paramWebDriver); public abstract void beforeClickOn(WebElement paramWebElement, WebDriver paramWebDriver); public abstract void afterClickOn(WebElement paramWebElement, WebDriver paramWebDriver); public abstract void beforeChangeValueOf(WebElement paramWebElement, WebDriver paramWebDriver); public abstract void afterChangeValueOf(WebElement paramWebElement, WebDriver paramWebDriver); public abstract void beforeScript(String paramString, WebDriver paramWebDriver); public abstract void afterScript(String paramString, WebDriver paramWebDriver); public abstract void onException(Throwable paramThrowable, WebDriver paramWebDriver);
第二步:通过EventFiringWebDriver给webdriver注册自己的listener
WebDriver driver = new RemoteWebDriver(new URL(remoteUrl), capabilities); WebDriverEventListener eventListener = new CustomWebDriverEventListener(); driver = new EventFiringWebDriver(driver).register(eventListener);
注册完就OK了,driver遇到异常的时候就执行异常监控中的截图操作,当然其他事件中的操作也会执行。
2. Use TestNG/JUnit listener
第一步:创建自己的TestListenerAdapter 或者custom rule
TestNG就是创建TestListenerAdapter,重写Onfailure方法, 如果用的是Junit就创建对应的test rule,此处只举例TestNG,Junit的可参照(http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2012/0824/205449_2.html)
Onfailure方法添加截图功能
import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.apache.log4j.LogManager; import org.openqa.selenium.WebDriver; import org.testng.IAnnotationTransformer; import org.testng.IResultMap; import org.testng.IRetryAnalyzer; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.ITestResult; import org.testng.Reporter; import org.testng.TestListenerAdapter; import org.testng.annotations.ITestAnnotation; public class MyListener extends TestListenerAdapter { @Override public synchronized void onTestFailure(ITestResult result) { LogManager.getLogger(this.getClass()).info("In failed method"); Object currentClass = result.getInstance(); ((TestBase) currentClass).takeScreenShot(true); LogManager.getLogger(this.getClass()).info("Out failed method"); } }
第二步:添加自己的listener到testng的xml文件中
<listeners> <listener class-name="test.MyListener" /> </listeners>
两者比较:
WebdriverEventListener:
1. 可以截图所有webdriver异常的图片,但是不能截取Assert.fail 失败的图片
2. 如果使用while等待某种条件是的报错也会截图,可能造成截图过多
3. 只能用于Webdriver,如果使用remote driver可能不行
4. 坚挺的事件很多,方便很多操作
测试框架TestNG,Junit
1. 用例失败就截图,和Webdriver异常关联不大
时间: 2024-10-08 06:06:14