Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
911 views
in Technique[技术] by (71.8m points)

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

I am developing a java application to help the construction of selenium tests and I would like to know if it is possible to force the application to wait for a click and after that click identify which element of html was clicked

greetings

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...