Package co.verisoft.fw.async
Class AsyncListenerImp
- java.lang.Object
-
- co.verisoft.fw.async.AsyncListenerImp
-
- All Implemented Interfaces:
AsyncExecutor.AsyncListener,Subject,org.openqa.selenium.support.events.WebDriverListener
public class AsyncListenerImp extends Object implements AsyncExecutor.AsyncListener
A listener to enable asynchronous operations. The listener is a Selenium WebDrvier based listener which implements both the Junit 5 listener design and the observer design pattern.
VeriSoft's framework asynchronous operation model explained:
Since WebDriver is a single thread application, and it's operations are not thread safe, a solution is needed to async operations within a single thread.
org.openqa.selenium. @see JavascriptExecutor#executeAsyncScript(String, Object...) method does not provide a solution since the js is indeed executed asynchronously, however WebDriver does not, hence it is hanged while it waits for the JS to finish it's execution.
So, the current solution is based on the JS model of running async operations on the same thread. It is async rather than paralel. In order to run operations in an async way, breaking points during the execution are needed. The most common breaking point in a Selenium WebDriver scenario is the findElement operation. It is the most common operation and the most frequently used operation. So... We hooked on, using the listener and EventFiringWebDriver mechnism to hook in before every single findElement method is invoked.
In order to to not overdue, a time interval was introduced, so if
a. a findElement method was invoked, and
b. interval has elapsed, a method will be called.
We use the observer pattern to manage all the async code in one place. If you are not familiar with the observer patter, visit the wikipedia site. Essentially, this class serves as both listener, which implements the SearchingEventListener interface, and subject (from the observer mechanism), which implements the Subject interface.- Since:
- 2.0.1
- Author:
- Nir Gallner @ www.VeriSoft.co
-
-
Constructor Summary
Constructors Constructor Description AsyncListenerImp()AsyncListenerImp(Duration interval)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeforeAnyCall(Object target, Method method, Object[] args)voidcollectGarbage()Loops through the observers list and looks for observers which flags themselves as observers who want to unregister themselves from the subject's list.DurationgetDispatchInterval()GettervoidnotifyObservers()Notify all observers when time is due to execute async Typically, when the implementation uses an ArrayList to keep track of all the observes, this method will loop through the list and activate the update method for each observervoidregister(Observer o)Registers a new observer to be notified.voidsetDispatchInterval(Duration duration)Setter for the dispatch interval and the time unit.voidunregister(Observer o)Unregister an existing observer, and stop notifying the observer upon invocation of notifyObservers methodvoidunregisterAll()Unregisters all observers from subject-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.openqa.selenium.support.events.WebDriverListener
afterAccept, afterAddCookie, afterAnyAlertCall, afterAnyCall, afterAnyNavigationCall, afterAnyOptionsCall, afterAnyTimeoutsCall, afterAnyWebDriverCall, afterAnyWebElementCall, afterAnyWindowCall, afterBack, afterClear, afterClick, afterClose, afterDeleteAllCookies, afterDeleteCookie, afterDeleteCookieNamed, afterDismiss, afterExecuteAsyncScript, afterExecuteScript, afterFindElement, afterFindElement, afterFindElements, afterFindElements, afterForward, afterFullscreen, afterGet, afterGetAttribute, afterGetCookieNamed, afterGetCookies, afterGetCssValue, afterGetCurrentUrl, afterGetLocation, afterGetPageSource, afterGetPosition, afterGetSize, afterGetSize, afterGetTagName, afterGetText, afterGetText, afterGetTitle, afterGetWindowHandle, afterGetWindowHandles, afterImplicitlyWait, afterIsDisplayed, afterIsEnabled, afterIsSelected, afterMaximize, afterPageLoadTimeout, afterPerform, afterQuit, afterRefresh, afterResetInputState, afterSendKeys, afterSendKeys, afterSetPosition, afterSetScriptTimeout, afterSetSize, afterSubmit, afterTo, afterTo, beforeAccept, beforeAddCookie, beforeAnyAlertCall, beforeAnyNavigationCall, beforeAnyOptionsCall, beforeAnyTimeoutsCall, beforeAnyWebDriverCall, beforeAnyWebElementCall, beforeAnyWindowCall, beforeBack, beforeClear, beforeClick, beforeClose, beforeDeleteAllCookies, beforeDeleteCookie, beforeDeleteCookieNamed, beforeDismiss, beforeExecuteAsyncScript, beforeExecuteScript, beforeFindElement, beforeFindElement, beforeFindElements, beforeFindElements, beforeForward, beforeFullscreen, beforeGet, beforeGetAttribute, beforeGetCookieNamed, beforeGetCookies, beforeGetCssValue, beforeGetCurrentUrl, beforeGetLocation, beforeGetPageSource, beforeGetPosition, beforeGetSize, beforeGetSize, beforeGetTagName, beforeGetText, beforeGetText, beforeGetTitle, beforeGetWindowHandle, beforeGetWindowHandles, beforeImplicitlyWait, beforeIsDisplayed, beforeIsEnabled, beforeIsSelected, beforeMaximize, beforePageLoadTimeout, beforePerform, beforeQuit, beforeRefresh, beforeResetInputState, beforeSendKeys, beforeSendKeys, beforeSetPosition, beforeSetScriptTimeout, beforeSetSize, beforeSubmit, beforeTo, beforeTo, onError
-
-
-
-
Constructor Detail
-
AsyncListenerImp
public AsyncListenerImp(Duration interval)
-
AsyncListenerImp
public AsyncListenerImp()
-
-
Method Detail
-
setDispatchInterval
public void setDispatchInterval(Duration duration)
Setter for the dispatch interval and the time unit. The defailt valie of the dispatcher is 5 second and it is the minimum dispatcher possible. If tried to set less than 5 second, setter will not update the values- Specified by:
setDispatchIntervalin interfaceAsyncExecutor.AsyncListener- Parameters:
duration- new interval for invocation
-
getDispatchInterval
public Duration getDispatchInterval()
Getter- Returns:
- interval field
-
register
public void register(Observer o)
Description copied from interface:SubjectRegisters a new observer to be notified. Typically it adds an object which implements the Observer interface to an ArrayList. Once the observer is on the list, each invocation of notify will trigger an update for all observers on the list
-
unregister
public void unregister(Observer o)
Description copied from interface:SubjectUnregister an existing observer, and stop notifying the observer upon invocation of notifyObservers method- Specified by:
unregisterin interfaceSubject- Parameters:
o- Observer object to be removed
-
unregisterAll
public void unregisterAll()
Unregisters all observers from subject
-
notifyObservers
public void notifyObservers()
Description copied from interface:SubjectNotify all observers when time is due to execute async Typically, when the implementation uses an ArrayList to keep track of all the observes, this method will loop through the list and activate the update method for each observer- Specified by:
notifyObserversin interfaceSubject
-
collectGarbage
public void collectGarbage()
Loops through the observers list and looks for observers which flags themselves as observers who want to unregister themselves from the subject's list. Technically, the method calls observer.isDisposed() for each observer on the list, and if the result is true, the method performs unregister(observer)
-
-