Skip to content

Commit

Permalink
Merge pull request #7 from SonicCloudOrg/feat/add_element_clear_rect
Browse files Browse the repository at this point in the history
feat: add clear
  • Loading branch information
ZhouYixun authored Jul 31, 2022
2 parents 0237754 + 58ed3b1 commit 7fb9228
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ sonic-driver-core can be separated from appium and interact directly with webdri
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
</dependency>
```

#### Gradle

```
implementation 'io.github.soniccloudorg:sonic-driver-core:1.0.5'
implementation 'io.github.soniccloudorg:sonic-driver-core:1.0.6'
```

### Code
Expand Down
4 changes: 2 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ sonic-driver-core可以脱离Appium,直接与WebDriverAgent或UIautomator2交
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
</dependency>
```
#### Gradle
```
implementation 'io.github.soniccloudorg:sonic-driver-core:1.0.5'
implementation 'io.github.soniccloudorg:sonic-driver-core:1.0.6'
```

### 代码
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>

<name>sonic-driver-core</name>
<description>The Sonic Project UIAutomation Driver Core for Android, iOS, Windows, Mac and so on.</description>
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/org/cloud/sonic/core/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.cloud.sonic.core.ios.service.impl.WdaClientImpl;
import org.cloud.sonic.core.tool.SonicRespException;

import java.util.List;

