Skip to content

Commit

Permalink
Deprecate index, prefer description in T2C (#290)
Browse files Browse the repository at this point in the history
* feat: deprecate index, prefer description

* feat: log error step with description

* feat: improve code style

* feat: fix test case
  • Loading branch information
TedaLIEz committed Feb 22, 2023
1 parent c87d141 commit 947ec60
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,47 @@
package com.microsoft.hydralab.t2c.runner;

import com.microsoft.hydralab.t2c.runner.elements.BaseElementInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import javax.annotation.Nullable;

public class ActionInfo {
private final Integer id;
private final BaseElementInfo testElement;
private final String actionType;
private final String driverId;
private Map<String, Object> arguments;

private final String description;
private final Map<String, Object> arguments;
private final boolean isOptional;

public ActionInfo(Integer id, @Nullable BaseElementInfo testElement, String actionType, Map<String, Object> arguments,
String driverId, boolean isOptional) {
public ActionInfo(@Deprecated int id,
@Nullable BaseElementInfo testElement,
@NotNull String actionType,
@NotNull Map<String, Object> arguments,
@NotNull String driverId,
@NotNull String description,
boolean isOptional) {
this.id = id;
this.testElement = testElement;
this.actionType = actionType;
this.driverId = driverId;
this.isOptional = isOptional;
if (arguments != null) {
this.arguments = arguments;
}
this.description = description;
this.arguments = arguments;
}

@Deprecated
public Integer getId() {
return id;
}


public String getDescription() {
return description;
}

public BaseElementInfo getTestElement() {
return testElement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public static void doAction(@NotNull BaseDriverController driver, @NotNull Actio
} catch (Exception e) {
e.printStackTrace();
int index = actionInfo.getId();
logger.error("doAction at step " + index + "with exception: " + e.getMessage());
String description = actionInfo.getDescription();
logger.error("doAction at " + index + ", description: " + description + ", with exception: " + e.getMessage());
if (!isOption) {
throw new IllegalStateException("Failed at step " + index + ": " + e.getMessage()
throw new IllegalStateException("Failed at " + index + ", description: " + description + ", " + e.getMessage()
+ ", page source: \n" + driver.webDriver.getPageSource(), e);
}
}
Expand Down Expand Up @@ -92,7 +93,7 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
content = (String) arguments.get("content");
}
if (content == null) {
throw new IllegalArgumentException("Trying to input a null String. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Trying to input a null String. action index: " + actionInfo.getId());
}
if (webElement == null) {
driver.sendKeys(content);
Expand All @@ -106,14 +107,14 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
case "activateApp":
String appPackageName = (String) arguments.get("appPackageName");
if (appPackageName == null) {
throw new IllegalArgumentException("App package name should not be null. Please add argument 'appPackageName' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("App package name should not be null. Please add argument 'appPackageName' in the json. action index: " + actionInfo.getId());
}
driver.activateApp(appPackageName);
break;
case "terminateApp":
String removeAppPackageName = (String) arguments.get("appPackageName");
if (removeAppPackageName == null) {
throw new IllegalArgumentException("App package name should not be null. Please add argument 'appPackageName' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("App package name should not be null. Please add argument 'appPackageName' in the json. action index: " + actionInfo.getId());
}
driver.terminateApp(removeAppPackageName);
break;
Expand All @@ -131,7 +132,7 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
Object xVector = arguments.get("xVector");
Object yVector = arguments.get("yVector");
if (xVector == null || yVector == null) {
throw new IllegalArgumentException("Destination is not defined. Please add argument 'xVector' and 'yVector' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Destination is not defined. Please add argument 'xVector' and 'yVector' in the json. action index: " + actionInfo.getId());
}
int xVectorInt = xVector instanceof Integer ? (Integer) xVector : Integer.getInteger((String) xVector);
int yVectorInt = yVector instanceof Integer ? (Integer) yVector : Integer.getInteger((String) yVector);
Expand All @@ -140,14 +141,14 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
case "swipe":
String direction = (String) arguments.get("direction");
if (direction == null) {
throw new IllegalArgumentException("Direction is not defined. Please add argument 'direction' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Direction is not defined. Please add argument 'direction' in the json. action index: " + actionInfo.getId());
}
driver.swipe(direction);
break;
case "longClick":
Object durationObj = arguments.get("duration");
if (durationObj == null) {
throw new IllegalArgumentException("Duration is not defined. Please add argument 'duration' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Duration is not defined. Please add argument 'duration' in the json. action index: " + actionInfo.getId());
}
int duration = durationObj instanceof Integer ? (Integer) durationObj : Integer.getInteger((String) durationObj);
driver.longClick(duration, webElement);
Expand All @@ -156,7 +157,7 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
String attribute = (String) arguments.get("attribute");
String expectedValue = (String) arguments.get("expectedValue");
if (attribute == null || expectedValue == null) {
throw new IllegalArgumentException("Assert info is not defined. Please add argument 'attribute' and 'expectedValue' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Assert info is not defined. Please add argument 'attribute' and 'expectedValue' in the json. action index: " + actionInfo.getId());
}
driver.assertElementAttribute(webElement, attribute, expectedValue);
break;
Expand All @@ -169,7 +170,7 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
String attributeKey = (String) arguments.get("attribute");
String id = (String) arguments.get("id");
if (attributeKey == null || id == null) {
throw new IllegalArgumentException("Assert info is not defined. Please add argument 'attribute' and 'id' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Assert info is not defined. Please add argument 'attribute' and 'id' in the json. action index: " + actionInfo.getId());
}
String info = driver.getInfo(webElement, attributeKey);
keyToInfoMap.put(id, info);
Expand All @@ -191,19 +192,19 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
} else if (driver instanceof EdgeDriverController) {
toElementInfo = JSON.parseObject(toElementStr, EdgeElementInfo.class);
} else {
throw new IllegalArgumentException("Fail to parse the 'toElement' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Fail to parse the 'toElement' in the json. action index: " + actionInfo.getId());
}
WebElement toElement = findElement(driver, toElementInfo, logger);
driver.dragAndDrop(webElement, toElement);
} else {
throw new IllegalArgumentException("Destination is not defined. Please add argument 'xVector' & 'yVector' or 'toElement' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Destination is not defined. Please add argument 'xVector' & 'yVector' or 'toElement' in the json. action index: " + actionInfo.getId());
}
break;
case "switchToUrl":
String url = (String) arguments.get("url");
driver.switchToUrl(url);
if (url == null) {
throw new IllegalArgumentException("Url is not defined. Please add argument 'url' and 'id' in the json. actionId: " + actionInfo.getId());
throw new IllegalArgumentException("Url is not defined. Please add argument 'url' and 'id' in the json. action index: " + actionInfo.getId());
}
break;
case "copy":
Expand All @@ -221,7 +222,7 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
throw new IllegalStateException("action fail" +
"" +
"" +
"ed. actionId:" + actionInfo.getId() + "/t" + "actionType:" + actionInfo.getActionType());
"ed. action index:" + actionInfo.getId() + "/t" + "actionType:" + actionInfo.getActionType());

}
// Safe wait if no element required after doing this action to ensure the action is finished
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.*;

public class T2CJsonParser {
private Map<String, String> driveIdToTypeMap = new HashMap<>();
private final Map<String, String> driveIdToTypeMap = new HashMap<>();
private final Logger logger;

public T2CJsonParser(Logger logger) {
Expand All @@ -37,8 +37,8 @@ private TestInfo parseJson(String json) {

private ArrayList<DriverInfo> getDriverList(JSONArray driverJsonArray) {
ArrayList<DriverInfo> driverList = new ArrayList<>();
for (Iterator iterator = driverJsonArray.iterator(); iterator.hasNext(); ) {
JSONObject driverJsonObject = (JSONObject) iterator.next();
for (Object driverJson : driverJsonArray) {
JSONObject driverJsonObject = (JSONObject) driverJson;
logger.info("Driver: " + driverJsonObject.toJSONString());
String id = driverJsonObject.getString("id");
String platform = driverJsonObject.getString("platform");
Expand All @@ -65,52 +65,41 @@ private ArrayList<ActionInfo> getActionList(JSONArray caseJsonArray) {
WindowsElementInfo windowsElement = null;
EdgeElementInfo edgeElement = null;

for (Iterator iterator = caseJsonArray.iterator(); iterator.hasNext(); ) {
JSONObject caseJsonObject = (JSONObject) iterator.next();

Integer id = caseJsonObject.getInteger("index");
for (int i = 0; i < caseJsonArray.size(); i++) {
JSONObject caseJsonObject = caseJsonArray.getJSONObject(i);
String driverId = caseJsonObject.getString("driverId");

String description = "";
if (caseJsonObject.containsKey("description")) {
description = caseJsonObject.getString("description");
}
JSONObject elementInfo = caseJsonObject.getJSONObject("elementInfo");
//get element:android/ios/windows/web

JSONObject action = caseJsonObject.getJSONObject("action");
logger.info("Action: " + action.toJSONString());
String actionType = action.getString("actionType");
Map<String, Object> arguments = (Map<String, Object>) action.getJSONObject("arguments");
Map<String, Object> arguments = action.getJSONObject("arguments");
boolean isOptional = caseJsonObject.containsKey("isOptional") ? caseJsonObject.getBoolean("isOptional") :
caseJsonObject.containsKey("isOption") ? caseJsonObject.getBoolean("isOption") :
false;

if (elementInfo != null && !elementInfo.isEmpty()) {
if (driveIdToTypeMap.get(driverId).equals("android")) {
androidElement = AndroidElementInfo.getAndroidElementFromJson(elementInfo);
actionInfo = new ActionInfo(id, androidElement, actionType, arguments, driverId, isOptional);
actionInfo = new ActionInfo(i, androidElement, actionType, arguments, driverId, description, isOptional);
}
if (driveIdToTypeMap.get(driverId).equals("windows")) {
windowsElement = JSON.parseObject(caseJsonObject.getString("elementInfo"), WindowsElementInfo.class);
actionInfo = new ActionInfo(id, windowsElement, actionType, arguments, driverId, isOptional);
actionInfo = new ActionInfo(i, windowsElement, actionType, arguments, driverId, description, isOptional);
}
if (driveIdToTypeMap.get(driverId).equals("browser")) {
edgeElement = JSON.parseObject(caseJsonObject.getString("elementInfo"), EdgeElementInfo.class);
actionInfo = new ActionInfo(id, edgeElement, actionType, arguments, driverId, isOptional);
actionInfo = new ActionInfo(i, edgeElement, actionType, arguments, driverId, description, isOptional);
}
}else {
actionInfo = new ActionInfo(id, null, actionType, arguments, driverId, isOptional);
} else {
actionInfo = new ActionInfo(i, null, actionType, arguments, driverId, description, isOptional);
}
caseList.add(actionInfo);
Comparator<ActionInfo> comparator = (o1, o2) -> {
if (Objects.equals(o1.getId(), o2.getId())) {
throw new RuntimeException("Same Index Found In The Action Info");
}
if (o1.getId() > o2.getId()) {
return 1;
} else {
return -1;
}
};

caseList.sort(comparator);
}

return caseList;
Expand Down
16 changes: 8 additions & 8 deletions taps_to_cases/runner/src/test/resources/DemoJson.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"cases": [
{
"index": 0,
"description": "Input helloworld in PC",
"elementInfo": {},
"driverId": "windows",
"action": {
Expand All @@ -27,7 +27,7 @@
"isOption": false
},
{
"index": 1,
"description": "Click sign in in GP",
"elementInfo": {
"index": "3",
"package": "com.android.vending",
Expand Down Expand Up @@ -56,7 +56,7 @@
"isOption": false
},
{
"index": 2,
"description": "Assert text in TextView",
"elementInfo": {
"index": "2",
"package": "com.android.vending",
Expand Down Expand Up @@ -88,7 +88,7 @@
"isOption": false
},
{
"index": 3,
"description": "Sleep 10 seconds",
"elementInfo": { },
"driverId": "10086",
"action": {
Expand All @@ -100,7 +100,7 @@
"isOption": false
},
{
"index": 4,
"description": "Sleep 10 seconds",
"elementInfo": { },
"driverId": "10086",
"action": {
Expand All @@ -112,7 +112,7 @@
"isOption": false
},
{
"index": 5,
"description": "tap on phone screen",
"elementInfo": { },
"driverId": "10086",
"action": {
Expand All @@ -125,7 +125,7 @@
"isOption": false
},
{
"index": 6,
"description": "Launch application in phone",
"driverId": "10086",
"action": {
"actionType": "activateApp",
Expand All @@ -136,7 +136,7 @@
"isOption": false
},
{
"index": 7,
"description": "Input text in phone",
"driverId": "10086",
"elementInfo": {
"index": "2",
Expand Down

0 comments on commit 947ec60

Please sign in to comment.