EventFiringWebDriver网页事件监听
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EventFiringWebDriver网页事件监听相关的知识,希望对你有一定的参考价值。
Selenium提供了很多的event listening functions来跟踪脚本执行过程中的events.
How it works?
在注册了listener的webDriver里面,这些listener可以listen events。events可以是任何类型的event 或者 action,比如element click,改变输入框的值,甚至是exceptions。
To enable this feature:
1. 创建用户自定义的Event listener class.
2. 通过实现webdriver的接口,来创建一个EventFiringWebDriverObject
3. And register the Listener to the EventFiringWebDriver instance.
Q: 如何创建自己的event listener class?
A: By 实现WebDriverEventListener 接口(不推荐)。
或者By 继承AbstractWedDriverEventListener 类。(推荐)
首先展示实现WebdriverEventListener 接口的方式(不推荐):
package main.test.org.seleniummonster.com.demo.eventlistenerdemo; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.events.WebDriverEventListener; public class EventListenerType1 implements WebDriverEventListener { @Override public void afterChangeValueOf(WebElement arg0, WebDriver arg1) { // Things to be done after changing the value in the webelement argo } @Override public void afterClickOn(WebElement arg0, WebDriver arg1) { // Things to be done after clicking an element arg0 } @Override public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) { // Things to be done after findby of webelement arg1 } @Override public void afterNavigateBack(WebDriver arg0) { // Things to be done after navigating back } @Override public void afterNavigateForward(WebDriver arg0) { // Things to be done after navigating forward } @Override public void afterNavigateTo(String arg0, WebDriver arg1) { // Things to be done after navigating to the given URL say, arg0 } @Override public void afterScript(String arg0, WebDriver arg1) { // Things to be done after script execution } @Override public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) { // Things to be done before Changing value in the webelement arg0 } @Override public void beforeClickOn(WebElement arg0, WebDriver arg1) { // Things to be done before Clicking an element arg0 } @Override public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) { // Things to be done before a Findby of an web element arg1 } @Override public void beforeNavigateBack(WebDriver arg0) { // Things to be done before navigating back } @Override public void beforeNavigateForward(WebDriver arg0) { // Things to be done before Navigating forward } @Override public void beforeNavigateTo(String arg0, WebDriver arg1) { // Things to be done before navigating to a url arg0 } @Override publicvoid beforeScript(String arg0, WebDriver arg1) { // Things to be done before javascript execution } @Override public void onException(Throwable arg0, WebDriver arg1) { System.out.println("There is an exception in the script, please find the below error description" ); arg0.printStackTrace(); } }
在这种方法里,我们只重写了onException() ,其与方法都没做改动,但还是override了一遍。没办法啊。
然后就要介绍推荐的那种方法了。AbstractWebDriverEventListener 是一个抽象类,它实现了WebDriverEventListener接口,它override了WebDriverEventListener所有的方法(当然只是空的override了一下)。我们可以继承AbstractWebDriverEventListener class 来具体定义我们关注的方法,看如下例子:EventListenerType2.java
package main.test.org.seleniummonster.com.demo.eventlistenerdemo; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.events.AbstractWebDriverEventListener; public class EventListenerType2 extends AbstractWebDriverEventListener { /** * @author Selenium Monster */ @Override public void onException(Throwable arg0, WebDriver arg1) { System.out.println("There is an exception in the script, please find the below error description" ); arg0.printStackTrace(); } }
代码简短了不少,接下来就是Registering the Listener with Webdriver instance
下面是个实例: 创建个EventFiringWebDriver 类的实例, 我们定义的listener类的实例。用register()方法把我们定义的listener注册到EventFiringWebDriver 对象上。可以注册As many listener as you whish.
* If you have definitions for an action in all the listener, all of them will be executed.*
package main.test.org.seleniummonster.com.demo.eventlistenerdemo; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.events.EventFiringWebDriver; public class EventFiringWebdriverExample { /** * @author Selenium Monster */ public static void main(String[] args) { // 创建一个 webdriver实例 WebDriver driver = new FirefoxDriver(); // 创建EventFiringWebDriver 实例 EventFiringWebDriver eventFiringWD= new EventFiringWebDriver(driver); // Creating instance of eventListener, that we just defined EventListenerType2 eventListener1=new EventListenerType2(); // Register the Listener with the event firing driver eventFiringWD.register(eventListener1); eventFiringWD.get("https://www.google.co.in/"); eventFiringWD.findElement(By.className("nosuchclassName")); } }
解除注册的方法:
eventFiringWD=eventFiringWD.unregister(eventListener1);
加监听的最大目的,就是减少维护脚本所花的力气, 让报错的信息,更容易定位到问题。
以上是关于EventFiringWebDriver网页事件监听的主要内容,如果未能解决你的问题,请参考以下文章
EventFiringWebDriver / WebDriverEventListener 调用顺序