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

org.sitoolkit.tester.domain.selenium.ExecOperation Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013 Monocrea Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sitoolkit.tester.domain.selenium;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.sitoolkit.tester.domain.test.ElementPosition;
import org.sitoolkit.tester.domain.test.OperationLog;
import org.sitoolkit.tester.domain.test.TestStep;
import org.sitoolkit.tester.infra.TestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;

/**
 *
 * @author yu.kawai
 */
public class ExecOperation extends SeleniumOperation {

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

    private static final String CMD_SEPARATOR = "[\\s]+";

    @Resource
    protected OperationLog opelog;

    @Override
    public void execute(TestStep testStep) {

        String cmd = testStep.getLocator().getValue();
        opelog.info(log, ElementPosition.EMPTY, "コマンド[{}]を実行します。", cmd);

        ProcessBuilder pb = new ProcessBuilder(cmd.split(CMD_SEPARATOR));
        pb.redirectErrorStream(true);
        Process process = null;
        String cmdlog = null;
        InputStream is = null;
        int exitValue = 0;

        try {
            process = pb.start();
            process.waitFor();
            is = process.getInputStream();
            cmdlog = IOUtils.toString(is);
            exitValue = process.exitValue();
        } catch (Exception e) {
            throw new TestException(e);
        } finally {
            IOUtils.closeQuietly(is);
            process.destroy();
        }

        if (!StringUtils.isEmpty(cmdlog)) {
            opelog.info(log, ElementPosition.EMPTY, cmdlog);
        }

        if (exitValue != 0) {
            throw new TestException("コマンドが異常終了しました。 " + cmd);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy