All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.jarch.apt.generate.SystemTestGenerate Maven / Gradle / Ivy

The newest version!
package br.com.jarch.apt.generate;

import br.com.jarch.core.annotation.JArchGenerateCrud;
import br.com.jarch.core.form.FieldForm;
import br.com.jarch.core.model.type.ActionCrudType;
import br.com.jarch.util.FileUtils;
import br.com.jarch.util.LogUtils;
import br.com.jarch.apt.util.ProcessorUtils;
import br.com.jarch.core.type.FieldType;

import javax.lang.model.element.Element;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static br.com.jarch.core.annotation.JArchGenerateCrud.*;
import static br.com.jarch.apt.util.ProcessorUtils.addCode;
import static br.com.jarch.apt.util.ProcessorUtils.addLineBlank;

public final class SystemTestGenerate {

    private File fileTest;
    private File filePage;
    private String namePackage;
    private String nome;
    private String nomeSemCaracterEspecial;
    private String nomeSemCaracterEspecialMinusculo;
    private final List listFieldFormMaster;
    private final Element element;
    private final JArchGenerateCrud generateCrud;

    public SystemTestGenerate(Element element, JArchGenerateCrud generateCrud) {
        this.generateCrud = generateCrud;
        this.element = element;
        this.listFieldFormMaster = ProcessorUtils.getListFieldForm(generateCrud.master().fields());
        configure(element, generateCrud.master().name(), generateCrud.nameSubPackage());
    }

    public static void generate(Element element, JArchGenerateCrud generateCrud) {
        new SystemTestGenerate(element, generateCrud).create();
    }

    private void create() {
        saveTest();
        savePage();
    }

