From 738df0dd17ed677f355392a3d732608c09a2b971 Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Sun, 24 Jul 2022 13:16:31 +0800 Subject: [PATCH 1/2] 1.0.0 --- pom.xml | 2 +- .../org/cloud/sonic/core/ios/IOSDriver.java | 55 ++++++++++++++- .../org/cloud/sonic/core/ios/RespHandler.java | 19 ++++-- .../sonic/core/ios/service/WdaClient.java | 14 ++-- .../core/ios/service/impl/WdaClientImpl.java | 68 ++++++++++++++----- .../cloud/sonic/core/ios/IOSDriverTest.java | 10 ++- 6 files changed, 135 insertions(+), 33 deletions(-) diff --git a/pom.xml b/pom.xml index ab5e1bf..f98e6ee 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.soniccloudorg sonic-driver-core - 1.0.0-alpha + 1.0.0 sonic-driver-core The Sonic Project UIAutomation Driver Core for Android, iOS, Windows, Mac and so on. diff --git a/src/main/java/org/cloud/sonic/core/ios/IOSDriver.java b/src/main/java/org/cloud/sonic/core/ios/IOSDriver.java index c2cd9da..bf772b8 100644 --- a/src/main/java/org/cloud/sonic/core/ios/IOSDriver.java +++ b/src/main/java/org/cloud/sonic/core/ios/IOSDriver.java @@ -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() { @@ -51,6 +65,7 @@ public WdaClient getWdaClient() { /** * get wda sessionId + * * @return */ public String getSessionId() { @@ -59,35 +74,64 @@ 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 @@ -95,11 +139,12 @@ public void longPress(int x, int y, int ms) throws SonicRespException { * @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 */ @@ -109,6 +154,7 @@ public void performTouchAction(TouchActions touchActions) throws SonicRespExcept /** * press system button. + * * @param systemButton * @throws SonicRespException */ @@ -118,6 +164,7 @@ public void pressButton(SystemButton systemButton) throws SonicRespException { /** * send key without element + * * @param text * @throws SonicRespException */ @@ -127,6 +174,7 @@ public void sendKeys(String text) throws SonicRespException { /** * send key without element + * * @param text * @param frequency * @throws SonicRespException @@ -137,6 +185,7 @@ public void sendKeys(String text, int frequency) throws SonicRespException { /** * get page source + * * @return * @throws SonicRespException */ diff --git a/src/main/java/org/cloud/sonic/core/ios/RespHandler.java b/src/main/java/org/cloud/sonic/core/ios/RespHandler.java index bf7effb..2830056 100644 --- a/src/main/java/org/cloud/sonic/core/ios/RespHandler.java +++ b/src/main/java/org/cloud/sonic/core/ios/RespHandler.java @@ -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()); @@ -29,7 +34,7 @@ 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 { @@ -37,13 +42,13 @@ public static BaseResp initResp(String response) { } } - public static Map initHeader() { + public Map initHeader() { Map 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); diff --git a/src/main/java/org/cloud/sonic/core/ios/service/WdaClient.java b/src/main/java/org/cloud/sonic/core/ios/service/WdaClient.java index 6da42ed..daba27e 100644 --- a/src/main/java/org/cloud/sonic/core/ios/service/WdaClient.java +++ b/src/main/java/org/cloud/sonic/core/ios/service/WdaClient.java @@ -25,6 +25,8 @@ * wda client interface */ public interface WdaClient { + //Client Setting + void setGlobalTimeOut(int timeOut); //Session handler. String getRemoteUrl(); @@ -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. @@ -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; } diff --git a/src/main/java/org/cloud/sonic/core/ios/service/impl/WdaClientImpl.java b/src/main/java/org/cloud/sonic/core/ios/service/impl/WdaClientImpl.java index e497dee..71e145f 100644 --- a/src/main/java/org/cloud/sonic/core/ios/service/impl/WdaClientImpl.java +++ b/src/main/java/org/cloud/sonic/core/ios/service/impl/WdaClientImpl.java @@ -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) { @@ -40,6 +44,11 @@ private void checkSessionId() throws SonicRespException { } } + @Override + public void setGlobalTimeOut(int timeOut) { + respHandler.setRequestTimeOut(timeOut); + } + @Override public String getRemoteUrl() { return remoteUrl; @@ -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()); @@ -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); @@ -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); @@ -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); @@ -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(); diff --git a/src/test/java/org/cloud/sonic/core/ios/IOSDriverTest.java b/src/test/java/org/cloud/sonic/core/ios/IOSDriverTest.java index 3ba874e..14941cc 100644 --- a/src/test/java/org/cloud/sonic/core/ios/IOSDriverTest.java +++ b/src/test/java/org/cloud/sonic/core/ios/IOSDriverTest.java @@ -29,7 +29,7 @@ public class IOSDriverTest { @Parameterized.Parameters public static Object[][] data() { - return new Object[1][0]; + return new Object[10][0]; } @BeforeClass @@ -91,6 +91,14 @@ public void testGetPageSource() throws SonicRespException { Assert.assertTrue(iosDriver.getPageSource().contains("XCUIElementTypeApplication")); } + @Test + public void testLock() throws SonicRespException { + iosDriver.lock(); + Assert.assertTrue(iosDriver.isLocked()); + iosDriver.unlock(); + Assert.assertFalse(iosDriver.isLocked()); + } + @AfterClass public static void after() throws SonicRespException { iosDriver.closeDriver(); From e5a86e01dedd4692c467e38f038836b15aeb3c96 Mon Sep 17 00:00:00 2001 From: ZhouYixun <291028775@qq.com> Date: Sun, 24 Jul 2022 13:17:09 +0800 Subject: [PATCH 2/2] 1.0.0 --- README.md | 2 +- README_CN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4b191a2..94218bc 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ io.github.soniccloudorg sonic-driver-core - 1.0.0-alpha + 1.0.0 ``` ### Gradle diff --git a/README_CN.md b/README_CN.md index 82a03be..46af4f7 100644 --- a/README_CN.md +++ b/README_CN.md @@ -41,7 +41,7 @@ io.github.soniccloudorg sonic-driver-core - 1.0.0-alpha + 1.0.0 ``` ### Gradle