首页 > 代码库 > EventFiringWebDriver网页事件监听(二)
EventFiringWebDriver网页事件监听(二)
public class SeleniumDemo { /** * @param args */ public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); Navigation navigate = driver.navigate(); EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver); //注册事件 eventDriver.register(new MyWebDriverListener()); //浏览特定网页 eventDriver.navigate().to("http://www.jd.com/"); //点击按钮 WebElement loginLink = eventDriver.findElement(By.partialLinkText("登")); loginLink.click(); //在搜索框输入内容 eventDriver.findElement(By.id("kw")).sendKeys("诛仙剑阵"); //故意写一个错误操作, 使selenium产生异常 try{ eventDriver.findElement(By.id("番天印")); }catch(Exception e){ e.printStackTrace(); } } }
下面是我自己写的监听器 MyWebDriverListener
public class MyWebDriverListener implements WebDriverEventListener{ @Override public void onException(Throwable throwable, WebDriver driver) { SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); String dateString = format.format(new Date()); File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try{ File screenShot = new File("D:\\"+dateString+".png"); FileUtils.copyFile(srcFile, screenShot); }catch(Exception e){ e.printStackTrace(); } System.out.println("发生异变,原因是: "+throwable.getMessage()); System.out.println("截图保存在: "+"D:\\"+dateString+".png"); } @Override public void afterNavigateTo(String url, WebDriver driver) { System.out.println("afterNavigateTo: "+url); System.out.println("afterNavigateTo by driver: "+driver.getCurrentUrl()); } @Override public void beforeClickOn(WebElement element, WebDriver driver) { System.out.println("单机页面元素的属性: "+element.getAttribute("value")); } @Override public void beforeFindBy(By by, WebElement element, WebDriver driver) { System.out.println("查找元素的条件是: "+ by.toString()); } @Override public void afterChangeValueOf(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterClickOn(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterFindBy(By by, WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterNavigateBack(WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterNavigateForward(WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterNavigateRefresh(WebDriver driver) { // TODO Auto-generated method stub } @Override public void afterScript(String url, WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeChangeValueOf(WebElement element, WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeNavigateBack(WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeNavigateForward(WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeNavigateRefresh(WebDriver driver) { // TODO Auto-generated method stub } @Override public void beforeNavigateTo(String url, WebDriver driver) { System.out.println("beforeNavigateTo: "+url); System.out.println("beforeNavigateTo by driver : "+driver.getCurrentUrl()); } @Override public void beforeScript(String url, WebDriver driver) { // TODO Auto-generated method stub } }
以上的监听器可以满足以下需求:
1.执行打开网页的操作室, 需要分别记录打开前和之后的URL地址.
2.在查找某个页面元素时候, 查找之前和之后都需要记录查找条件
3.在对页面进行单机操作室, 单击前需要记录元素的查找条件, 单机后记录URL地址
4.在对页面元素的值进行更改, 需要分别记录更改前的值和更改后的值
5.在发生异常的时候, 需要进行截图, 将截图文件保存在D:\ , 用当前日期命名文件
原文章出处:http://blog.csdn.net/slowsnowscar/article/details/52507288
EventFiringWebDriver网页事件监听(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。