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

org.sitoolkit.tester.app.genscript.TestScriptGenerator Maven / Gradle / Ivy

The newest version!
package org.sitoolkit.tester.app.genscript;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.sitoolkit.tester.domain.genscript.PageContext;
import org.sitoolkit.tester.domain.genscript.PageListener;
import org.sitoolkit.tester.domain.genscript.PageLoader;
import org.sitoolkit.tester.domain.test.TestScriptDao;
import org.sitoolkit.tester.domain.test.debug.TestScriptGenerateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestScriptGenerator implements TestScriptGenerateTool, ApplicationContextAware {

    private static final Logger LOG = LoggerFactory.getLogger(TestScriptGenerator.class);

    private static final String MSG = "テストスクリプトに出力するページでEnterキーをタイプしてください。\n" + "終了する場合はqを入力しEnterキーをタイプしてください。";

    private List loaders;

    private PageListener listener;

    private TestScriptDao dao;

    private ApplicationContext appCtx;

    private String outputDir = "pageobj";

    public static void main(String[] args) {
        System.exit(staticStart());
    }

    public static int staticStart() {
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("sit-wt-conf.xml", "sit-wt-gen-script-conf.xml");

        TestScriptGenerator generator = appCtx.getBean(TestScriptGenerator.class);
        return generator.start();
    }

    public int start() {
        try {
            listener.setUp();

            Scanner scan = new Scanner(System.in);

            LOG.info("ブラウザが起動したらブラウザを操作してください。");
            LOG.info(MSG);

            while (!"q".equalsIgnoreCase(scan.nextLine())) {
                generateFromPage();
                LOG.info(MSG);
            }

            scan.close();

        } catch (Exception e) {
            LOG.error("予期しないエラーが発生しました。", e);
            return -1;

        } finally {
            listener.tearDown();
        }

        return 0;
    }

    public void generateFromPage() {
        PageContext pageCtx = appCtx.getBean(PageContext.class);

        if (listener != null) {
            listener.setUpPage(pageCtx);
        }

        LOG.info("ページの読み込みを開始します。{} {}", pageCtx.getTitle(), pageCtx.getUrl());

        for (PageLoader loader : loaders) {
            LOG.info("{}を実行します。", loader.getClass().getName());
            loader.load(pageCtx);
        }

        if (listener != null) {
            listener.tearDownPage(pageCtx);
        }

        String fileName = pageCtx.getTitle();
        if (StringUtils.isEmpty(fileName)) {
            fileName = pageCtx.getUrl();
            fileName = StringUtils.substringAfterLast(fileName, "/");
            fileName = StringUtils.substringBefore(fileName, "?");
        }
        fileName = fileName + ".xlsx";
        String filePath = FilenameUtils.concat(outputDir, fileName);

        dao.write(filePath, pageCtx.asList());

    }

    public List getLoaders() {
        return loaders;
    }

    public void setLoaders(List loaders) {
        this.loaders = loaders;
    }

    public PageListener getListener() {
        return listener;
    }

    public void setListener(PageListener listener) {
        this.listener = listener;
    }

    public TestScriptDao getDao() {
        return dao;
    }

    public void setDao(TestScriptDao dao) {
        this.dao = dao;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.appCtx = applicationContext;
    }

    public String getOutputDir() {
        return outputDir;
    }

    public void setOutputDir(String outputDir) {
        this.outputDir = outputDir;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy