Forum Discussion

ak0032803's avatar
ak0032803
Contributor
14 years ago

How to get Test Steps names in Scripts

Hi All,

I am trying to save response for my test request into a file.
I have mulitple test requests for which i need to store responses.
e.g. I have two test request in my test cases as
Step 1 - Check Data_Positive_Scenario
Step 2 - Check Data_Negative_Scenario
After running these two request i am storing response in file for each step using following code

def StepNames = "Check Data_Positive_Scenario"
def Path = "C:/.../Test/"
//Call Method to save response
SaveMyResponseToFile(StepNames , Path )
//Save Response to file with current date and time
def SaveMyResponseToFile(StepName, FilePath){
date = new Date()
dateFormat = new java.text.SimpleDateFormat('yyyyMMdd-kkmmss')
shortDate = dateFormat.format(date)
respFilename = shortDate + "-" + StepName + "-response.xml" // implicitely returned
log.info("File Name Generated--" +respFilename)
def file_Name ;
file_Name = FilePath + respFilename
def File = new PrintWriter (file_Name)
log.info("After Filaname" + file_Name)
def response = testRunner.testCase.testSteps[StepName].testRequest.response.contentAsString
log.info("Response Written")
File.println(response)
File.flush()
File.close()
if (log.isInfoEnabled())
log.info("Saved response in file: " + respFilename)
}

Is there any way so that I dont need to put test request name manually?
i am expecting something like when the test case will run for all test requests my sript will store response for each request automatically.
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    To get the name of the current Test Step, use this:

    tstep = testRunner.runContext.currentStep.name
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    Hi,

    You can get the name of the test step by calling getName() method. testRunner has information about the currently running thread and you would be able to access the current running testStep or testCase like below:

    tstep = testRunner.testCase.testStep.getName();
    tcase = testRunner.testCase.getName();

    You can now access any property from the test case/test step. For example:

    response = tstep.getProperty("Response")
  • Thanks for your help.
    But the code for Test case name worked fine but for test step ame it is throwing erorr as
    No such property testStep for class.
    Please suggest.
  • deepesh_jain's avatar
    deepesh_jain
    Frequent Contributor
    You can access response fo the test step like below as well:

    def groovyUtils1 = new GroovyUtils (context) ;
    holder = groovyUtils1.getXmlHolder("Check Data_Positive_Scenario#Response") ;
    def resp = holder.getDomNode("root node");

    Populate the root node with the xpath. For example if your response structure is like:
    <CheckResponse>
    <Response1>
    ...
    ...
    </Response1>
    </CheckResponse>

    Then your root node would be:
    "\\ns1:CheckResponse"

    where ns1 would be your name space.

    This would give you the complete response.