Forum Discussion

radhika1's avatar
radhika1
Contributor
6 years ago

how to use a variable in context.expand

Can u tell me how to use a variable inside this.

 

def y =1;

 

def g  = context.expand('${Operation#Response#//*//consumerProfile/phoneNumber["+y+"]}');

 

this is not taking 1 as value of y

  • JHunt's avatar
    JHunt
    6 years ago

    Some other problem with this Xpath then?

     

    def y = 1
    assert '${Operation#Response#//*//consumerProfile/phoneNumber[' + y + ']}' == 
    '${Operation#Response#//*//consumerProfile/phoneNumber[1]}'
  • JHunt's avatar
    JHunt
    Community Hero

    It is already mostly correct, just need to be consistent about the style of quotes:

     

     

    def g  = context.expand('${Operation#Response#//*//consumerProfile/phoneNumber[' + y + ']}');
    // ^ ^

     

      • JHunt's avatar
        JHunt
        Community Hero

        Some other problem with this Xpath then?

         

        def y = 1
        assert '${Operation#Response#//*//consumerProfile/phoneNumber[' + y + ']}' == 
        '${Operation#Response#//*//consumerProfile/phoneNumber[1]}'