Forum Discussion

cristiandiaconu's avatar
cristiandiaconu
New Contributor
4 years ago

How to place a Variable into Xpath expression?

Hello,

 

I want to modify an Xpath expression and include a Project Variable into it.

Instead of using //gub-beratercard/mat-card[contains(., 'Jonathan')], I want to replace 'Jonathan" with a variable from my project. 

I tried to use //gub-beratercard/mat-card[contains(., 'Project.Variables.ConsultantName')], with and also without quotes around the variable, but with no success.

Could you please help me find out how to place a variable into this Xpath expression?

 

Thanks!

  • Hi,

     

    Just concatenate your template XPath string with the value stored in the variable.

    E.g.:

    var strXPath = aqString.Format("//gub-beratercard/mat-card[contains(., '%s')]", Project.Variables.ConsultantName);

     

    And I agree with Marsha: try to avoid XPath in TestComplete unless you are considering cross-platform testing.

     

4 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Just concatenate your template XPath string with the value stored in the variable.

    E.g.:

    var strXPath = aqString.Format("//gub-beratercard/mat-card[contains(., '%s')]", Project.Variables.ConsultantName);

     

    And I agree with Marsha: try to avoid XPath in TestComplete unless you are considering cross-platform testing.

     

    • cristiandiaconu's avatar
      cristiandiaconu
      New Contributor

      Hi,

       

      Thanks for your answer!

      I`m not required to use Xpath, but it feels more natural and simple for me to use Xpath/CSS.

       

      So, if I want to avoid Xpath, I should be able to place a variable inside the Value of the contentText.

       

      I switched to the conditional and I saw that "contains" condition is not an option there, but for reaching this, I should use wildcard before/after the value. 

       

      My question here is: I am able here to place a variable + a wildcard? The options for the "contentText" value are Constant, Project Variable and Property, but nothing like code syntax where I can combine a variable + wildcard.

       

      Thank you in advance!

      Cristian

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        Correct, code expression is not an option for the value in NameMapping.

        I am not sure if you really need to use Conditional mode, but in order to emulate 'contains' you must append asterisk symbol to the value of the Project Variable used in NameMapping before referring to the object that uses this given variable.

        E.g.:

        Project.Variables.SomeVar = '*' + Project.Variables.SomeVar + '*';

        ...

         

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    1) I don't know if that's even possible in TestComplete.  How would XPath know to interpret that?  Maybe AlexKaras knows.

     

    2) Are you required to use XPath?