下面列出了怎么用org.openqa.selenium.interactions.HasInputDevices的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Void call() {
Mouse mouse = ((HasInputDevices) getDriver()).getMouse();
Coordinates elementLocation = null;
if (elementProvided) {
WebElement element = getKnownElements().get(elementId);
elementLocation = ((Locatable) element).getCoordinates();
}
if (offsetsProvided) {
mouse.mouseMove(elementLocation, xOffset, yOffset);
} else {
mouse.mouseMove(elementLocation);
}
return null;
}
@Override
public Keyboard getKeyboard()
{
if (wrappedDriver instanceof HasInputDevices)
{
return ((HasInputDevices) wrappedDriver).getKeyboard();
}
throw new UnsupportedOperationException(ADVANCED_INTERACTION_NOT_SUPPORTED);
}
@Override
public Mouse getMouse()
{
if (wrappedDriver instanceof HasInputDevices)
{
return ((HasInputDevices) wrappedDriver).getMouse();
}
throw new UnsupportedOperationException(ADVANCED_INTERACTION_NOT_SUPPORTED);
}
@Test
void testGetKeyboard()
{
WebDriver driverWithInputDevices = Mockito.mock(WebDriver.class,
withSettings().extraInterfaces(HasInputDevices.class));
Keyboard keyboard = Mockito.mock(Keyboard.class);
when(((HasInputDevices) driverWithInputDevices).getKeyboard()).thenReturn(keyboard);
assertEquals(keyboard, new DelegatingWebDriver(driverWithInputDevices).getKeyboard());
}
@Test
void testGetMouse()
{
WebDriver driverWithInputDevices = Mockito.mock(WebDriver.class,
withSettings().extraInterfaces(HasInputDevices.class));
Mouse mouse = Mockito.mock(Mouse.class);
when(((HasInputDevices) driverWithInputDevices).getMouse()).thenReturn(mouse);
assertEquals(mouse, new DelegatingWebDriver(driverWithInputDevices).getMouse());
}
@Test
void clickInvisibleElement()
{
when(webDriverProvider.get()).thenReturn(webDriver);
when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
Coordinates coordinates = mock(Coordinates.class);
when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
mouseActions.moveToAndClick(locatableWebElement);
verify(mouse).mouseMove(coordinates);
verify(mouse).click(null);
}
@Test
void testContextClick()
{
when(webDriverProvider.get()).thenReturn(webDriver);
when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
Coordinates coordinates = mock(Coordinates.class);
when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
mouseActions.contextClick(locatableWebElement);
verify(mouse).contextClick(coordinates);
}
@Test
void testMoveToElement()
{
when(webDriverProvider.get()).thenReturn(webDriver);
when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
Coordinates coordinates = mock(Coordinates.class);
when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
mouseActions.moveToElement(locatableWebElement);
verify(mouse).mouseMove(coordinates);
}
@Test
void testMoveToElementMoveTargetOutOfBoundsException()
{
when(webDriverProvider.get()).thenReturn(webDriver);
when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
MoveTargetOutOfBoundsException boundsException = new MoveTargetOutOfBoundsException(
COULD_NOT_MOVE_TO_ERROR_MESSAGE);
Coordinates coordinates = mock(Coordinates.class);
when(((Locatable) locatableWebElement).getCoordinates()).thenReturn(coordinates);
doThrow(boundsException).when(mouse).mouseMove(coordinates);
mouseActions.moveToElement(locatableWebElement);
verify(softAssert).recordFailedAssertion(COULD_NOT_MOVE_TO_ERROR_MESSAGE + boundsException);
}
@Test
void testMoveToElementOnMobile()
{
when(webDriverProvider.get()).thenReturn(webDriver);
when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
when(webDriverManager.isMobile()).thenReturn(true);
mouseActions.moveToElement(locatableWebElement);
verify(javascriptActions).scrollIntoView(locatableWebElement, true);
}
@Test
void shouldScrollToElementOnBrowsersNotPerformingScrollAutomatically()
{
when(webDriverProvider.get()).thenReturn(webDriver);
when(((HasInputDevices) webDriver).getMouse()).thenReturn(mouse);
when(webDriverManager.isMobile()).thenReturn(false);
when(webDriverManager.isTypeAnyOf(WebDriverType.SAFARI, WebDriverType.FIREFOX, WebDriverType.EDGE))
.thenReturn(true);
mouseActions.moveToElement(locatableWebElement);
verify(javascriptActions).scrollIntoView(locatableWebElement, true);
}
public static <T extends WebDriver> SelfHealingDriver createDriverProxy(ClassLoader loader, InvocationHandler handler, Class<T> clazz) {
Class<?>[] interfaces = Stream.concat(
Arrays.stream(clazz.getInterfaces()),
Stream.of(JavascriptExecutor.class, SelfHealingDriver.class, HasInputDevices.class, Interactive.class)
).distinct().toArray(Class[]::new);
return (SelfHealingDriver) Proxy.newProxyInstance(loader, interfaces, handler);
}
@Override
public Keyboard getKeyboard()
{
setLastAction();
if ( webDriver instanceof HasInputDevices )
return ((HasInputDevices) webDriver).getKeyboard();
else
return null;
}
@Override
public Mouse getMouse()
{
setLastAction();
if ( webDriver instanceof HasInputDevices )
return ((HasInputDevices) webDriver).getMouse();
else
return null;
}
public boolean _moveTo()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).moveTo(createPoint(webElement)).perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).moveToElement(webElement).build().perform();
else
new Actions(getWebDriver()).moveToElement(webElement).build().perform();
}
return true;
}
}
return false;
}
public boolean _press()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).press(createPoint(webElement)).perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).clickAndHold(webElement).build().perform();
else
new Actions(getWebDriver()).clickAndHold(webElement).build().perform();
}
return true;
}
}
return false;
}
public boolean _clickAt( int offsetX, int offsetY )
{
WebElement webElement = getElement();
if (webElement != null)
{
CloudActionProvider aP = getWebDriver().getCloud().getCloudActionProvider();
Dimension elementSize = aP.translateDimension(getWebDriver(), webElement.getSize());
int useX = (int) ((double) elementSize.getWidth() * ((double) offsetX / 100.0) + webElement.getLocation().getX());
int useY = (int) ((double) elementSize.getHeight() * ((double) offsetY / 100.0) + webElement.getLocation().getY());
if (log.isInfoEnabled())
log.info("Clicking " + useX + "," + useY + " pixels relative to " + getName());
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
aP.tap(getWebDriver(), new PercentagePoint(useX, useY, false), 500);
return true;
}
return true;
}
return false;
}
public boolean _mouseDoubleClick()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchActions((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).doubleTap(webElement).perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).doubleClick().build().perform();
else
new Actions(getWebDriver()).doubleClick(webElement).build().perform();
}
return true;
}
}
return false;
}
public boolean _release()
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).release().perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof HasTouchScreen)
new TouchActions(getWebDriver()).release().build().perform();
else
new Actions(getWebDriver()).release().build().perform();
}
return true;
}
}
return false;
}
/**
* Forces a mouseDown event on the WebElement.
*/
public void mouseDown() {
TestLogging.log("MouseDown " + this.toString());
findElement();
final Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseDown(null);
}
/**
* Forces a mouseOver event on the WebElement.
*/
public void mouseOver() {
TestLogging.log("MouseOver " + this.toString());
findElement();
// build and perform the mouseOver with Advanced User Interactions API
// Actions builder = new Actions(driver);
// builder.moveToElement(element).build().perform();
final Locatable hoverItem = (Locatable) element;
final Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
}
/**
* Forces a mouseUp event on the WebElement.
*/
public void mouseUp() {
TestLogging.log("MouseUp " + this.toString());
findElement();
final Mouse mouse = ((HasInputDevices) driver).getMouse();
mouse.mouseUp(null);
}
@Override
public Keyboard getKeyboard() {
if (driver instanceof HasInputDevices) {
return new EventFiringKeyboard(driver, dispatcher);
}
throw new UnsupportedOperationException("Underlying driver does not implement advanced"
+ " user interactions yet.");
}
@Override
public Mouse getMouse() {
if (driver instanceof HasInputDevices) {
return new EventFiringMouse(driver, dispatcher);
}
throw new UnsupportedOperationException("Underlying driver does not implement advanced"
+ " user interactions yet.");
}
@Override
public Void call() {
Mouse mouse = ((HasInputDevices) getDriver()).getMouse();
if (leftMouseButton) {
mouse.click(null);
} else {
mouse.contextClick(null);
}
return null;
}
@Override
public Void call() {
Keyboard keyboard = ((HasInputDevices) getDriver()).getKeyboard();
String[] keysToSend = keys.toArray(new String[0]);
keyboard.sendKeys(keysToSend);
return null;
}
@Override
public Keyboard getKeyboard() {
return ((HasInputDevices) driver).getKeyboard();
}
@Override
public Mouse getMouse() {
return ((HasInputDevices) driver).getMouse();
}
public boolean _clickFor( int length )
{
WebElement webElement = getElement();
if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0)
{
if (getWebDriver() instanceof HasInputDevices)
{
if (isTimed())
getActionProvider().startTimer((DeviceWebDriver) getWebDriver(), this, getExecutionContext());
if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof AppiumDriver)
{
new TouchAction((AppiumDriver<?>) ((DeviceWebDriver) getWebDriver()).getNativeDriver()).press(createPoint(webElement)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(length))).release().perform();
}
else if (((DeviceWebDriver) getWebDriver()).getNativeDriver() instanceof RemoteWebDriver)
{
CloudActionProvider aP = getWebDriver().getCloud().getCloudActionProvider();
Point clickLocation = aP.translatePoint(getWebDriver(), webElement.getLocation());
Dimension clickSize = aP.translateDimension(getWebDriver(), webElement.getSize());
Dimension windowSize = aP.translateDimension(getWebDriver(), getWebDriver().manage().window().getSize());
double x = clickLocation.getX() + (clickSize.getWidth() / 2);
double y = clickLocation.getY() + (clickSize.getHeight() / 2);
int percentX = (int) (x / windowSize.getWidth() * 100.0);
int percentY = (int) (y / windowSize.getHeight() * 100.0);
try
{
new TouchActions(getWebDriver()).longPress(webElement).build().perform();
}
catch (Exception e)
{
((DeviceWebDriver) getWebDriver()).getCloud().getCloudActionProvider().tap((DeviceWebDriver) getWebDriver(), new PercentagePoint(percentX, percentY, true), length);
}
}
return true;
}
}
return false;
}
@Override
public Keyboard getKeyboard()
{
return ((HasInputDevices)driver).getKeyboard();
}