How to get Test Case name in Katalon Studio

大城市里の小女人 提交于 2019-12-24 06:37:20

问题


I am trying to take a screenshot of every Test Case and have it exported to a screenshot directory with it's name.

I am using:

testName = RunConfiguration.getExecutionSourceName().toString()

but this only contains the name of the Test Suite and NOT the Test Case name.

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

How would I reference the Test Case name and not the Test Suite name?

Thank you.

EDIT: The code I am taking a screenshot currently lives in the "TearDownTestCase" method located in Test Suites.


回答1:


Okay so I figured it out with the help of @Mate Mrse. Running the .getExecutionSource() method would return me the Test Suite name when running a Test Suite. However I needed to return the Test Case name.

I first created a Test Listener and added to the '@BeforeTestCase':

class TestCaseName {

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

This returns the path:

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

Then I've used the .substring() method to store the Test Case Name as a string

class TestCaseName {

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

Thank you @Mate Mrse




回答2:


You can use RunConfiguration.getExecutionSource() to get the full path to the test case being run.

And then you can do with that whatever you want. For example, to get the test case name you might do something like

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

Explanation:

The .getExecutionSource() method will give you a full path to your test case, something like C:\users\user.name\Katalon Studio\Test Cases\Test Case Name.tc (you might have something different).

Since you want only the last part, you can use Groovy to cut this string to something you like. So, I cut the string at the place of the last \ (that is what the lastIndexOf is doing) just before the Test Case Name.tc (+1 because I want to cut the backslash as well).

Then the .substring() method will give me just what is leftover after the cut.



来源:https://stackoverflow.com/questions/57010304/how-to-get-test-case-name-in-katalon-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!