Webdriver-Add failed
Webdriver-Add failed
Author: Max.bai
Time: 2015/01
Webdriver failures can be achieved in two ways:
1. Use Webdrivereventlistener First step: Create your own Webdrivereventlistener
Create your own Webdrivereventlistener override Onexception method to perform the action when the Webdriver encounters an exception.
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 {@Overridepublic void onexception (Throwable paramthrowable, Webdriver paramwebdriver) {//TODO Auto-generated method Stubthrowable 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 ();}}}}}
Of course, there are many other events that can be rewritten, such as: Beforeclickon, when the Click Object name is printed, the object's recognition path
The list of events is as follows:
public abstract void Beforenavigateto (String paramstring, Webdriver paramwebdriver);p ublic abstract void Afternavigateto (String paramstring, Webdriver paramwebdriver);p ublic abstract void Beforenavigateback (Webdriver Paramwebdriver);p ublic abstract void Afternavigateback (Webdriver paramwebdriver);p ublic abstract void Beforenavigateforward (Webdriver paramwebdriver);p ublic abstract void Afternavigateforward (Webdriver paramWebDriver) ;p ublic abstract void Beforefindby (by Paramby, webelement paramwebelement,webdriver paramwebdriver);p ublic Abstract void Afterfindby (by Paramby, webelement paramwebelement,webdriver paramwebdriver);p ublic abstract void Beforeclickon ( Webelement paramwebelement, Webdriver paramwebdriver);p ublic abstract void Afterclickon (Webelement paramWebElement, Webdriver paramwebdriver);p ublic abstract void beforechangevalueof (Webelement paramwebelement, Webdriver Paramwebdriver);p ublic abstract void afterchangevalueof (Webelement paramwebelement, webdriver paramwebdriver);p uBlic abstract void Beforescript (String paramstring, Webdriver paramwebdriver);p ublic abstract void Afterscript (string Paramstring, Webdriver paramwebdriver);p ublic abstract void onexception (Throwable paramthrowable, Webdriver Paramwebdriver);
The second step: through Eventfiringwebdriver to webdriver register their own listener
Webdriver Driver = new Remotewebdriver (new URL (Remoteurl), capabilities); Webdrivereventlistener EventListener = new Customwebdrivereventlistener ();d river = new Eventfiringwebdriver (driver). Register (EventListener);
After the registration is OK, driver encountered an exception to perform the operation of abnormal monitoring, of course, the operation of other events will also be performed.
2. Use Testng/junit Listener First step: Create your own testlisteneradapter or custom rule
TestNG is to create a testlisteneradapter, overriding the OnFailure method, if you use JUnit to create the corresponding test rule, here is only a reference to Testng,junit (HTTP/ www.ltesting.net/ceshi/open/kygncsgj/selenium/2012/0824/205449_2.html)
OnFailure method Add function
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.G Etlogger (This.getclass ()). Info ("in Failed Method"); Object Currentclass = Result.getinstance (); ((testbase) currentclass). Takescreenshot (True); Logmanager.getlogger (This.getclass ()). Info ("Out failed method"); }}
Step Two: Add your own listener to the testng XML file
<listeners><listener class-name= "test. MyListener "/> </listeners>
Compare the two:
Webdrivereventlistener:
1. Can all webdriver abnormal picture, but cannot intercept assert.fail failed picture
2. If you use while to wait for a certain condition, an error can result in too much
3. Can only be used for webdriver, if using remote driver may not
4. A lot of strong events, convenient for many operations
Test Framework Testng,junit
1. The use case fails, and the webdriver exception is not very relevant
Webdriver-Failure