    private void saveTest() {
        if (fileTest.exists())
            return;

        StringBuilder sbSystemTest = new StringBuilder();
        addCode(sbSystemTest, "package " + namePackage + ";");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "import br.com.jarch.test.BrowseFactory;");
        addCode(sbSystemTest, "import br.com.jarch.test.TestUtils;");
        addCode(sbSystemTest, "import br.com.jarch.test.system.CrudSystemTest;");
        addCode(sbSystemTest, "import org.junit.AfterClass;");
        addCode(sbSystemTest, "import org.junit.BeforeClass;");
        addCode(sbSystemTest, "import org.openqa.selenium.WebDriver;");
        addCode(sbSystemTest, "import org.openqa.selenium.support.PageFactory;");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "public class " + nomeSemCaracterEspecial + "SystemTest extends CrudSystemTest<" + nomeSemCaracterEspecial + "Page> {");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "\tprivate static WebDriver driver;");
        addCode(sbSystemTest, "\tprivate static " + nomeSemCaracterEspecial + "Page " + nomeSemCaracterEspecialMinusculo + "Page;");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "\tstatic {");
        addCode(sbSystemTest, "\t\tdriver = BrowseFactory.getFirefoxDriver();");
        addCode(sbSystemTest, "\t\t" + nomeSemCaracterEspecialMinusculo + "Page = PageFactory.initElements(driver, " + nomeSemCaracterEspecial + "Page.class);");
        addCode(sbSystemTest, "\t}");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "\t@Override");
        addCode(sbSystemTest, "\tpublic " + nomeSemCaracterEspecial + "Page getPage() {");
        addCode(sbSystemTest, "\t\treturn " + nomeSemCaracterEspecialMinusculo + "Page;");
        addCode(sbSystemTest, "\t}");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "\t@BeforeClass");
        addCode(sbSystemTest, "\tpublic static void beforeClass() {");
        addCode(sbSystemTest, "\t\tTestUtils.login(driver, \"" + getUrlApplication() + "/paginas/login/login.jsf?multiTenantId=1&systemTest=S\", \"login\", \"senha\");");
        addCode(sbSystemTest, "\t}");
        addLineBlank(sbSystemTest);
        addCode(sbSystemTest, "\t@AfterClass");
        addCode(sbSystemTest, "\tpublic static void afterClass() {");
        addCode(sbSystemTest, "\t\tTestUtils.logout(driver);");
        addCode(sbSystemTest, "\t}");
        addCode(sbSystemTest, "}");

        try {
            FileUtils.save(fileTest, sbSystemTest.toString());
            ProcessorUtils.messageNote("JARCH Created ==> " + fileTest.getAbsoluteFile(), element);
        } catch (IOException e) {
            LogUtils.generate(e);
        }
    }

    private void savePage() {
        if (filePage.exists())
            return;

        StringBuilder sbPage = new StringBuilder();
        addCode(sbPage, "package " + namePackage + ";");
        addLineBlank(sbPage);
        addImportsPage(sbPage);
        addLineBlank(sbPage);
        addCode(sbPage, "public class " + nomeSemCaracterEspecial + "Page extends CrudPage {");
        addLineBlank(sbPage);
        addMethodConstruct(sbPage);
        addInjectWebComponentFilter(sbPage);
        addInjectWebComponentData(sbPage);
        addInjectWebComponentTabDetail(sbPage);
        addInjectWebComponentTabSubDetail(sbPage);
        addMethodGetFolderXhtml(sbPage);
        addMethodGetPageList(sbPage);
        addMethodGetPageData(sbPage);
        addMethodActionInsert(sbPage);
        addMethodActionClone(sbPage);
        addMethodActionChange(sbPage);
        addMethodActionConsult(sbPage);
        addMethodActionDelete(sbPage);
        addCode(sbPage, "}");

        try {
            FileUtils.save(filePage, sbPage.toString());
            ProcessorUtils.messageNote("Create Page Class ==> " + filePage.getAbsoluteFile(), element);
        } catch (IOException e) {
            LogUtils.generate(e);
        }
    }

    private void addMethodConstruct(StringBuilder sbPage) {
        addCode(sbPage, "\tpublic " + nomeSemCaracterEspecial + "Page(WebDriver driver) {");
        addCode(sbPage, "\t\tsuper(driver);");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodActionDelete(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tpublic void actionDelete() {");
        addCode(sbPage, "\t\tgoDeleteDataInsertAndDataChange(" + getWebElementFilterString() + ", FieldType." + getWebElementFilterFieldForm().getTipo().name() + (generateCrud == null ? "" : ", true") + ");");
        addCode(sbPage, "\t}");
    }

    private void addMethodActionConsult(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tpublic void actionConsult() {");
        addCodeGoPageListFilterChange(sbPage);
        addCode(sbPage, "\t\tclickListActionConsult();");
        addCode(sbPage, "\t\twaitForPageData();");

        if (generateCrud != null)
            addCode(sbPage, "\t\tscrollBottomPage();");

        addCode(sbPage, "\t\tclickMasterReturn();");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodActionChange(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tpublic void actionChange() {");
        addCodeGoPageListFilterClone(sbPage);
        addCode(sbPage, "\t\tclickListActionChange();");
        addClickClearSendkeysChange(sbPage);

        if (generateCrud != null)
            addCode(sbPage, "\t\tscrollBottomPage();");

        addCode(sbPage, "\t\tclickMasterSave();");
        addCode(sbPage, "\t\twaitForMessageSuccess();");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodActionClone(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tpublic void actionClone() {");
        addCodeGoPageListFilterInsert(sbPage);
        addCode(sbPage, "\t\tclickListActionClone();");
        addClickClearSendkeysClone(sbPage);

        if (generateCrud != null)
            addCode(sbPage, "\t\tscrollBottomPage();");

        addCode(sbPage, "\t\tclickMasterSave();");
        addCode(sbPage, "\t\twaitForMessageSuccess();");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodActionInsert(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tpublic void actionInsert() {");
        addCode(sbPage, "\t\tgoPageListFromUrl();");
        addCode(sbPage, "\t\tclickListActionInsert();");
        addClickClearSendkeysInsert(sbPage);

        if (generateCrud != null)
            addCode(sbPage, "\t\tscrollBottomPage();");

        addCode(sbPage, "\t\tclickMasterSave();");
        addCode(sbPage, "\t\twaitForMessageSuccess();");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodGetPageData(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tprotected String getPageData() {");
        addCode(sbPage, "\t\treturn \"" + nomeSemCaracterEspecialMinusculo + "Data.jsf\";");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodGetFolderXhtml(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tprotected String getFolderXhtml() {");
        addCode(sbPage, "\t\treturn \"" + nomeSemCaracterEspecialMinusculo.toLowerCase() + "\";");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addMethodGetPageList(StringBuilder sbPage) {
        addCode(sbPage, "\t@Override");
        addCode(sbPage, "\tprotected String getPageList() {");
        addCode(sbPage, "\t\treturn \"" + nomeSemCaracterEspecialMinusculo + "List.jsf\";");
        addCode(sbPage, "\t}");
        addLineBlank(sbPage);
    }

    private void addImportsPage(StringBuilder sbPage) {
        addCode(sbPage, "import br.com.jarch.test.page.CrudPage;");
        addCode(sbPage, "import br.com.jarch.core.type.FieldType;");
        addCode(sbPage, "import org.openqa.selenium.WebDriver;");
        addCode(sbPage, "import org.openqa.selenium.WebElement;");
        addCode(sbPage, "import org.openqa.selenium.support.FindBy;");
        addCode(sbPage, "import org.openqa.selenium.support.How;");

        listFieldFormMaster
                .stream()
                .filter(f -> f.getTipo().isEnum())
                .forEach(f -> addCode(sbPage, "import " + namePackage.replace("test", "client").concat(".").concat(f.getAtributoPrimeiroLetraMaiusculo().concat("Type;"))));
    }

    private void addCodeGoPageListFilterInsert(StringBuilder sbPage) {
        FieldForm fieldForm = getWebElementFilterFieldForm();
        addCode(sbPage, "\t\tgoPageListAndFilter(" + getWebElementFilterString() + ", FieldType." + fieldForm.getTipo().name() + ".getValueInsertTest());");
    }

    private void addCodeGoPageListFilterClone(StringBuilder sbPage) {
        FieldForm fieldForm = getWebElementFilterFieldForm();
        addCode(sbPage, "\t\tgoPageListAndFilter(" + getWebElementFilterString() + ", FieldType." + fieldForm.getTipo().name() + ".getValueCloneTest());");
    }

    private void addCodeGoPageListFilterChange(StringBuilder sbPage) {
        FieldForm fieldForm = getWebElementFilterFieldForm();
        addCode(sbPage, "\t\tgoPageListAndFilter(" + getWebElementFilterString() + ", FieldType." + fieldForm.getTipo().name() + ".getValueChangeTest());");
    }

    private FieldForm getWebElementFilterFieldForm() {
        return listFieldFormMaster
                .stream()
                .filter(f -> !f.getSearch().isEmpty())
                .findFirst()
                .get();
    }

    private String getWebElementFilterString() {
        return "webElement" + nomeSemCaracterEspecial + getWebElementFilterFieldForm().getAtributoPrimeiroLetraMaiusculo() + "FilterSearch";
    }

    private void addInjectWebComponentFilter(StringBuilder sbSystemTest) {
        listFieldFormMaster
                .stream()
                .filter(f -> !f.getSearch().isEmpty())
                .forEach(fieldForm -> {
                            addCode(sbSystemTest, "\t@FindBy(how = How.XPATH, xpath = \"//input[contains(@id, 'filterSearch" + fieldForm.getAtributo() + "')]\")");
                            addCode(sbSystemTest, "\tprivate WebElement webElement" + nomeSemCaracterEspecial + fieldForm.getAtributoPrimeiroLetraMaiusculo() + "FilterSearch;");
                            addLineBlank(sbSystemTest);
                        }
                );
    }

    private void addInjectWebComponentDataListFormField(StringBuilder sbSystemTest, String name, String dataDetail, List fieldForms) {
        fieldForms
                .stream()
                .filter(FieldForm::isObrigatorio)
                .forEach(fieldForm -> {
                            if (fieldForm.getTipo().isEnum() || fieldForm.getTipo().equals(FieldType.BOOLEAN)) {
                                addCode(sbSystemTest, "\t@FindBy(how = How.XPATH, xpath = \"//div[contains(@id, '" + fieldForm.getGenerateId(name, dataDetail) + "')]\")");
                                addCode(sbSystemTest, "\tprivate WebElement webElement" + fieldForm.getGenerateId(name, dataDetail).substring(2) + ";");
                                addLineBlank(sbSystemTest);
                            } else {
                                if (fieldForm.getTipo().equals(FieldType.ADDRESS)) {
                                    addCode(sbSystemTest, "\t@FindBy(how = How.CSS, css = \"div[id *= '" + fieldForm.getGenerateId(name, dataDetail) + "'] input[id *= 'zipCode']\")");
                                } else if (fieldForm.getTipo().equals(FieldType.MEMO_RICH)) {
                                    addCode(sbSystemTest, "\t@FindBy(how = How.CSS, css = \"div[id *= '" + fieldForm.getGenerateId(name, dataDetail) + "'] .ql-editor\")");
                                } else {
                                    addCode(sbSystemTest, "\t@FindBy(how = How.XPATH, xpath = \"//input[contains(@id, '" + fieldForm.getGenerateId(name, dataDetail) + (fieldForm.getTipo().isAddress() ? ":zipCode" : "") + "')]\")");
                                }

                                addCode(sbSystemTest, "\tprivate WebElement webElement" + fieldForm.getGenerateId(name, dataDetail).substring(2) + ";");

                                addLineBlank(sbSystemTest);
                            }
                        }
                );
    }

    private void addInjectWebComponentTabDetail(StringBuilder sbSystemTest) {
        Arrays
                .stream(generateCrud.master().details())
                .forEach(detail -> {
                    addCode(sbSystemTest, "\t@FindBy(how = How.XPATH, xpath = \"//a[contains(@href, 'tab" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(detail.name()) + "')]/span/input\")");
                    addCode(sbSystemTest, "\tprivate WebElement webElementInsert" + ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(detail.name()) + ";");
                    addLineBlank(sbSystemTest);
                });
    }

    private void addInjectWebComponentTabSubDetail(StringBuilder sbSystemTest) {
        Arrays
                .stream(generateCrud.master().details())
                .forEach(detail -> Arrays
                        .stream(detail.subDetails())
                        .forEach(subDetail -> {
                            addCode(sbSystemTest, "\t@FindBy(how = How.XPATH, xpath = \"//a[contains(@href, 'tab" + ProcessorUtils.getNameWithoutCharEspecialCapitalize(subDetail.name()) + "')]/input\")");
                            addCode(sbSystemTest, "\tprivate WebElement webElementInsert" + ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(subDetail.name()) + ";");
                            addLineBlank(sbSystemTest);
                        }));
    }

    private void addInjectWebComponentData(StringBuilder sbSystemTest) {
        addInjectWebComponentDataListFormField(sbSystemTest, nomeSemCaracterEspecial, "", listFieldFormMaster);

        Arrays
                .stream(generateCrud.master().details())
                .forEach(detail -> addInjectWebComponentDataListFormField(sbSystemTest, nomeSemCaracterEspecial, ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(detail.name()), ProcessorUtils.getListFieldForm(detail.fields())));

        Arrays
                .stream(generateCrud.master().details())
                .forEach(detail -> Arrays
                        .stream(detail.subDetails())
                        .forEach(subDetail -> addInjectWebComponentDataListFormField(sbSystemTest, nomeSemCaracterEspecial, ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(subDetail.name()), ProcessorUtils.getListFieldForm(subDetail.fields()))));
    }

    private void addClickClearSendkeysInsert(StringBuilder sbSystemTest) {
        listFieldFormMaster
                .forEach(fieldForm -> addCodeFillWebComponentInsert(sbSystemTest, nome, fieldForm));

        for (Detail detail : Arrays.stream(generateCrud.master().details()).collect(Collectors.toList())) {
            String nomeSemCaracterEspecialDetailStartLowerCase = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(detail.name());
            String nomeSemCaracterEspecialDetailCapitalize = ProcessorUtils.getNameWithoutCharEspecialCapitalize(detail.name());

            addCode(sbSystemTest, "\t\tscrollTo(webElementInsert" + nomeSemCaracterEspecialDetailStartLowerCase + ");");
            addCode(sbSystemTest, "\t\tclickDetailActionInsert(\"tab" + nomeSemCaracterEspecialDetailCapitalize + "\");");

            ProcessorUtils
                    .getListFieldForm(detail.fields())
                    .forEach(fieldForm -> addCodeFillWebComponentInsert(sbSystemTest, nomeSemCaracterEspecialDetailStartLowerCase, fieldForm));

            for (SubDetail subDetail : Arrays.stream(detail.subDetails()).collect(Collectors.toList())) {
                String nomeSemCaracterEspecialSubDetailStartLowerCase = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(subDetail.name());
                String nomeSemCaracterEspecialSubDetailCapitalize = ProcessorUtils.getNameWithoutCharEspecialCapitalize(subDetail.name());

                addCode(sbSystemTest, "\t\tscrollTo(webElementInsert" + nomeSemCaracterEspecialSubDetailStartLowerCase + ");");
                addCode(sbSystemTest, "\t\tclickDetailActionInsert(\"tab" + nomeSemCaracterEspecialSubDetailCapitalize + "\");");

                ProcessorUtils
                        .getListFieldForm(subDetail.fields())
                        .forEach(fieldForm -> addCodeFillWebComponentInsert(sbSystemTest, nomeSemCaracterEspecialSubDetailStartLowerCase, fieldForm));

                addCode(sbSystemTest, "\t\tclickDetailSave();");
            }

            addCode(sbSystemTest, "\t\tclickDetailSave();");
        }
    }

    private void addClickClearSendkeysClone(StringBuilder sbSystemTest) {
        listFieldFormMaster
                .forEach(fieldForm -> addCodeFillWebComponentClone(sbSystemTest, nome, fieldForm));
    }

    private void addClickClearSendkeysChange(StringBuilder sbSystemTest) {
        listFieldFormMaster
                .forEach(fieldForm -> addCodeFillWebComponentChange(sbSystemTest, nome, fieldForm));
    }

    private void configure(Element element, String name, String nameSubPackage) {
        nome = name;
        nomeSemCaracterEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(name);
        nomeSemCaracterEspecialMinusculo = ProcessorUtils.getNameWithoutCharEspecialStartLowerCase(name);

        File folder = new File(ProcessorUtils.getPathTest(element, nameSubPackage).replace("-client", "-web").replace(File.separator + "client" + File.separator, File.separator + "test" + File.separator));

        if (!folder.exists())
            folder.mkdirs();

        fileTest = new File(folder.getAbsolutePath() + File.separator + nomeSemCaracterEspecial + "SystemTest.java");
        filePage = new File(folder.getAbsolutePath() + File.separator + nomeSemCaracterEspecial + "Page.java");

        namePackage = ProcessorUtils.getPackage(element, nameSubPackage).replace(".client.", ".test.");
    }

    private String getUrlApplication() {
        String context = filePage.getAbsolutePath();
        if (context.contains("-web"))
            context = context.substring(0, context.indexOf("-web"));
        else if (context.contains("-client"))
            context = context.substring(0, context.indexOf("-client"));
        else
            context = "";

        if (!context.isEmpty())
            context = context.substring(context.lastIndexOf(File.separator) + 1);

        if (context.isEmpty())
            context = "myapp";

        context = context.replace("\\", "/");

        return "http://localhost:8080/" + context;
    }

    private void addCodeFillWebComponentInsert(StringBuilder sbSystemTest, String name, FieldForm fieldForm) {
        addCodeFillWebComponent(sbSystemTest, name, fieldForm, ActionCrudType.INSERT);
    }

    private void addCodeFillWebComponentClone(StringBuilder sbSystemTest, String name, FieldForm fieldForm) {
        addCodeFillWebComponent(sbSystemTest, name, fieldForm, ActionCrudType.CLONE);
    }

    private void addCodeFillWebComponentChange(StringBuilder sbSystemTest, String name, FieldForm fieldForm) {
        addCodeFillWebComponent(sbSystemTest, name, fieldForm, ActionCrudType.CHANGE);
    }

    private void addCodeFillWebComponent(StringBuilder sbSystemTest, String name, FieldForm fieldForm, ActionCrudType actionCrudType) {
        if (!fieldForm.isObrigatorio())
            return;

        String nameNoCharEspecial = ProcessorUtils.getNameWithoutCharEspecialCapitalize(name);
        String action = actionCrudType.name().substring(0, 1).concat(actionCrudType.name().substring(1).toLowerCase());

        if (fieldForm.getTipo().isEnum())
            addCode(sbSystemTest, "\t\tclickSelect(webElement" + nameNoCharEspecial + fieldForm.getAtributoPrimeiroLetraMaiusculo() + ", " + fieldForm.getAtributoPrimeiroLetraMaiusculo() + "Type.SAMPLE" + (actionCrudType.isInsert() || actionCrudType.isChange() ? "1" : "2") + ".name());");
        else if (fieldForm.getTipo().equals(FieldType.BOOLEAN))
            addCode(sbSystemTest, "\t\tclick(webElement" + nameNoCharEspecial + fieldForm.getAtributoPrimeiroLetraMaiusculo() + ");");
        else if (FieldType.BINARY.equals(fieldForm.getTipo()))
            addCode(sbSystemTest, "\t\tfileUploadPdfFake(webElement" + nameNoCharEspecial + fieldForm.getAtributoPrimeiroLetraMaiusculo() + ");");
        else
            addCode(sbSystemTest, "\t\tclickClearSendKeys(webElement" + nameNoCharEspecial + fieldForm.getAtributoPrimeiroLetraMaiusculo() + ", FieldType." + fieldForm.getTipo().name() + ".getValue" + action + "Test());");

        if (fieldForm.getTipo() == FieldType.ADDRESS)
            addCode(sbSystemTest, "\t\twaitForAjaxProcess();");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy