Search in sources :

Example 1 with AndroidTestInfo

use of entity.AndroidTestInfo in project CodeUtils by boredream.

the class Main method parseTestInfo.

private static AndroidTestInfo parseTestInfo(Row row) {
    String num = getCellString(row.getCell(indexs.get(INDEX_NUMBER)));
    String title = getCellString(row.getCell(indexs.get(INDEX_TITLE)));
    String actions = getCellString(row.getCell(indexs.get(INDEX_ACTIONS)));
    String assertion = getCellString(row.getCell(indexs.get(INDEX_ASSERTION)));
    AndroidTestInfo testInfo = new AndroidTestInfo();
    testInfo.setNumber(num);
    testInfo.setTitle(title);
    testInfo.setActions(parseActions(actions));
    testInfo.setAssertion(parseAssertion(assertion));
    return testInfo;
}
Also used : AndroidTestInfo(entity.AndroidTestInfo)

Example 2 with AndroidTestInfo

use of entity.AndroidTestInfo in project CodeUtils by boredream.

the class Main method main.

public static void main(String[] args) throws Exception {
    File file = new File("temp" + File.separator + "androidtest" + File.separator + "test.xlsx");
    XSSFWorkbook xssfSheets = OfficeUtils.openXlsx(file);
    if (xssfSheets != null) {
        XSSFSheet sheet1 = xssfSheets.getSheetAt(0);
        List<AndroidTestInfo> testInfos = parseAllTestInfo(sheet1);
        String s = generateTestCode(testInfos);
        System.out.println(s);
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) AndroidTestInfo(entity.AndroidTestInfo) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File)

Example 3 with AndroidTestInfo

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

Example 4 with AndroidTestInfo

use of entity.AndroidTestInfo in project CodeUtils by boredream.

the class Main method parseAllTestInfo.

private static List<AndroidTestInfo> parseAllTestInfo(XSSFSheet sheet1) {
    List<AndroidTestInfo> testInfos = new ArrayList<>();
    for (int rowIndex = 0; rowIndex < sheet1.getPhysicalNumberOfRows(); rowIndex++) {
        Row row = sheet1.getRow(rowIndex);
        if (rowIndex == 0) {
            parseHeaderInfo(row);
        } else {
            AndroidTestInfo testInfo = parseTestInfo(row);
            testInfos.add(testInfo);
        }
    }
    return testInfos;
}
Also used : AndroidTestInfo(entity.AndroidTestInfo) ArrayList(java.util.ArrayList) Row(org.apache.poi.ss.usermodel.Row)

Aggregations

AndroidTestInfo (entity.AndroidTestInfo)4 AndroidTestAction (entity.AndroidTestAction)1 AndroidTestAssertion (entity.AndroidTestAssertion)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Row (org.apache.poi.ss.usermodel.Row)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1