Search in sources :

Example 1 with AndroidTestAssertion

use of entity.AndroidTestAssertion in project CodeUtils by boredream.

the class Main method parseAssertion.

private static AndroidTestAssertion parseAssertion(String assertionContent) {
    Pattern pattern = Pattern.compile("(【.*?】)(:[\\S\\s]+)?");
    Matcher matcher = pattern.matcher(assertionContent);
    if (matcher.find()) {
        int assertionType = -1;
        if (matcher.group(1) != null) {
            switch(matcher.group(1)) {
                case "【提示】":
                    assertionType = AndroidTestAssertion.TYPE_SHOW_TOAST;
                    break;
                case "【进入页面】":
                    assertionType = AndroidTestAssertion.TYPE_SHOW_ACTIVITY;
                    break;
            }
        }
        String text = null;
        if (matcher.group(2) != null) {
            text = matcher.group(2).replaceFirst(":", "").trim();
        }
        AndroidTestAssertion assertion = new AndroidTestAssertion();
        assertion.setAssertionType(assertionType);
        assertion.setText(text);
        return assertion;
    }
    return null;
}
Also used : AndroidTestAssertion(entity.AndroidTestAssertion) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 2 with AndroidTestAssertion

use of entity.AndroidTestAssertion 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

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