/**
* @author Eason
* ios driver
Expand Down Expand Up @@ -459,4 +461,118 @@ public WebElement findElement(XCUIElementType xcuiElementType, Integer retry, In
public WebElement findElement(String selector, String value, Integer retry, Integer interval) throws SonicRespException {
return wdaClient.findElement(selector, value, retry, interval);
}

/**
* find element list in device.
*
* @param iosSelector
* @param value
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(IOSSelector iosSelector, String value) throws SonicRespException {
return findElementList(iosSelector, value, null);
}

/**
* find element list in device.
*
* @param xcuiElementType
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(XCUIElementType xcuiElementType) throws SonicRespException {
return findElementList(xcuiElementType, null);
}

/**
* find element list in device.
*
* @param selector
* @param value
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(String selector, String value) throws SonicRespException {
return findElementList(selector, value, null);
}

/**
* find element list in device.
*
* @param iosSelector
* @param value
* @param retry
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(IOSSelector iosSelector, String value, Integer retry) throws SonicRespException {
return findElementList(iosSelector, value, retry, null);
}

/**
* find element list in device.
*
* @param xcuiElementType
* @param retry
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(XCUIElementType xcuiElementType, Integer retry) throws SonicRespException {
return findElementList(xcuiElementType, retry, null);
}

/**
* find element list in device.
*
* @param selector
* @param value
* @param retry
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(String selector, String value, Integer retry) throws SonicRespException {
return findElementList(selector, value, retry, null);
}

/**
* find element list in device.
*
* @param iosSelector
* @param value
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(IOSSelector iosSelector, String value, Integer retry, Integer interval) throws SonicRespException {
return findElementList(iosSelector.getSelector(), value, retry, interval);
}

/**
* find element list in device.
*
* @param xcuiElementType
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(XCUIElementType xcuiElementType, Integer retry, Integer interval) throws SonicRespException {
return findElementList(IOSSelector.CLASS_NAME.getSelector(), xcuiElementType.getType(), retry, interval);
}

/**
* find element list in device.
*
* @param selector
* @param value
* @param retry
* @param interval
* @return
* @throws SonicRespException
*/
public List<WebElement> findElementList(String selector, String value, Integer retry, Integer interval) throws SonicRespException {
return wdaClient.findElementList(selector, value, retry, interval);
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/cloud/sonic/core/ios/models/IOSRect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) [SonicCloudOrg] Sonic Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cloud.sonic.core.ios.models;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@AllArgsConstructor
public class IOSRect {
private int x;
private int y;
private int width;
private int height;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@AllArgsConstructor
public class SessionInfo {
private String sessionId;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/cloud/sonic/core/ios/service/WdaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.cloud.sonic.core.ios.models.TouchActions;
import org.cloud.sonic.core.tool.SonicRespException;

import java.util.List;

/**
* @author Eason
* wda client interface
Expand Down Expand Up @@ -83,4 +85,7 @@ public interface WdaClient {

//element handler.
WebElement findElement(String selector, String value, Integer retry, Integer interval) throws SonicRespException;

//element handler.
List<WebElement> findElementList(String selector, String value, Integer retry, Integer interval) throws SonicRespException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.cloud.sonic.core.ios.service;

import org.cloud.sonic.core.ios.models.IOSRect;
import org.cloud.sonic.core.tool.SonicRespException;

/**
Expand All @@ -29,4 +30,10 @@ public interface WebElement {
void sendKeys(String text) throws SonicRespException;

void sendKeys(String text,int frequency) throws SonicRespException;

void clear() throws SonicRespException;

String getText() throws SonicRespException;

IOSRect getRect() throws SonicRespException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.cloud.sonic.core.tool.SonicRespException;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
Expand Down Expand Up @@ -371,4 +372,50 @@ public WebElement findElement(String selector, String value, Integer retry, Inte
}
return webElement;
}

@Override
public List<WebElement> findElementList(String selector, String value, Integer retry, Integer interval) throws SonicRespException {
List<WebElement> webElementList = new ArrayList<>();
int wait = 0;
int intervalInit = (interval == null ? FIND_ELEMENT_INTERVAL : interval);
int retryInit = (retry == null ? FIND_ELEMENT_RETRY : retry);
String errMsg = "";
while (wait < retryInit) {
wait++;
checkSessionId();
JSONObject data = new JSONObject();
data.put("using", selector);
data.put("value", value);
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/elements")
.body(data.toJSONString()));
if (b.getErr() == null) {
log.info("find elements successful.");
List<JSONObject> ids = JSON.parseObject(b.getValue().toString(), ArrayList.class);
for (JSONObject ele : ids) {
String id = parseElementId(ele);
if (id.length() > 0) {
webElementList.add(new WebElementImpl(id, this));
} else {
log.error("parse element id {} failed.", ele);
continue;
}
}
break;
} else {
log.error("elements not found. retried {} times, retry in {} ms.", wait, intervalInit);
errMsg = b.getErr().getMessage();
}
if (wait < retryInit) {
try {
Thread.sleep(intervalInit);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (webElementList.size() == 0) {
throw new SonicRespException(errMsg);
}
return webElementList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.cloud.sonic.core.ios.models.BaseResp;
import org.cloud.sonic.core.ios.models.IOSRect;
import org.cloud.sonic.core.ios.service.WdaClient;
import org.cloud.sonic.core.ios.service.WebElement;
import org.cloud.sonic.core.tool.SonicRespException;
Expand Down Expand Up @@ -61,12 +63,58 @@ public void sendKeys(String text, int frequency) throws SonicRespException {
data.put("frequency", frequency);
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createPost(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/value"));
+ wdaClient.getSessionId() + "/element/" + id + "/value")
.body(data.toJSONString()), 60000);
if (b.getErr() == null) {
log.info("send key to {}.", id);
} else {
log.error("send key to {} failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void clear() throws SonicRespException {
wdaClient.checkSessionId();
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createPost(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/clear"), 60000);
if (b.getErr() == null) {
log.info("clear {}.", id);
} else {
log.error("clear {} failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public String getText() throws SonicRespException {
wdaClient.checkSessionId();
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createGet(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/text"));
if (b.getErr() == null) {
log.info("get {} text {}.", id, b.getValue());
return b.getValue().toString();
} else {
log.error("get {} text failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public IOSRect getRect() throws SonicRespException {
wdaClient.checkSessionId();
BaseResp b = wdaClient.getRespHandler().getResp(
HttpUtil.createGet(wdaClient.getRemoteUrl() + "/session/"
+ wdaClient.getSessionId() + "/element/" + id + "/rect"));
if (b.getErr() == null) {
log.info("get {} rect {}.", id, b.getValue());
IOSRect iosRect = JSON.parseObject(b.getValue().toString(),IOSRect.class);
return iosRect;
} else {
log.error("get {} rect failed.", id);
throw new SonicRespException(b.getErr().getMessage());
}
}
}
Loading

0 comments on commit 7fb9228

Please sign in to comment.