Java等待HTML元素并通过WebDriverEventListener记录鼠标单击 [英] Java Wait for a HTML element and record the mouse click through WebDriverEventListener
问题描述
我正在开发一个Java应用程序来帮助进行硒测试,我想知道是否有可能强制该应用程序等待单击,然后单击确定html的哪个元素被单击
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
问候
推荐答案
回答您的问题:
如果可以强制应用程序等待点击:从技术上讲,click()
的调用由最终用户控制,该最终用户也是脚本/程序的所有者.同样,在功能上,您的脚本/程序不必等待click()
,而需要等待预期的 WebElement 是可交互的( clickable ).与该用例相似,当您自动化测试用例时,可能必须将快速移动的 WebDriver 实例与滞后的 Web Client 进行同步.为了实现 Selenium 为您提供 ExpectedConditions Class .
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 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)
单击该按钮之后,确定单击了html的哪个元素:要对此进行识别,您必须使用 WebDriverEventListener
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 是围绕任意 WebDriver 实例的包装,该实例主要用于记录目的而支持WebDriverEventListener的注册.
EventFiringWebDriver is a wrapper around an arbitrary WebDriver instance which supports registering of a WebDriverEventListener majorly for logging purposes.
-
EventFiringWebDriver 程序的示例:
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
EventHandler handler = new EventHandler();
eventDriver.register(handler);
eventDriver.get("https://google.com");
System.out.println(eventDriver.getTitle());
-
EventHandler 类的示例:
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);
}
}
控制台输出:
Just before beforeNavigateTo https://google.com
Inside the afterNavigateTo to https://google.com
Google
这篇关于Java等待HTML元素并通过WebDriverEventListener记录鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!