Search in sources :

Example 1 with AndroidTestAction

use of entity.AndroidTestAction in project CodeUtils by boredream.

the class Main method parseActions.

private static List<AndroidTestAction> parseActions(String actionContent) {
    List<AndroidTestAction> actions = new ArrayList<>();
    Pattern pattern = Pattern.compile("([\\d]{1,2})、(【.*?】)(【.*?】)?(:[\\S]+[\\s]+)?");
    Matcher matcher = pattern.matcher(actionContent);
    while (matcher.find()) {
        int step = -1;
        if (matcher.group(1) != null) {
            step = Integer.parseInt(matcher.group(1));
        }
        boolean isNotAction = false;
        int actionType = -1;
        String actionTypeContent = matcher.group(2);
        if (actionTypeContent != null) {
            if (actionTypeContent.startsWith("【不")) {
                isNotAction = true;
                actionTypeContent = actionTypeContent.replaceFirst("不", "");
            }
            switch(actionTypeContent) {
                case "【输入】":
                    actionType = AndroidTestAction.TYPE_TYPE_TEXT;
                    break;
                case "【点击】":
                    actionType = AndroidTestAction.TYPE_CLICK;
                    break;
            }
        }
        String view = null;
        if (matcher.group(3) != null) {
            view = matcher.group(3).replace("【", "").replace("】", "");
        }
        String text = null;
        if (matcher.group(4) != null) {
            text = matcher.group(4).replaceFirst(":", "").trim();
        }
        AndroidTestAction action = new AndroidTestAction();
        action.setStep(step);
        action.setNotAction(isNotAction);
        action.setActionType(actionType);
        action.setView(view);
        action.setText(text);
        actions.add(action);
    }
    return actions;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AndroidTestAction(entity.AndroidTestAction) ArrayList(java.util.ArrayList)

Example 2 with AndroidTestAction

use of entity.AndroidTestAction in project CodeUtils by boredream.

the class Main method generateTestCode.

private static String generateTestCode(List<AndroidTestInfo> testInfos) {
    StringBuilder sb = new StringBuilder();
    sb.append(StringUtils.formatSingleLine(1, "@Rule"));
    sb.append(StringUtils.formatSingleLine(1, "public ActivityTestRule<XXXActivity> mActivityRule = new ActivityTestRule<>(XXXActivity.class, true, false);"));
    sb.append("\n");
    for (int i = 0; i < testInfos.size(); i++) {
        AndroidTestInfo testInfo = testInfos.get(i);
        List<AndroidTestAction> actions = testInfo.getActions();
        AndroidTestAssertion assertion = testInfo.getAssertion();
        String number = testInfo.getNumber().replace(".0", "");
        if (number.equals("-1")) {
            number = i + "";
        }
        String testMethodName = "test" + number;
        sb.append(StringUtils.formatSingleLine(1, "/**"));
        sb.append(StringUtils.formatSingleLine(1, " * " + testInfo.getTitle()));
        sb.append(StringUtils.formatSingleLine(1, " */"));
        sb.append(StringUtils.formatSingleLine(1, "@Test"));
        sb.append(StringUtils.formatSingleLine(1, "public void " + testMethodName + "() throws InterruptedException {"));
        sb.append(StringUtils.formatSingleLine(2, "Intent intent = new Intent();"));
        sb.append(StringUtils.formatSingleLine(2, "mActivityRule.launchActivity(intent);"));
        sb.append("\n");
        sb.append(StringUtils.formatSingleLine(2, "// actions"));
        for (AndroidTestAction action : actions) {
            StringBuilder sbAction = new StringBuilder();
            sbAction.append("onView(withContentDescription(\"" + action.getView() + "\"))");
            switch(action.getActionType()) {
                case AndroidTestAction.TYPE_TYPE_TEXT:
                    if (action.isNotAction()) {
                        sbAction.append("; // do nothing");
                    } else {
                        sbAction.append(".perform(typeText(\"" + action.getText() + "\"), closeSoftKeyboard());");
                    }
                    break;
                case AndroidTestAction.TYPE_CLICK:
                    sbAction.append(".perform(click());");
                    break;
            }
            sb.append(StringUtils.formatSingleLine(2, sbAction.toString()));
        }
        sb.append("\n");
        sb.append(StringUtils.formatSingleLine(2, "Thread.sleep(500);"));
        sb.append("\n");
        sb.append(StringUtils.formatSingleLine(2, "// assertions"));
        switch(assertion.getAssertionType()) {
            case AndroidTestAssertion.TYPE_SHOW_TOAST:
                sb.append(StringUtils.formatSingleLine(2, "onView(withId(android.R.id.message))"));
                sb.append(StringUtils.formatSingleLine(3, ".inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView()))))"));
                sb.append(StringUtils.formatSingleLine(3, ".check(matches(withText(\"" + assertion.getText() + "\")));"));
                break;
            default:
                sb.append(StringUtils.formatSingleLine(2, "// TODO write your assertion~ or waiting seconds for check result by yourself"));
                sb.append(StringUtils.formatSingleLine(2, "Thread.sleep(3000);"));
                break;
        }
        sb.append(StringUtils.formatSingleLine(1, "}"));
        sb.append("\n");
    }
    return sb.toString();
}
Also used : AndroidTestAssertion(entity.AndroidTestAssertion) AndroidTestInfo(entity.AndroidTestInfo) AndroidTestAction(entity.AndroidTestAction)

Aggregations

AndroidTestAction (entity.AndroidTestAction)2 AndroidTestAssertion (entity.AndroidTestAssertion)1 AndroidTestInfo (entity.AndroidTestInfo)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1