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

后端 未结 1 1708
再見小時候
再見小時候 2020-11-30 15:55

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 aft

相关标签:
1条回答
  • 2020-11-30 16:48

    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 :

    • presenceOfElementLocated(By locator)
    • visibilityOfElementLocated(By locator)
    • elementToBeClickable(By locator)
    • frameToBeAvailableAndSwitchToIt(By locator)
    • numberOfwindowsToBe(int expectedNumberOfWindows)

    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
    
    0 讨论(0)
提交回复
热议问题