Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
add base action
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyitao committed Jul 17, 2020
1 parent 68b50e0 commit 3ef7484
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/main/java/com/daxiang/action/MobileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ public WebElement swipeToFindElement(String findBy, String value,
Point start = createPoint(startPoint, window);
Point end = createPoint(endPoint, window);

// 默认最多滑动3次
if (!StringUtils.hasText(maxSwipeCount)) {
maxSwipeCount = "3";
}
int count = parseInt(maxSwipeCount);
int count = StringUtils.hasText(maxSwipeCount) ? parseInt(maxSwipeCount) : 3;

for (int i = 1; i <= count; i++) {
log.info("[{}]滑动第{}次", mobileDevice.getId(), i);
Expand Down Expand Up @@ -160,11 +156,7 @@ public WebElement swipeInContainerToFindElement(WebElement container, String fin

Point[] points = createStartAndEndPointInContainer(container, startPoint, endPoint);

// 默认最多滑动3次
if (!StringUtils.hasText(maxSwipeCount)) {
maxSwipeCount = "3";
}
int count = parseInt(maxSwipeCount);
int count = StringUtils.hasText(maxSwipeCount) ? parseInt(maxSwipeCount) : 3;

for (int i = 1; i <= count; i++) {
log.info("[{}]容器内滑动第{}次", mobileDevice.getId(), i + 1);
Expand Down Expand Up @@ -251,24 +243,38 @@ public void asyncDismissAlert(String timeoutInSeconds, String once) {
*/
public WebElement clickByTouchAction(String findBy, String value) {
WebElement element = findElement(findBy, value);
PointOption center = getElementCenter(element);

new TouchAction(getAppiumDriver()).tap(center).perform();
return element;
}

/**
* 1012.长按元素
*/
public void longPressElement(WebElement element, String durationInMs) {
Assert.hasText(durationInMs, "durationInMs不能为空");

PointOption center = getElementCenter(element);
new TouchAction(getAppiumDriver())
.longPress(center)
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(parseLong(durationInMs))))
.release().perform();
}

private PointOption getElementCenter(WebElement element) {
Assert.notNull(element, "element不能为空");

Point leftTopPoint = element.getLocation();
Dimension dimension = element.getSize();

int x = leftTopPoint.x + dimension.width / 2;
int y = leftTopPoint.y + dimension.height / 2;
PointOption center = PointOption.point(x, y);

new TouchAction(getAppiumDriver()).tap(center).perform();
return element;
return PointOption.point(x, y);
}

private void swipe(Point start, Point end, String durationInMs) {
long duration = DEFAULT_SWIPE_DURATION_IN_MS;
if (!StringUtils.isEmpty(durationInMs)) {
duration = parseLong(durationInMs);
}

long duration = StringUtils.hasText(durationInMs) ? parseLong(durationInMs) : DEFAULT_SWIPE_DURATION_IN_MS;
new TouchAction(getAppiumDriver())
.press(PointOption.point(start))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
Expand All @@ -278,13 +284,10 @@ private void swipe(Point start, Point end, String durationInMs) {
}

private Point createPoint(String point, Dimension window) {
int screenWidth = window.width;
int screenHeight = window.height;

try {
JSONObject _point = JSONObject.parseObject(point.trim());
int x = (int) (_point.getFloat("x") * screenWidth);
int y = (int) (_point.getFloat("y") * screenHeight);
int x = (int) (_point.getFloat("x") * window.width);
int y = (int) (_point.getFloat("y") * window.height);

return new Point(x, y);
} catch (Exception e) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/daxiang/action/action.sql
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,25 @@ VALUES
'[1,2]'
);

-- 1012.longPressElement
INSERT INTO `action` (
`id`,
`name`,
`invoke`,
`return_value`,
`params`,
`platforms`
)
VALUES
(
1012,
'长按元素',
'$.longPressElement',
'void',
'[{"name":"element","type":"WebElement","description":""},{"name":"durationInMs","type":"String","description":"长按时间,单位: ms"}]',
'[1,2]'
);

-- 2000~2999 AndroidAction platforms = [1]

-- 2000.clearApkData
Expand Down

0 comments on commit 3ef7484

Please sign in to comment.