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

org.sitoolkit.wt.app.selenium2script.SeleniumStepConverter Maven / Gradle / Ivy

There is a newer version: 2.3
Show newest version
package org.sitoolkit.wt.app.selenium2script;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.sitoolkit.wt.domain.testscript.Locator;
import org.sitoolkit.wt.domain.testscript.TestStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SeleniumStepConverter implements ApplicationContextAware {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    protected ApplicationContext appCtx;

    private Map seleniumIdeCommandMap;

    private Pattern screenshotPattern;

    public List convertTestScript(SeleniumTestScript seleniumTestScript, String caseNo) {
        List testStepList = new ArrayList();

        int stepNo = 1;

        for (SeleniumTestStep seleniumStep : seleniumTestScript.getTestStepList()) {
            TestStep sitStep = new TestStep();
            testStepList.add(sitStep);

            sitStep.setCurrentCaseNo(caseNo);
            sitStep.setNo(Integer.toString(stepNo++));

            // 操作
            sitStep.setOperationName(convertOperationName(seleniumStep));

            // ロケーター
            sitStep.setLocator(convertLocator(seleniumStep));

            // テストデータ
            setTestData(sitStep, caseNo, seleniumStep.getValue());

            // open操作の場合はseleniumScriptのbaseUrlを設定
            if ("open".equals(sitStep.getOperationName())) {
                String locatorValue = sitStep.getLocator().getValue();
                sitStep.getLocator().setValue(seleniumTestScript.getBaseUrl() + locatorValue);
            }

            // スクリーンショット
            sitStep.setScreenshotTiming(convertScreenshotTiming(seleniumStep));

        }
        return testStepList;
    }

    /**
     * TestStepのテストデータを設定します。
     *
     * @param testStep
     *            TestStep
     * @param caseNo
     *            ケース番号
     * @param value
     *            テストデータ
     */
    protected void setTestData(TestStep testStep, String caseNo, String value) {
        Map testData = new HashMap();
        String[] pair = StringUtils.split(value, "=");
        if (pair.length == 2) {
            testStep.setDataType(pair[0]);
            testData.put(caseNo, pair[1]);
        } else {
            if (StringUtils.isBlank(value)) {
                testData.put(caseNo, "y");
            } else {
                testData.put(caseNo, value);
            }
        }
        testStep.setTestData(testData);
    }

    protected String convertOperationName(SeleniumTestStep seleniumStep) {

        String operationName = seleniumIdeCommandMap.get(seleniumStep.getCommand());

        if (operationName == null) {
            if (appCtx.containsBeanDefinition(seleniumStep.getCommand() + "Operation")) {
                operationName = seleniumStep.getCommand();
            } else {
                log.info("Seleniumコマンド:{}は非対応です。テストスクリプトの操作は空白で出力します。",
                        seleniumStep.getCommand());
            }
        }

        return operationName;

    }

    protected Locator convertLocator(SeleniumTestStep seleniumStep) {
        return Locator.build(seleniumStep.getTarget());
    }

    protected String convertScreenshotTiming(SeleniumTestStep seleniumStep) {
        String command = StringUtils.defaultString(seleniumStep.getCommand());
        return screenshotPattern.matcher(command).matches() ? "前" : "";
    }

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

    public Map getSeleniumIdeCommandMap() {
        return seleniumIdeCommandMap;
    }

    public void setSeleniumIdeCommandMap(Map seleniumIdeCommandMap) {
        this.seleniumIdeCommandMap = seleniumIdeCommandMap;
    }

    public void setScreenshotPattern(Pattern screenshotPattern) {
        this.screenshotPattern = screenshotPattern;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy