Skip to content

Commit

Permalink
Merge pull request #1 from SonicCloudOrg/dev
Browse files Browse the repository at this point in the history
1.0.0
  • Loading branch information
ZhouYixun authored Jul 24, 2022
2 parents 7b77966 + e5a86e0 commit b668578
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.0.0-alpha</version>
<version>1.0.0</version>
</dependency>
```
### Gradle
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<dependency>
<groupId>io.github.soniccloudorg</groupId>
<artifactId>sonic-driver-core</artifactId>
<version>1.0.0-alpha</version>
<version>1.0.0</version>
</dependency>
```
### Gradle
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.0-alpha</version>
<version>1.0.0</version>

<name>sonic-driver-core</name>
<description>The Sonic Project UIAutomation Driver Core for Android, iOS, Windows, Mac and so on.</description>
Expand Down
55 changes: 52 additions & 3 deletions src/main/java/org/cloud/sonic/core/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,31 @@ public class IOSDriver {

/**
* Init ios driver
*
* @param url
* @throws SonicRespException
*/
public IOSDriver(String url) throws SonicRespException {
this(url, RespHandler.DEFAULT_REQUEST_TIMEOUT);
}

/**
* Init ios driver
*
* @param url
* @param timeOut
* @throws SonicRespException
*/
public IOSDriver(String url, int timeOut) throws SonicRespException {
wdaClient = new WdaClientImpl();
wdaClient.setRemoteUrl(url);
wdaClient.setGlobalTimeOut(timeOut);
wdaClient.newSession(new JSONObject());
}

/**
* Get wda client
*
* @return
*/
public WdaClient getWdaClient() {
Expand All @@ -51,6 +65,7 @@ public WdaClient getWdaClient() {

/**
* get wda sessionId
*
* @return
*/
public String getSessionId() {
Expand All @@ -59,47 +74,77 @@ public String getSessionId() {

/**
* destroy sessionId
*
* @throws SonicRespException
*/
public void closeDriver() throws SonicRespException {
wdaClient.closeSession();
}

/**
* get device lock status
* @return
* @throws SonicRespException
*/
public boolean isLocked() throws SonicRespException {
return wdaClient.isLocked();
}

/**
* lock device
* @throws SonicRespException
*/
public void lock() throws SonicRespException {
wdaClient.lock();
}

/**
* unlock device
* @throws SonicRespException
*/
public void unlock() throws SonicRespException {
wdaClient.unlock();
}

/**
* tap position on screen
*
* @param x
* @param y
* @throws SonicRespException
*/
public void tap(int x, int y) throws SonicRespException {
wdaClient.performTouchAction(new TouchActions().press(x,y).release());
wdaClient.performTouchAction(new TouchActions().press(x, y).release());
}

/**
* long press position on screen
*
* @param x
* @param y
* @param ms
* @throws SonicRespException
*/
public void longPress(int x, int y, int ms) throws SonicRespException {
wdaClient.performTouchAction(new TouchActions().press(x,y).wait(ms).release());
wdaClient.performTouchAction(new TouchActions().press(x, y).wait(ms).release());
}

/**
* swipe position on screen
*
* @param fromX
* @param fromY
* @param toX
* @param toY
* @throws SonicRespException
*/
public void swipe(int fromX, int fromY, int toX, int toY) throws SonicRespException {
wdaClient.performTouchAction(new TouchActions().press(fromX,fromY).wait(50).move(toX,toY).wait(10).release());
wdaClient.performTouchAction(new TouchActions().press(fromX, fromY).wait(50).move(toX, toY).wait(10).release());
}

/**
* perform touch action
*
* @param touchActions
* @throws SonicRespException
*/
Expand All @@ -109,6 +154,7 @@ public void performTouchAction(TouchActions touchActions) throws SonicRespExcept

/**
* press system button.
*
* @param systemButton
* @throws SonicRespException
*/
Expand All @@ -118,6 +164,7 @@ public void pressButton(SystemButton systemButton) throws SonicRespException {

/**
* send key without element
*
* @param text
* @throws SonicRespException
*/
Expand All @@ -127,6 +174,7 @@ public void sendKeys(String text) throws SonicRespException {

/**
* send key without element
*
* @param text
* @param frequency
* @throws SonicRespException
Expand All @@ -137,6 +185,7 @@ public void sendKeys(String text, int frequency) throws SonicRespException {

/**
* get page source
*
* @return
* @throws SonicRespException
*/
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/cloud/sonic/core/ios/RespHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
import java.util.Map;

public class RespHandler {
private static final int REQUEST_TIMEOUT = 15000;
public static final int DEFAULT_REQUEST_TIMEOUT = 15000;
private int requestTimeout = 15000;

public static BaseResp getResp(HttpRequest httpRequest) throws SonicRespException {
return getResp(httpRequest, REQUEST_TIMEOUT);
public void setRequestTimeOut(int timeOut) {
requestTimeout = timeOut;
}

public static BaseResp getResp(HttpRequest httpRequest, int timeout) throws SonicRespException {
public BaseResp getResp(HttpRequest httpRequest) throws SonicRespException {
return getResp(httpRequest, requestTimeout);
}

public BaseResp getResp(HttpRequest httpRequest, int timeout) throws SonicRespException {
synchronized (WdaClient.class) {
try {
return initResp(httpRequest.addHeaders(initHeader()).timeout(timeout).execute().body());
Expand All @@ -29,21 +34,21 @@ public static BaseResp getResp(HttpRequest httpRequest, int timeout) throws Soni
}
}

public static BaseResp initResp(String response) {
public BaseResp initResp(String response) {
if (response.contains("traceback")) {
return initErrorMsg(response);
} else {
return JSON.parseObject(response, BaseResp.class);
}
}

public static Map<String, String> initHeader() {
public Map<String, String> initHeader() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}

public static BaseResp initErrorMsg(String resp) {
public BaseResp initErrorMsg(String resp) {
BaseResp err = JSON.parseObject(resp, BaseResp.class);
ErrorMsg errorMsg = JSONObject.parseObject(err.getValue().toString(), ErrorMsg.class);
err.setErr(errorMsg);
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/cloud/sonic/core/ios/service/WdaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* wda client interface
*/
public interface WdaClient {
//Client Setting
void setGlobalTimeOut(int timeOut);

//Session handler.
String getRemoteUrl();
Expand All @@ -39,8 +41,14 @@ public interface WdaClient {

void closeSession() throws SonicRespException;

//perform handler.
//lock handler.
boolean isLocked() throws SonicRespException;

void lock() throws SonicRespException;

void unlock() throws SonicRespException;

//perform handler.
void performTouchAction(TouchActions touchActions) throws SonicRespException;

//button handler.
Expand All @@ -49,10 +57,6 @@ public interface WdaClient {
//keyboard handler.
void sendKeys(String text, Integer frequency) throws SonicRespException;

void setPasteboard(String contentType, String content) throws SonicRespException;

byte[] getPasteboard(String contentType) throws SonicRespException;

//source
String pageSource() throws SonicRespException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
import org.cloud.sonic.core.ios.service.WdaClient;
import org.cloud.sonic.core.tool.SonicRespException;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Slf4j
public class WdaClientImpl implements WdaClient {
private String remoteUrl;
private String sessionId;
private RespHandler respHandler = new RespHandler();

private void checkSessionId() throws SonicRespException {
if (sessionId == null || sessionId.length() == 0) {
Expand All @@ -40,6 +44,11 @@ private void checkSessionId() throws SonicRespException {
}
}

@Override
public void setGlobalTimeOut(int timeOut) {
respHandler.setRequestTimeOut(timeOut);
}

@Override
public String getRemoteUrl() {
return remoteUrl;
Expand All @@ -64,7 +73,7 @@ public void setSessionId(String sessionId) {
public void newSession(JSONObject capabilities) throws SonicRespException {
JSONObject data = new JSONObject();
data.put("capabilities", capabilities);
BaseResp b = RespHandler.getResp(HttpUtil.createPost(remoteUrl + "/session").body(data.toJSONString()));
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session").body(data.toJSONString()));
if (b.getErr() == null) {
SessionInfo sessionInfo = JSON.parseObject(b.getValue().toString(), SessionInfo.class);
setSessionId(sessionInfo.getSessionId());
Expand All @@ -78,14 +87,51 @@ public void newSession(JSONObject capabilities) throws SonicRespException {
@Override
public void closeSession() throws SonicRespException {
checkSessionId();
RespHandler.getResp(HttpUtil.createRequest(Method.DELETE, remoteUrl + "/session/" + sessionId));
respHandler.getResp(HttpUtil.createRequest(Method.DELETE, remoteUrl + "/session/" + sessionId));
log.info("close session successful!");
}

@Override
public boolean isLocked() throws SonicRespException {
checkSessionId();
BaseResp b = respHandler.getResp(HttpUtil.createGet(remoteUrl + "/session/" + sessionId + "/wda/locked"));
if (b.getErr() == null) {
log.info("device lock status: {}.", b.getValue());
return (boolean) b.getValue();
} else {
log.error("get device lock status failed.");
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void lock() throws SonicRespException {
checkSessionId();
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/lock"));
if (b.getErr() == null) {
log.info("lock device.");
} else {
log.error("lock device failed.");
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void unlock() throws SonicRespException {
checkSessionId();
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/unlock"));
if (b.getErr() == null) {
log.info("unlock device.");
} else {
log.error("unlock device failed.");
throw new SonicRespException(b.getErr().getMessage());
}
}

@Override
public void performTouchAction(TouchActions touchActions) throws SonicRespException {
checkSessionId();
BaseResp b = RespHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/touch/multi/perform")
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/touch/multi/perform")
.body(String.valueOf(JSONObject.toJSON(touchActions))));
if (b.getErr() == null) {
log.info("perform action {}.", touchActions);
Expand All @@ -100,7 +146,7 @@ public void pressButton(String buttonName) throws SonicRespException {
checkSessionId();
JSONObject data = new JSONObject();
data.put("name", buttonName);
BaseResp b = RespHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/pressButton")
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/pressButton")
.body(data.toJSONString()));
if (b.getErr() == null) {
log.info("press button {} .", buttonName);
Expand All @@ -116,7 +162,7 @@ public void sendKeys(String text, Integer frequency) throws SonicRespException {
JSONObject data = new JSONObject();
data.put("value", text.split(""));
data.put("frequency", frequency);
BaseResp b = RespHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/keys")
BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/keys")
.body(data.toJSONString()));
if (b.getErr() == null) {
log.info("send key {} .", text);
Expand All @@ -126,20 +172,10 @@ public void sendKeys(String text, Integer frequency) throws SonicRespException {
}
}

@Override
public void setPasteboard(String contentType, String content) throws SonicRespException {

}

@Override
public byte[] getPasteboard(String contentType) throws SonicRespException {
return new byte[0];
}

@Override
public String pageSource() throws SonicRespException {
checkSessionId();
BaseResp b = RespHandler.getResp(HttpUtil.createGet(remoteUrl + "/session/" + sessionId + "/source"), 60000);
BaseResp b = respHandler.getResp(HttpUtil.createGet(remoteUrl + "/session/" + sessionId + "/source"), 60000);
if (b.getErr() == null) {
log.info("get page source.");
return b.getValue().toString();
Expand Down
Loading

0 comments on commit b668578

Please sign in to comment.