EventFiringWebDriver网页事件监听

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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网页事件监听的主要内容,如果未能解决你的问题,请参考以下文章

EventFiringWebDriver / WebDriverEventListener 调用顺序

在 PyCharm 中调试时在 EventFiringWebDriver 中触发的异常

jQuery是怎样监听到事件的?

oninput事件(解决onkeyup无法监听到复制黏贴)

js 怎么监听到页面关闭或页面跳转事件

qt QListWidget 添加鼠标移动事件(mouseMoveEvent),让父窗体可以监听到鼠标移动