Skip to content

Commit

Permalink
Add Android key code support for T2C (#166)
Browse files Browse the repository at this point in the history
1. Add Android key code support for T2C
2. Attach step index in log and error message to improve debuggability
  • Loading branch information
taoran6 authored Nov 29, 2022
1 parent 9c2cc55 commit 8b7fcc3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public static void doAction(BaseDriverController driver, ActionInfo actionInfo)
} catch (Exception e) {
e.printStackTrace();
if (!isOption) {
throw e;
int index = actionInfo.getId();
throw new IllegalStateException("Failed at step " + index + ": " + e.getMessage(), e);
}
}
}
Expand All @@ -73,7 +74,6 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
driver.click(webElement);
break;
case "tap":
//wait 3s before and after the tap action
int x = (Integer) arguments.get("x");
int y = (Integer) arguments.get("y");
driver.tap(x, y);
Expand Down Expand Up @@ -118,6 +118,10 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
case "home":
driver.pressKey(AndroidKey.HOME);
break;
case "pressKeyCode":
String keyCode = arguments.get("keyCode") + "";
driver.pressKeyCode(keyCode);
break;
case "move":
Object xVector = arguments.get("xVector");
Object yVector = arguments.get("yVector");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class AndroidDriverController extends BaseDriverController {
private final AndroidDriver androidDriver;
Expand Down Expand Up @@ -53,6 +55,13 @@ public void pressKey(AndroidKey key) {
androidDriver.pressKey(new KeyEvent(key));
}

@Override
public void pressKeyCode(String keyCode) {
List<String> keyEventArgs = Arrays.asList("keyevent", keyCode);
Map<String, Object> keyEventCmd = ImmutableMap.of("command", "input", "args", keyEventArgs);
androidDriver.executeScript("mobile: shell", keyEventCmd);
}

@Override
public void scroll(WebElement webElement, int xVector, int yVector) {
Point location = webElement.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public void terminateApp(String appPackageName) {
public void pressKey(AndroidKey key) {
}

public void pressKeyCode(String keyCode) {
}

public void scroll(WebElement webElement, int xVector, int yVector) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public void pressKey(AndroidKey key) {
logger.info("Called " + currentMethodName());
}

public void pressKeyCode(String keyCode) {
logger.info("Called " + currentMethodName());
}

public void scroll(WebElement webElement, int xVector, int yVector) {
logger.info("Called " + currentMethodName());
}
Expand Down

0 comments on commit 8b7fcc3

Please sign in to comment.