Skip to content

Commit

Permalink
Merge pull request #101 from huang1988519/main
Browse files Browse the repository at this point in the history
feat(iOS): add doubleTap,rotate screen, active element
  • Loading branch information
ZhouYixun committed Aug 3, 2023
2 parents c778ad9 + b4137ff commit a663b55
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/cloud/sonic/driver/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -649,6 +659,15 @@ public List<IOSElement> 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.
*
Expand All @@ -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();
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/cloud/sonic/driver/ios/enums/Orientation.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -103,11 +106,16 @@ public interface WdaClient {

List<IOSElement> 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;

//appium setting handler.
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -475,6 +491,32 @@ public List<IOSElement> 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<String> 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();
Expand Down Expand Up @@ -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());
}
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/cloud/sonic/driver/ios/IOSDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a663b55

Please sign in to comment.