是否可以在 Katalon 中执行 运行 命令?

Is it possible to run commands in Katalon?

Katalon 在自动化测试中很流行。我已经在我们的项目中使用过它并且效果惊人。

现在,我想要实现的是创建一个测试用例,它打开一个终端(使用 mac)并向 运行 输入一些命令,例如:

cd /documents/pem/key.pem
connect to -my server via SSH@method
sudo su
yum install php7
yum install mysql

我做了一些研究。我没有找到任何资源或任何正在寻找与我相同的东西的人。我认为这是官方的,不。对此的回答是,不可能。

可以从 command line.

运行 Katalon Studio

有一个简短的教程 here

从 v5.10(目前处于测试阶段)开始,可以通过命令行执行模式覆盖配置文件变量。 Katalon forum 给出的例子是:

只需在命令行中传递参数,使用:-g_XXX = XXX

下面是覆盖 URL 变量的示例:

-g_URL=http://demoaut.katalon.com

您并不孤单,使用自定义关键字可以实现您想要的。这是一个显示命令行应用程序测试的示例。你可以做同样的事情来调用你想要的任何命令行脚本。想想一个 运行Cmd 关键字,或者一个 运行CmdWithOutput 来获取输出和 运行 各种断言它。

@Keyword
def pdfMetadata(String input) {
    KeywordUtil.logInfo("input: ${input}")

    def csaHome = System.getenv("CSA_HOME")
    def cmd = "cmd /c ${csaHome}/bin/csa -pdfmetadata -in \"${projectPath}${input}\"";
    runCmd(cmd)
}

def runCmd(String cmd) {
    KeywordUtil.logInfo("cmd: ${cmd}")

    def proc = cmd.execute();
    def outputStream = new StringBuffer();
    def errStream = new StringBuffer()
    proc.waitForProcessOutput(outputStream, errStream);
    println(outputStream.toString());
    println(errStream.toString())

    if(proc.exitValue() != 0){
        KeywordUtil.markFailed("Out:" + outputStream.toString() + ", Err: " + errStream.toString())
    }
}

然后您可以在测试用例中使用它:

CustomKeywords.'CSA.pdfMetadata'('/src/pdf/empty.pdf')

这是另一个自定义关键字!它接受文件名和路径,如果你不给它一个路径,它会在项目根目录中搜索文件。它将批处理文件的输出导出到项目文件夹的 batch_reports 文件夹中,您需要提前创建该文件夹。

@Keyword
    def runPostmanBatch(String batchName , String batchPath){


        // source: https://www.mkyong.com/java/how-to-execute-shell-command-from-java/

        String firstParameter = "cmd /c " + batchName;
        String  secondParameter =  batchPath;

        if (batchPath == ""){
            secondParameter = RunConfiguration.getProjectDir();
            }

        try {
            KeywordUtil.logInfo("Executing " + firstParameter + " at " +  secondParameter)
                Process process = Runtime.getRuntime().exec(
                        firstParameter , null, new File(secondParameter));

                StringBuilder output = new StringBuilder();

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));

                String line;

                while ((line = reader.readLine()) != null) {
                    output.append(line + "\n");
                }

                int exitVal = process.waitFor();

                Date atnow = new Date()
                String now = atnow.format('yy-MM-dd HH-mm-ss')
                String report_path = RunConfiguration.getProjectDir() + "/postman_reports/" + RunConfiguration.getExecutionSourceName() + "_" + now + ".txt"
                BufferedWriter writer = new BufferedWriter(new FileWriter(report_path));
                writer.write(output.toString());
                writer.close();
                KeywordUtil.logInfo("postman report at: " + report_path)
                if (exitVal == 0) {

                    println("Success!"); 
                    println(output); 
                    KeywordUtil.markPassed("Ran successfully")                  
                } else {
                KeywordUtil.markFailed("Something went wrong")
                    println(exitVal);
                }



            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }               
    }