From f38976f1aeab0d1fdf9a05d072b88fd7b0a07a06 Mon Sep 17 00:00:00 2001 From: huangwh Date: Wed, 2 Aug 2023 21:01:44 +0800 Subject: [PATCH 1/2] feat(iOS): add doubleTap,rotate screen, active element --- .../org/cloud/sonic/driver/ios/IOSDriver.java | 31 +++++++ .../sonic/driver/ios/enums/Orientation.java | 43 ++++++++++ .../sonic/driver/ios/service/WdaClient.java | 8 ++ .../ios/service/impl/WdaClientImpl.java | 86 ++++++++++++++++++- .../cloud/sonic/driver/ios/IOSDriverTest.java | 27 +++++- 5 files changed, 190 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/cloud/sonic/driver/ios/enums/Orientation.java diff --git a/src/main/java/org/cloud/sonic/driver/ios/IOSDriver.java b/src/main/java/org/cloud/sonic/driver/ios/IOSDriver.java index 41d53d7..dab2413 100644 --- a/src/main/java/org/cloud/sonic/driver/ios/IOSDriver.java +++ b/src/main/java/org/cloud/sonic/driver/ios/IOSDriver.java @@ -174,6 +174,16 @@ public void tap(int x, int y) throws SonicRespException { performTouchAction(new TouchActions().press(x, y).release()); } + /** + * double tap position on screen + * @param x + * @param y + * @throws SonicRespException + */ + public void doubleTap(int x, int y) throws SonicRespException { + wdaClient.doubleTap(x,y); + } + /** * long press position on screen. * @@ -649,6 +659,15 @@ public List findElementList(String selector, String value, Integer r return wdaClient.findElementList(selector, value, retry, interval); } + /** + * get current active element + * @return + * @throws SonicRespException + */ + public IOSElement activeElement() throws SonicRespException{ + return wdaClient.activeElement(); + } + /** * get screenshot. * @@ -668,4 +687,16 @@ public byte[] screenshot() throws SonicRespException { public void setAppiumSettings(JSONObject settings) throws SonicRespException { wdaClient.setAppiumSettings(settings); } + + /** + * rotate screen orientation + * @throws SonicRespException + */ + public void rotate(Orientation orientation) throws SonicRespException { + wdaClient.rotate(orientation); + } + + public Orientation getRotate() throws SonicRespException { + return wdaClient.getRotate(); + } } diff --git a/src/main/java/org/cloud/sonic/driver/ios/enums/Orientation.java b/src/main/java/org/cloud/sonic/driver/ios/enums/Orientation.java new file mode 100644 index 0000000..8e60906 --- /dev/null +++ b/src/main/java/org/cloud/sonic/driver/ios/enums/Orientation.java @@ -0,0 +1,43 @@ +package org.cloud.sonic.driver.ios.enums; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + +import java.util.HashMap; + +public enum Orientation { + UNKNOWN(0), + PORTRAIT(1), + PORTRAITUPSIDEDOWN(2), + LANDSCAPELEFT(3), + LANDSCAPERIGHT(4); + + private final int orientation; + + Orientation(int orientation) { + this.orientation = orientation; + } + + public JSONObject getValue() { + JSONObject value = new JSONObject(); + value.put("x",0); + value.put("y",0); + switch (this.orientation) { + case 1: + value.put("z",0); + break; + case 2: + value.put("z",180); + break; + case 3: + value.put("z",90); + break; + case 4: + value.put("z",270); + break; + } + + return value; + } + +} diff --git a/src/main/java/org/cloud/sonic/driver/ios/service/WdaClient.java b/src/main/java/org/cloud/sonic/driver/ios/service/WdaClient.java index 003098c..b692f67 100644 --- a/src/main/java/org/cloud/sonic/driver/ios/service/WdaClient.java +++ b/src/main/java/org/cloud/sonic/driver/ios/service/WdaClient.java @@ -21,6 +21,7 @@ import org.cloud.sonic.driver.common.tool.Logger; import org.cloud.sonic.driver.common.tool.RespHandler; import org.cloud.sonic.driver.common.tool.SonicRespException; +import org.cloud.sonic.driver.ios.enums.Orientation; import org.cloud.sonic.driver.ios.models.TouchActions; import java.util.List; @@ -74,6 +75,8 @@ public interface WdaClient { //button handler. void pressButton(String buttonName) throws SonicRespException; + void doubleTap(int x, int y) throws SonicRespException; + //keyboard handler. void sendKeys(String text, Integer frequency) throws SonicRespException; @@ -103,6 +106,9 @@ public interface WdaClient { List findElementList(String selector, String value, Integer retry, Integer interval) throws SonicRespException; + // get current active element + IOSElement activeElement() throws SonicRespException; + //screen handler. byte[] screenshot() throws SonicRespException; @@ -110,4 +116,6 @@ public interface WdaClient { void setAppiumSettings(JSONObject settings) throws SonicRespException; void swipe(double fromX, double fromY, double toX, double toY, double duration) throws SonicRespException; + void rotate(Orientation orientation) throws SonicRespException; + Orientation getRotate() throws SonicRespException; } diff --git a/src/main/java/org/cloud/sonic/driver/ios/service/impl/WdaClientImpl.java b/src/main/java/org/cloud/sonic/driver/ios/service/impl/WdaClientImpl.java index aeb47b0..79ebeb9 100644 --- a/src/main/java/org/cloud/sonic/driver/ios/service/impl/WdaClientImpl.java +++ b/src/main/java/org/cloud/sonic/driver/ios/service/impl/WdaClientImpl.java @@ -26,15 +26,14 @@ import org.cloud.sonic.driver.common.tool.Logger; import org.cloud.sonic.driver.common.tool.RespHandler; import org.cloud.sonic.driver.common.tool.SonicRespException; +import org.cloud.sonic.driver.ios.enums.Orientation; import org.cloud.sonic.driver.ios.models.TouchActions; import org.cloud.sonic.driver.ios.service.IOSElement; import org.cloud.sonic.driver.ios.service.WdaClient; +import org.jsoup.select.CombiningEvaluator; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Base64; -import java.util.List; +import java.util.*; public class WdaClientImpl implements WdaClient { private String remoteUrl; @@ -231,6 +230,23 @@ public void pressButton(String buttonName) throws SonicRespException { } } + @Override + public void doubleTap(int x, int y) throws SonicRespException { + checkSessionId(); + JSONObject data = new JSONObject(); + data.put("x",x); + data.put("y",y); + + BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/session/" + sessionId + "/wda/doubleTap") + .body(data.toJSONString())); + if (b.getErr() == null) { + logger.info("double tap success. %s", data.toJSONString()); + } else { + logger.error("double tap failed."); + throw new SonicRespException(b.getErr().getMessage()); + } + } + @Override public void sendKeys(String text, Integer frequency) throws SonicRespException { checkSessionId(); @@ -475,6 +491,32 @@ public List findElementList(String selector, String value, Integer r return iosElementList; } + @Override + public IOSElement activeElement() throws SonicRespException { + checkSessionId(); + BaseResp b = respHandler.getResp( + HttpUtil.createGet(remoteUrl + "/session/" + sessionId + "/element/active")); + if (b.getErr() == null) { + logger.info("find active elements successful."); + JSONObject value = JSON.parseObject(b.getValue().toString(), JSONObject.class); + List identifier = Arrays.asList("ELEMENT", "element-6066-11e4-a52e-4f735466cecf"); + Iterator var4 = identifier.iterator(); + String eleId; + do { + if (!var4.hasNext()) { + return null; + } + String i = (String)var4.next(); + eleId = value.getString(i); + } while(eleId == null || eleId.length() <= 0); + + return new IOSElementImpl(eleId,this); + } else { + logger.error("active element not found."); + throw new SonicRespException(b.getErr().getMessage()); + } + } + @Override public byte[] screenshot() throws SonicRespException { checkSessionId(); @@ -529,4 +571,40 @@ public void swipe(double fromX, double fromY, double toX, double toY, double dur } } + @Override + public void rotate(Orientation orientation) throws SonicRespException { + JSONObject xyz = orientation.getValue(); + BaseResp b = respHandler.getResp(HttpUtil.createPost(remoteUrl + "/rotation").body(String.valueOf(xyz.toJSONString()))); + if (b.getErr() == null) { + logger.info("set orientation success. %s", xyz.toJSONString()); + } else { + logger.error("set orientation failed."); + throw new SonicRespException(b.getErr().getMessage()); + } + } + + @Override + public Orientation getRotate() throws SonicRespException { + BaseResp b = respHandler.getResp(HttpUtil.createGet(remoteUrl + "/rotation")); + if (b.getErr() == null) { + logger.info("get orientation %s.",b.getValue()); + JSONObject value = JSON.parseObject(b.getValue().toString(), JSONObject.class); + Integer zValue = Integer.valueOf(value.getString("z")); + switch (zValue) { + case 0: + return Orientation.PORTRAIT; + case 180: + return Orientation.PORTRAITUPSIDEDOWN; + case 90: + return Orientation.LANDSCAPELEFT; + case 270: + return Orientation.LANDSCAPERIGHT; + default: + return Orientation.UNKNOWN; + } + } else { + logger.error("get orientation failed."); + throw new SonicRespException(b.getErr().getMessage()); + } + } } diff --git a/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java b/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java index e4cc306..451c431 100644 --- a/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java +++ b/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java @@ -37,7 +37,7 @@ @RunWith(Parameterized.class) public class IOSDriverTest { static IOSDriver iosDriver; - static String url = "http://localhost:8100"; + static String url = "http://localhost:8827"; @Parameterized.Parameters public static Object[][] data() { @@ -312,6 +312,31 @@ public void testIsDisplayed() throws SonicRespException { System.out.println(element2.getUniquelyIdentifies() + ",rect=" + element2.getRect()); } + @Test + public void testDoubleTap() throws SonicRespException { + iosDriver.doubleTap(100,100); + iosDriver.pressButton("HOME"); + } + + @Test + public void testActiveElement() throws SonicRespException { + IOSElement element = iosDriver.activeElement(); + Assert.assertNotNull(element); + element.sendKeys("find active element"); + } + + @Test + public void testOrientation() throws SonicRespException, InterruptedException { + Orientation orientation = iosDriver.getRotate(); + System.out.print("current orientation: " + orientation); + + iosDriver.rotate(Orientation.LANDSCAPELEFT); + Thread.sleep(2000); + iosDriver.rotate(Orientation.LANDSCAPERIGHT); + Thread.sleep(2000); + iosDriver.rotate(Orientation.PORTRAIT); + } + @AfterClass public static void after() throws SonicRespException { iosDriver.closeDriver(); From b4137ffb4d290162819f0fd252a87c1a20694cfa Mon Sep 17 00:00:00 2001 From: Wally Huang Date: Thu, 3 Aug 2023 10:18:03 +0800 Subject: [PATCH 2/2] fix(iOS): reset wda local port to 8100 Signed-off-by: Wally Huang --- src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java b/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java index 451c431..c41aaf3 100644 --- a/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java +++ b/src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java @@ -37,7 +37,7 @@ @RunWith(Parameterized.class) public class IOSDriverTest { static IOSDriver iosDriver; - static String url = "http://localhost:8827"; + static String url = "http://localhost:8100"; @Parameterized.Parameters public static Object[][] data() {