下面列出了怎么用org.openqa.selenium.interactions.Sequence的API类实例代码及写法,或者点击链接到github查看源代码。
private void drawCircle (AppiumDriver driver, Point origin, double radius, int steps) {
Point firstPoint = getPointOnCircle(0, steps, origin, radius);
PointerInput finger = new PointerInput(Kind.TOUCH, "finger");
Sequence circle = new Sequence(finger, 0);
circle.addAction(finger.createPointerMove(NO_TIME, VIEW, firstPoint.x, firstPoint.y));
circle.addAction(finger.createPointerDown(MouseButton.LEFT.asArg()));
for (int i = 1; i < steps + 1; i++) {
Point point = getPointOnCircle(i, steps, origin, radius);
circle.addAction(finger.createPointerMove(STEP_DURATION, VIEW, point.x, point.y));
}
circle.addAction(finger.createPointerUp(MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(circle));
}
@Test
public void testLowLevelKeys() {
wait.until(ExpectedConditions.presenceOfElementLocated(loginScreen)).click();
WebElement usernameField = driver.findElement(username);
usernameField.click();
KeyInput keyboard = new KeyInput("keyboard");
Sequence sendKeys = new Sequence(keyboard, 0);
sendKeys.addAction(keyboard.createKeyDown(Keys.SHIFT.getCodePoint()));
sendKeys.addAction(keyboard.createKeyDown("f".codePointAt(0)));
sendKeys.addAction(keyboard.createKeyUp("f".codePointAt(0)));
sendKeys.addAction(keyboard.createKeyUp(Keys.SHIFT.getCodePoint()));
sendKeys.addAction(keyboard.createKeyDown("o".codePointAt(0)));
sendKeys.addAction(keyboard.createKeyUp("o".codePointAt(0)));
sendKeys.addAction(keyboard.createKeyDown("o".codePointAt(0)));
sendKeys.addAction(keyboard.createKeyUp("o".codePointAt(0)));
driver.perform(Arrays.asList(sendKeys));
Assert.assertEquals("Foo", usernameField.getText());
}
@Override
public void perform(Collection<Sequence> actions)
{
if (wrappedDriver instanceof Interactive)
{
((Interactive) wrappedDriver).perform(actions);
return;
}
throw new UnsupportedOperationException(ADVANCED_INTERACTION_NOT_SUPPORTED);
}
private static String asString(Collection<Sequence> sequences)
{
return sequences.stream()
.map(Sequence::encode)
.map(Map::toString)
.sorted()
.collect(Collectors.joining());
}
private Collection<Sequence> zoom(Point locus, int startRadius, int endRadius, int pinchAngle, Duration duration) {
// convert degree angle into radians. 0/360 is top (12 O'clock).
double angle = Math.PI / 2 - (2 * Math.PI / 360 * pinchAngle);
// create the gesture for one finger
Sequence fingerAPath = zoomSinglefinger("fingerA", locus, startRadius, endRadius, angle, duration);
// flip the angle around to the other side of the locus and get the gesture for the second finger
angle = angle + Math.PI;
Sequence fingerBPath = zoomSinglefinger("fingerB", locus, startRadius, endRadius, angle, duration);
return Arrays.asList(fingerAPath, fingerBPath);
}
private Sequence zoomSinglefinger(String fingerName, Point locus, int startRadius, int endRadius, double angle, Duration duration) {
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, fingerName);
Sequence fingerPath = new Sequence(finger, 0);
double midpointRadius = startRadius + (endRadius > startRadius ? 1 : -1) * 20;
// find coordinates for starting point of action (converting from polar coordinates to cartesian)
int fingerStartx = (int)Math.floor(locus.x + startRadius * Math.cos(angle));
int fingerStarty = (int)Math.floor(locus.y - startRadius * Math.sin(angle));
// find coordinates for first point that pingers move quickly to
int fingerMidx = (int)Math.floor(locus.x + (midpointRadius * Math.cos(angle)));
int fingerMidy = (int)Math.floor(locus.y - (midpointRadius * Math.sin(angle)));
// find coordinates for ending point of action (converting from polar coordinates to cartesian)
int fingerEndx = (int)Math.floor(locus.x + endRadius * Math.cos(angle));
int fingerEndy = (int)Math.floor(locus.y - endRadius * Math.sin(angle));
// move finger into start position
fingerPath.addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), fingerStartx, fingerStarty));
// finger comes down into contact with screen
fingerPath.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
// finger moves a small amount very quickly
fingerPath.addAction(finger.createPointerMove(Duration.ofMillis(1), PointerInput.Origin.viewport(), fingerMidx, fingerMidy));
// pause for a little bit
fingerPath.addAction(new Pause(finger, Duration.ofMillis(100)));
// finger moves to end position
fingerPath.addAction(finger.createPointerMove(duration, PointerInput.Origin.viewport(), fingerEndx, fingerEndy));
// finger lets up, off the screen
fingerPath.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
return fingerPath;
}
private void shootBird(AndroidDriver driver, WebElement birdEl, int xOffset, int yOffset) {
Rectangle rect = birdEl.getRect();
Point start = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
Point end = start.moveBy(xOffset, yOffset);
Duration dragDuration = Duration.ofMillis(750);
PointerInput finger = new PointerInput(Kind.TOUCH, "finger");
Sequence shoot = new Sequence(finger, 0);
shoot.addAction(finger.createPointerMove(Duration.ofMillis(0), Origin.viewport(), start.x, start.y));
shoot.addAction(finger.createPointerDown(MouseButton.LEFT.asArg()));
shoot.addAction(finger.createPointerMove(dragDuration, Origin.viewport(), end.x, end.y));
shoot.addAction(finger.createPointerUp(MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(shoot));
}
@Override
protected Map<String, ?> amendParameters(String name, Map<String, ?> parameters) {
// This blocks parent constructor from undesirable parameters amending
switch (name) {
case SEND_KEYS_TO_ACTIVE_ELEMENT:
Object rawValue = parameters.get("value");
//noinspection unchecked
Stream<CharSequence> source = (rawValue instanceof Collection)
? ((Collection<CharSequence>) rawValue).stream()
: Stream.of((CharSequence[]) rawValue);
String text = source
.flatMap(Stream::of)
.collect(Collectors.joining());
final KeyInput keyboard = new KeyInput("keyboard");
Sequence sequence = new Sequence(keyboard, 0);
for (int i = 0; i < text.length(); ++i) {
sequence.addAction(keyboard.createKeyDown(text.charAt(i)))
.addAction(keyboard.createKeyUp(text.charAt(i)));
}
return ImmutableMap.<String, Object>builder()
.put("actions", ImmutableList.of(sequence.toJson()))
.build();
case SEND_KEYS_TO_ELEMENT:
case SET_TIMEOUT:
return super.amendParameters(name, parameters);
default:
return parameters;
}
}
@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.");
}
private Collection<Sequence> zoomIn(Point locus, int distance) {
return zoom(locus, 200, 200 + distance, 45, Duration.ofMillis(25));
}
private Collection<Sequence> zoomOut(Point locus, int distance) {
return zoom(locus, 200 + distance, 200, 45, Duration.ofMillis(25));
}
@Override
public void perform(Collection<Sequence> actions) {
execute(DriverCommand.ACTIONS(actions));
}
static CommandPayload ACTIONS(Collection<Sequence> actions) {
return new CommandPayload(ACTIONS, ImmutableMap.of("actions", actions));
}