Java Wait for a HTML element and record the mouse click through WebDriverEventListener

 ̄綄美尐妖づ 提交于 2019-11-28 14:30:11

Answering your questions :

If it is possible to force the application to wait for a click : Technically the invokation of click() is governed by the enduser who is also the owner of the script/program. Again functionally your script/program need not wait for click() but need to wait for the intended WebElement to be interactable (i.e. clickable). Similar to this usecase while you automate your testcases you may have to synchronize the fast moving WebDriver instance with the lagging Web Client. To achieve that Selenium provides you the WebDriverWait Class which can be used in conjunction with ExpectedConditions Class.

ExpectedConditions

ExpectedConditions Class enables us to comply with numerous conditions. A couple of most widely used ExpectedConditions are as follows :


After that click identify which element of html was clicked : To acieve this you have to take help of EventFiringWebDriver which will register an instance of EventHandler which will implement WebDriverEventListener

EventFiringWebDriver

EventFiringWebDriver is a wrapper around an arbitrary WebDriver instance which supports registering of a WebDriverEventListener majorly for logging purposes.

  • An example of EventFiringWebDriver program :

    EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
    EventHandler handler = new EventHandler();
    eventDriver.register(handler);
    eventDriver.get("https://google.com");
    System.out.println(eventDriver.getTitle());
    

EventHandler

  • An example of EventHandler class :

     public class EventHandler implements WebDriverEventListener
     {
        @Override
        public void afterNavigateTo(String arg0, WebDriver arg1) {
            System.out.println("Inside the afterNavigateTo to " + arg0);
        }
    
        @Override
        public void beforeNavigateTo(String arg0, WebDriver arg1) {
            System.out.println("Just before beforeNavigateTo " + arg0);
        }
     }
    

Console Output :

Just before beforeNavigateTo https://google.com
Inside the afterNavigateTo to https://google.com
Google
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!