【问题标题】:Is it possible to run commands in Katalon?是否可以在 Katalon 中运行命令?
【发布时间】:2018-09-28 05:13:24
【问题描述】:

Katalon 在自动化测试中很受欢迎。我已经在我们的项目中使用了它,并且效果非常好。

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

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

【问题讨论】:

    标签: testing cmd integration katalon-studio


    【解决方案1】:

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

    @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')
    

    【讨论】:

      【解决方案2】:

      这是另一个自定义关键字!它需要文件名和路径,如果你不给它路径,它会在项目根目录中搜索文件。它将批处理文件的输出导出到项目文件夹中的 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();
                  }               
          }
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 我认为打开终结器是一种桌面应用自动化。不幸的是,Katalon 还不支持。
        【解决方案4】:

        可以从command line 运行 Katalon Studio。

        有一个简短的教程here

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

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

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

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

        【讨论】:

        • 不,我想做的是在katalon里面我运行命令行
        • 哦,我明白了。不知道这个
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-30
        • 2021-05-15
        • 2022-08-14
        • 1970-01-01
        • 1970-01-01
        • 2012-09-21
        • 1970-01-01
        相关资源
        最近更新 更多