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
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
来源:https://stackoverflow.com/questions/49367957/java-wait-for-a-html-element-and-record-the-mouse-click-through-webdrivereventli