下面列出了怎么用org.openqa.selenium.interactions.Interactive的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
void shouldDragAndDropToTargetAtLocation()
{
WebElement draggable = mockWebElementWithRect(100, 100, 50, 50);
WebElement target = mockWebElementWithRect(200, 200, 50, 50);
WebDriver driver = mock(WebDriver.class, withSettings().extraInterfaces(Interactive.class));
when(webDriverProvider.get()).thenReturn(driver);
testDragAndDropToTargetAtLocation(draggable, Location.TOP, target);
String actionsSequence = String.format(
"{id=default mouse, type=pointer, parameters={pointerType=mouse}, "
+ "actions=["
+ "{duration=100, x=0, y=0, type=pointerMove, origin=Mock for WebElement, hashCode: %s}, "
+ "{button=0, type=pointerDown}, "
+ "{duration=200, x=10, y=0, type=pointerMove, origin=pointer}, "
+ "{duration=200, x=-10, y=0, type=pointerMove, origin=pointer}, "
+ "{duration=200, x=100, y=50, type=pointerMove, origin=pointer}, "
+ "{button=0, type=pointerUp}, "
+ "{duration=1000, type=pause}]}", draggable.hashCode());
verify((Interactive) driver).perform(
argThat(arg -> arg.iterator().next().encode().toString().equals(actionsSequence)));
}
@Override
public void perform(Collection<Sequence> actions)
{
if (wrappedDriver instanceof Interactive)
{
((Interactive) wrappedDriver).perform(actions);
return;
}
throw new UnsupportedOperationException(ADVANCED_INTERACTION_NOT_SUPPORTED);
}
@Override
public void resetInputState()
{
if (wrappedDriver instanceof Interactive)
{
((Interactive) wrappedDriver).resetInputState();
return;
}
throw new UnsupportedOperationException(ADVANCED_INTERACTION_NOT_SUPPORTED);
}
@Test
void testPerform()
{
WebDriver interactiveDriver = Mockito.mock(WebDriver.class, withSettings().extraInterfaces(Interactive.class));
DelegatingWebDriver interactiveDelegatingDriver = new DelegatingWebDriver(interactiveDriver);
interactiveDelegatingDriver.perform(List.of());
verify((Interactive) interactiveDriver).perform(List.of());
}
@Test
void testResetInputState()
{
WebDriver interactiveDriver = Mockito.mock(WebDriver.class, withSettings().extraInterfaces(Interactive.class));
DelegatingWebDriver interactiveDelegatingDriver = new DelegatingWebDriver(interactiveDriver);
interactiveDelegatingDriver.resetInputState();
verify((Interactive) interactiveDriver).resetInputState();
}
@Test
void testExecuteActionsSequenceElementIsNull()
{
SearchAttributes searchAttributes = new SearchAttributes(ActionAttributeType.XPATH, SIGNATURE_FIELD_XPATH);
when(baseValidations.assertIfElementExists(ELEMENT_EXISTS_MESSAGE, searchAttributes)).thenReturn(null);
List<SequenceAction> actions = List.of(
new SequenceAction(SequenceActionType.DOUBLE_CLICK, searchAttributes),
new SequenceAction(SequenceActionType.ENTER_TEXT, TEXT)
);
actionSteps.executeSequenceOfActions(actions);
verify((Interactive) webDriver, never()).perform(any());
}
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 void perform(Collection<Sequence> actions) {
if (driver instanceof Interactive) {
((Interactive) driver).perform(actions);
return;
}
throw new UnsupportedOperationException("Underlying driver does not implement advanced"
+ " user interactions yet.");
}
@Override
public void resetInputState() {
if (driver instanceof Interactive) {
((Interactive) driver).resetInputState();
return;
}
throw new UnsupportedOperationException("Underlying driver does not implement advanced"
+ " user interactions yet.");
}
@SuppressWarnings("LineLength")
@Test
void testExecuteSequenceOfActions()
{
SearchAttributes searchAttributes = new SearchAttributes(ActionAttributeType.XPATH, SIGNATURE_FIELD_XPATH);
Point point = mock(Point.class);
WebElement webElement = mock(WebElement.class);
int offset = 15;
when(baseValidations.assertIfElementExists(ELEMENT_EXISTS_MESSAGE, searchAttributes)).thenReturn(webElement);
when(point.getX()).thenReturn(offset);
when(point.getY()).thenReturn(offset);
List<SequenceAction> actions = List.of(
new SequenceAction(SequenceActionType.DOUBLE_CLICK, searchAttributes),
new SequenceAction(SequenceActionType.CLICK_AND_HOLD, searchAttributes),
new SequenceAction(SequenceActionType.MOVE_BY_OFFSET, point),
new SequenceAction(SequenceActionType.RELEASE, searchAttributes),
new SequenceAction(SequenceActionType.ENTER_TEXT, TEXT),
new SequenceAction(SequenceActionType.CLICK, searchAttributes),
new SequenceAction(SequenceActionType.MOVE_TO, searchAttributes),
new SequenceAction(SequenceActionType.DOUBLE_CLICK, null),
new SequenceAction(SequenceActionType.CLICK_AND_HOLD, null),
new SequenceAction(SequenceActionType.RELEASE, null),
new SequenceAction(SequenceActionType.CLICK, null)
);
actionSteps.executeSequenceOfActions(actions);
verify((Interactive) webDriver).perform(argThat(arg -> {
int hash = webElement.hashCode();
String mouseSequence = "{id=default mouse, type=pointer, parameters={pointerType=mouse}, actions=["
+ format(POINTER_MOVE, hash)
+ DOUBLE_CLICK_ACTION + SEPARATOR
+ format(POINTER_MOVE, hash)
+ CLICK_AND_HOLD_ACTION + SEPARATOR
+ "{duration=200, x=15, y=15, type=pointerMove, origin=pointer}" + SEPARATOR
+ format(POINTER_MOVE, hash)
+ RELEASE_ACTION + SEPARATOR
+ DURATION_PART + DURATION_PART
+ format(POINTER_MOVE, hash)
+ CLICK_ACTION + SEPARATOR
+ format(POINTER_MOVE_ACTION, hash) + SEPARATOR
+ DOUBLE_CLICK_ACTION + SEPARATOR
+ CLICK_AND_HOLD_ACTION + SEPARATOR
+ RELEASE_ACTION + SEPARATOR
+ CLICK_ACTION
+ "]}";
String keyboardSequence = "{type=key, actions=["
+ DURATION_PART + DURATION_PART
+ "{duration=0, type=pause}, {duration=0, type=pause}, "
+ "{type=keyDown, value=t}, {type=keyUp, value=t}, {type=keyDown, value=e}, {type=keyUp, value=e}, "
+ "{type=keyDown, value=x}, {type=keyUp, value=x}, {type=keyDown, value=t}, {type=keyUp, value=t}, "
+ "{duration=0, type=pause}, {duration=0, type=pause}, {duration=0, type=pause}, {duration=0, type=pause},"
+ " {duration=0, type=pause}, {duration=0, type=pause}, {duration=0, type=pause}, {duration=0, type=pause},"
+ " {duration=0, type=pause}, {duration=0, type=pause}, {duration=0, type=pause}, {duration=0, type=pause}], "
+ "id=default keyboard}";
return asString(arg).equals(mouseSequence + keyboardSequence);
}));
}