Forum Discussion

Asubbiah's avatar
Asubbiah
Contributor
9 years ago
Solved

can i pass header value from datasource?

Hi

 

I have a grid data source, where i pass the properties: usertype and user name

The usertype should be passed as a header value for my REST service.

I tried to write an event code for filtering based on teh datasource property. But it fails.

 

datasource: userDS

 

usertype  Z Y X

condition true true false

 

event : filter.request

 

 

def TC= context.testCase.name.contains ("myTC")
def headers = new StringToStringMap()

 

if(myTC)
{
if((context.testCase.getTestStepAt (7).getLabel()).contains("myteststep"))
{
if('${#userDS#usertype}' == 'Z')
{
headers.put('id','${#Project#id}')
headers.put('usertype', '${#userDS#usertype}')
headers.put('valid', '${#userDS#condition}')

}

else if('${#userDS#usertype}' == 'Y')
{
headers.put('id','${#Project#id}')
headers.put('usertype', '${#userDS#usertype}')
headers.put('valid', '${#userDS#condition}')

}

else if('${#userDS#usertype}' == 'X')
{
headers.put('id','${#Project#id}')
headers.put('usertype', '${#userDS#usertype}')
headers.put('valid', '${#userDS#condition}')

}

}

}

 

  •  

    Header value is a list type. You are putting a string.

    Here is an example code snippet, which adds the headers to the given request based on step name.

     

    Issue #1:

    ${#userDS#usertype} -- this can't directly be used. Instead use context.expand('${#userDS#usertype}')

    Similarly other instances in your script.

     

     

    Also another improvemement in the code is you can use a list to check instead of repeating the code for three times.

     

     

    def userTypesLookup = ['X', 'Y', 'Z']
    if (userTypesLookup.contains(context.expand('${#userDS#usertype}'))) {
    
         //do the stuff
    
    }

     

1 Reply

  • nmrao's avatar
    nmrao
    Champion Level 3

     

    Header value is a list type. You are putting a string.

    Here is an example code snippet, which adds the headers to the given request based on step name.

     

    Issue #1:

    ${#userDS#usertype} -- this can't directly be used. Instead use context.expand('${#userDS#usertype}')

    Similarly other instances in your script.

     

     

    Also another improvemement in the code is you can use a list to check instead of repeating the code for three times.

     

     

    def userTypesLookup = ['X', 'Y', 'Z']
    if (userTypesLookup.contains(context.expand('${#userDS#usertype}'))) {
    
         //do the stuff
    
    }