1

I am trying to take a screenshot of every Test Case and have it exported to a screenshot directory with its 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 of currently lives in the "TearDownTestCase" method located in Test Suites.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Qma
  • 111
  • 1
  • 8

2 Answers2

3

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

Qma
  • 111
  • 1
  • 8
1

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.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • could you explain: "substring(RC.getExecutionSource().toString().lastIndexOf("\\")+1)" I blindly just copied and pasted your suggestion and I'm getting the same output as I did before (it's grabbing Test Suite name rather than Test Case). – Qma Jul 15 '19 at 18:00
  • @Qma I edited the post with the explanation, you will need to play around with Groovy string methods to get the exact TC. Btw, what path do you get when you run the full command? – Mate Mrše Jul 16 '19 at 06:34
  • It's giving me a path to Test Suite vs Test Case ..\Katalon Studio\Test Suite\Test Suite Name.ts and not Test Case This what we'd expect anyway from the .getExecutionSource method. I was hoping if there was a work-around to get the Test Case name from having the method live in the Test Suite. I have a Test Suite that runs several Test Cases. At the end of every Test Case, I want take a screenshot before closing the window with having the screenshot named as the Test Case. – Qma Jul 16 '19 at 13:38