Forum Discussion

roryLIT's avatar
roryLIT
Contributor
10 years ago

[Resolved] Pass a variable into context.expand code

I have a script assertion in a teststep. I want to get the response xml from the previous step to parse. The code to do this is currently like this:

def currentTestStep = context.getCurrentStep().getLabel()
def currentStepInd = context.currentStepIndex
def previousTestStep = context.testCase.getTestStepAt(currentStepInd - 1)
previousTestStep = previousTestStep.name

def response = context.expand( '${previousTestStep#ResponseAsXml#//Row[1]}' )
def records = new XmlSlurper().parseText(response)


Have I passed the previousTestStep variable in correctly?

Thanks

2 Replies

  • marcpa's avatar
    marcpa
    Occasional Contributor
    Not exactly, but close.

    Try this:

    def previousTestStep = context.testCase.getTestStepAt(context.currentStepIndex - 1)
    String propertySpec = '${' + previousTestStep.name + '#Response}'
    log.info "propertySpec : ${propertySpec}"
    def response = context.expand(propertySpec)

    log.info("context.expand(propertySpec) : ${response}")

    def document = new XmlSlurper().parseText(response)
    def allNodes = document.depthFirst().collect{ it }

    allNodes.each { node ->
    log.info "node named '${node.name()}' has content :"
    log.info node.text()
    }
    log.info "DONE"