【问题标题】:How to get Test Case name in Katalon Studio如何在 Katalon Studio 中获取测试用例名称
【发布时间】:2020-12-09 23:01:09
【问题描述】:

我正在尝试截取每个测试用例的屏幕截图,并将其导出到带有名称的屏幕截图目录。

我正在使用:

testName = RunConfiguration.getExecutionSourceName().toString()

但这仅包含测试套件的名称,而不是测试用例的名称。

WebUI.takeScreenshot('path'+testName+'.png')

如何引用测试用例名称而不是测试套件名称?

谢谢。

编辑:我截取的代码目前位于测试套件中的“TearDownTestCase”方法中。

【问题讨论】:

    标签: katalon-studio katalon


    【解决方案1】:

    好的,所以我在@Mate Mrse 的帮助下想通了。运行 .getExecutionSource() 方法会在运行测试套件时返回测试套件名称。但是我需要返回测试用例名称。

    我首先创建了一个测试监听器并添加到'@BeforeTestCase':

    class TestCaseName {
    
        @BeforeTestCase
        def sampleBeforeTestCase(TestCaseContext testCaseContext) {
            String testCaseId = testCaseContext.getTestCaseId()
        }
    }
    

    这会返回路径:

    ../Katalon Studio/Test Cases/Test Case Name
    

    然后我使用 .substring() 方法将测试用例名称存储为字符串

    class TestCaseName {
    
        @BeforeTestCase
        def sampleBeforeTestCase(TestCaseContext testCaseContext) {
            String testCaseId = testCaseContext.getTestCaseId()
            GlobalVariable.testCaseName = testCaseId.substring((testCaseId.lastIndexOf("/").toInteger()) + 1)
        }
    }
    

    谢谢@Mate Mrse

    【讨论】:

      【解决方案2】:

      您可以使用RunConfiguration.getExecutionSource() 获取正在运行的测试用例的完整路径。

      然后你就可以随心所欲了。例如,要获取测试用例名称,您可能会执行类似

      RunConfiguration.getExecutionSource().toString().substring(RunConfiguration.getExecutionSource().toString().lastIndexOf("\\")+1)
      

      说明:

      .getExecutionSource() 方法将为您提供测试用例的完整路径,例如 C:\users\user.name\Katalon Studio\Test Cases\Test Case Name.tc(您可能有不同的东西)。

      由于您只需要最后一部分,因此您可以使用 Groovy 将此字符串剪切为您喜欢的内容。所以,我在Test Case Name.tc 之前的最后一个\ (这就是lastIndexOf 正在做的事情)的位置剪断了字符串(+1,因为我也想剪掉反斜杠)。

      然后.substring() 方法将给我切割后的剩余部分。

      【讨论】:

      • 你能解释一下吗:“substring(RC.getExecutionSource().toString().lastIndexOf("\\")+1)” 我只是盲目地复制并粘贴了你的建议,我得到了与我之前所做的相同的输出(它获取测试套件名称而不是测试用例)。
      • @Qma 我编辑了带有解释的帖子,您需要使用 Groovy 字符串方法来获得准确的 TC。顺便说一句,当你运行完整的命令时,你会得到什么路径?
      • 它给了我一条通往 Test Suite 与 Test Case 的路径 ..\Katalon Studio\Test Suite\Test Suite Name.ts 而不是 Test Case 这是我们对 .getExecutionSource 方法的期望。我希望是否有一种变通方法可以通过将方法放在测试套件中来获取测试用例名称。我有一个运行多个测试用例的测试套件。在每个测试用例结束时,我想在关闭窗口之前截取屏幕截图,并将屏幕截图命名为测试用例。
      猜你喜欢
      • 2021-04-11
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多