可以使用EventFiringWebDriver类创建对象,然后使用register(监听类对象)注册监听类。具体使用方法如下:
1 首先创建监听类,创建很简单,只要实现WebDriverEventListener接口即可,然后实现里面的方法
1 public class MyWebDriverListener implements WebDriverEventListener { 2 3 private Log log = new Log(MyWebDriverListener.class); 4 5 @Override 6 public void beforeNavigateTo(String url, WebDriver driver) { 7 // TODO Auto-generated method stub 8 9 } 10 11 @Override 12 public void afterNavigateTo(String url, WebDriver driver) { 13 // TODO Auto-generated method stub 14 log.debug("afterNavigateTo: "+url); 15 log.debug("afterNavigateTo by driver: "+driver.getCurrentUrl()); 16 System.out.println("afterNavigateTo: "+url); 17 System.out.println("afterNavigateTo by driver: "+driver.getCurrentUrl()); 18 19 } 20 21 @Override 22 public void beforeNavigateBack(WebDriver driver) { 23 // TODO Auto-generated method stub 24 25 } 26 27 @Override 28 public void afterNavigateBack(WebDriver driver) { 29 // TODO Auto-generated method stub 30 31 } 32 33 @Override 34 public void beforeNavigateForward(WebDriver driver) { 35 // TODO Auto-generated method stub 36 37 } 38 39 @Override 40 public void afterNavigateForward(WebDriver driver) { 41 // TODO Auto-generated method stub 42 43 } 44 45 @Override 46 public void beforeNavigateRefresh(WebDriver driver) { 47 // TODO Auto-generated method stub 48 49 } 50 51 @Override 52 public void afterNavigateRefresh(WebDriver driver) { 53 // TODO Auto-generated method stub 54 55 } 56 57 @Override 58 public void beforeFindBy(By by, WebElement element, WebDriver driver) { 59 // TODO Auto-generated method stub 60 log.debug("查找元素的条件是: "+ by.toString()); 61 System.out.println("查找元素的条件是: "+ by.toString()); 62 63 } 64 65 @Override 66 public void afterFindBy(By by, WebElement element, WebDriver driver) { 67 // TODO Auto-generated method stub 68 } 69 70 @Override 71 public void beforeClickOn(WebElement element, WebDriver driver) { 72 // TODO Auto-generated method stub 73 System.out.println("单机页面元素的属性: "+element.getAttribute("value")); 74 75 } 76 77 @Override 78 public void afterClickOn(WebElement element, WebDriver driver) { 79 // TODO Auto-generated method stub 80 81 } 82 83 @Override 84 public void beforeChangeValueOf(WebElement element, WebDriver driver) { 85 // TODO Auto-generated method stub 86 87 } 88 89 @Override 90 public void afterChangeValueOf(WebElement element, WebDriver driver) { 91 // TODO Auto-generated method stub 92 93 } 94 95 @Override 96 public void beforeScript(String script, WebDriver driver) { 97 // TODO Auto-generated method stub 98 99 } 100 101 @Override 102 public void afterScript(String script, WebDriver driver) { 103 // TODO Auto-generated method stub 104 105 } 106 107 @Override 108 public void onException(Throwable throwable, WebDriver driver) { 109 // TODO Auto-generated method stub 110 SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); 111 String dateString = format.format(new Date()); 112 113 File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 114 115 try{ 116 File screenShot = new File("D:\"+dateString+".png"); 117 FileUtils.copyFile(srcFile, screenShot); 118 }catch(Exception e){ 119 e.printStackTrace(); 120 } 121 122 log.error("发生异常,原因是: "+throwable.getMessage()); 123 //后期可以替换为log4j 124 System.out.println("发生异常,原因是: "+throwable.getMessage()); 125 126 log.error("截图保存在: "+"D:\"+dateString+".png"); 127 //后期保存路径读取properties配置文件 128 System.out.println("截图保存在: "+"D:\"+dateString+".png"); 129 } 130 131 }
2 初始化EventFiringWebDriver对象
ChromeDriver chromedriver = new ChromeDriver();
EventFiringWebDriver eventdriver = new EventFiringWebDriver(chromedriver);
//为eventdriver添加监听类MyWebDriverListener
eventdriver.register(new MyWebDriverListener());
注册后,后续使用chromedriver进行各种操作(监听类中实现的各种方法)后,可进行打印日志等处理,EventFiringWebDriver通过动态代理,在调用webDriver的各种方法的前后添加处理操作。
。