|
| 1 | +since 4.1.0 |
| 2 | + |
| 3 | +# The purpose |
| 4 | + |
| 5 | +This feature allows end user to organize the event logging on the client side. Also this feature may be useful in a binding with standard or custom reporting |
| 6 | +frameworks. |
| 7 | + |
| 8 | + |
| 9 | +# The API |
| 10 | + |
| 11 | +The API was designed the way which allows end user to select events (searching, navigation, exception throwing etc.) which should be listened to. It contains |
| 12 | +the following list of interfaces (new items may be added further): |
| 13 | + |
| 14 | +- `io.appium.java_client.events.api.Listener` is the basic interface |
| 15 | +- `io.appium.java_client.events.api.general.AlertEventListener` is for the listening to alerts |
| 16 | +- `io.appium.java_client.events.api.general.ElementEventListener` is for the listening to actions related to elements |
| 17 | +- `io.appium.java_client.events.api.general.JavaScriptEventListener` is for the listening to java script executing |
| 18 | +- `io.appium.java_client.events.api.general.ListensToException` is for the listening to exceptions which are thrown |
| 19 | +- `io.appium.java_client.events.api.general.NavigationEventListener` is for the listening to events related to navigation |
| 20 | +- `io.appium.java_client.events.api.general.SearchingEventListener` is for the listening to events related to the searching. |
| 21 | +- `io.appium.java_client.events.api.general.WindowEventListener` is for the listening to actions on a window |
| 22 | +- `io.appium.java_client.events.api.mobile.ContextEventListener` is for the listening to the switching to mobile context |
| 23 | +- `io.appium.java_client.events.api.mobile.RotationEventListener` is for the listening to screen rotation |
| 24 | +- `io.appium.java_client.events.api.general.AppiumWebDriverEventListener` was added to provide the compatibility with |
| 25 | +user's implementation of `org.openqa.selenium.support.events.WebDriverEventListener`. Also it extends some interfaces above. |
| 26 | + |
| 27 | +# Briefly about the engine. |
| 28 | + |
| 29 | +This is pretty similar solution as the `org.openqa.selenium.support.events.EventFiringWebDriver` of the Selenium project. You |
| 30 | +can read about this thing there [The blog post](http://seleniumworks.blogspot.ru/2014/02/eventfiringwebdriver.html). |
| 31 | + |
| 32 | +Here we were trying to improve existing drawbacks and restrictions using: |
| 33 | + |
| 34 | +- API splitting, see above. |
| 35 | + |
| 36 | +- the binding of some [Spring framework engines](https://projects.spring.io/spring-framework/) with [AspectJ](https://en.wikipedia.org/wiki/AspectJ). |
| 37 | + |
| 38 | +# How to use |
| 39 | + |
| 40 | +It is easy. |
| 41 | + |
| 42 | +```java |
| 43 | +import io.appium.java_client.events.api.general.AlertEventListener; |
| 44 | + |
| 45 | +public class AlertListener implements AlertEventListener { |
| 46 | +... |
| 47 | +} |
| 48 | + |
| 49 | +... |
| 50 | +import io.appium.java_client.events.api.general.ElementEventListener; |
| 51 | + |
| 52 | +public class ElementListener implements ElementEventListener { |
| 53 | +... |
| 54 | +} |
| 55 | + |
| 56 | +//and so on |
| 57 | +... |
| 58 | +import io.appium.java_client.events.EventFiringWebDriverFactory; |
| 59 | +import io.appium.java_client.events.api.Listener; |
| 60 | +... |
| 61 | + |
| 62 | +AndroidDriver driver = new AndroidDriver(parameters); |
| 63 | +driver = EventFiringWebDriverFactory.getEventFiringWebDriver(driver, new AlertListener(), |
| 64 | + new ElementListener()); |
| 65 | + |
| 66 | +//or |
| 67 | +AndroidDriver driver2 = new AndroidDriver(parameters); |
| 68 | +List<Listener> listeners = new ArrayList<>(); |
| 69 | +listeners.add(new AlertListener()); |
| 70 | +listeners.add(new ElementListener()); |
| 71 | +driver = EventFiringWebDriverFactory.getEventFiringWebDriver(driver2, listeners); |
| 72 | +``` |
| 73 | + |
| 74 | +## What if there are listeners which used everywhere by default. |
| 75 | + |
| 76 | +In order to avoid the repeating actions an end user is free to do these things: |
| 77 | + |
| 78 | +- create folders `/META-INF/services` and put the file `io.appium.java_client.events.api.Listener` there. Please read about |
| 79 | +[SPI](https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html). |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +- define the list of default listeners at the `io.appium.java_client.events.api.Listener` |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +And then it is enough |
| 88 | + |
| 89 | +```java |
| 90 | + |
| 91 | +//and so on |
| 92 | +... |
| 93 | +import io.appium.java_client.events.EventFiringWebDriverFactory; |
| 94 | +... |
| 95 | + |
| 96 | +AndroidDriver driver = new AndroidDriver(parameters); |
| 97 | +driver = EventFiringWebDriverFactory.getEventFiringWebDriver(driver); |
| 98 | +``` |
| 99 | + |
| 100 | +If there are listeners defined externally when this collection is merged with default set of listeners. |
| 101 | + |
| 102 | +# How to reuse customized WebDriverEventListener |
| 103 | + |
| 104 | +If an end user has their own `org.openqa.selenium.support.events.WebDriverEventListener` implementation then in order to |
| 105 | +make it compatible with this engine it is enough to do the following. |
| 106 | + |
| 107 | + |
| 108 | +```java |
| 109 | +import org.openqa.selenium.support.events.WebDriverEventListener; |
| 110 | +import io.appium.java_client.events.api.general.AppiumWebDriverEventListener; |
| 111 | + |
| 112 | +public class UsersWebDriverEventListener implements WebDriverEventListener, AppiumWebDriverEventListener { |
| 113 | +... |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +or just |
| 118 | + |
| 119 | +```java |
| 120 | +import io.appium.java_client.events.api.general.AppiumWebDriverEventListener; |
| 121 | + |
| 122 | +public class UsersWebDriverEventListener implements AppiumWebDriverEventListener { |
| 123 | +... |
| 124 | +} |
| 125 | +``` |
0 commit comments