Class 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beforeAnyCall​(Object target, Method method, Object[] args)  
      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.
      Duration getDispatchInterval()
      Getter
      void notifyObservers()
      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 observer
      void register​(Observer o)
      Registers a new observer to be notified.
      void setDispatchInterval​(Duration duration)
      Setter for the dispatch interval and the time unit.
      void unregister​(Observer o)
      Unregister an existing observer, and stop notifying the observer upon invocation of notifyObservers method
      void unregisterAll()
      Unregisters all observers from subject
      • 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:
        setDispatchInterval in interface AsyncExecutor.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: Subject
        Registers 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
        Specified by:
        register in interface Subject
        Parameters:
        o - Observer object to be added
      • unregister

        public void unregister​(Observer o)
        Description copied from interface: Subject
        Unregister an existing observer, and stop notifying the observer upon invocation of notifyObservers method
        Specified by:
        unregister in interface Subject
        Parameters:
        o - Observer object to be removed
      • unregisterAll

        public void unregisterAll()
        Unregisters all observers from subject
      • notifyObservers

        public void notifyObservers()
        Description copied from interface: Subject
        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 observer
        Specified by:
        notifyObservers in interface Subject
      • 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)
      • beforeAnyCall

        public void beforeAnyCall​(Object target,
                                  Method method,
                                  Object[] args)
        Specified by:
        beforeAnyCall in interface org.openqa.selenium.support.events.WebDriverListener