cancel
Showing results for 
Search instead for 
Did you mean: 

How to place a Variable into Xpath expression?

SOLVED
Highlighted
New Contributor

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: How to place a Variable into Xpath expression?

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.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

4 REPLIES 4
Highlighted
Community Hero

Re: How to place a Variable into Xpath expression?

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?

Highlighted
Community Hero

Re: How to place a Variable into Xpath expression?

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.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

Highlighted
New Contributor

Re: How to place a Variable into Xpath expression?

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

Highlighted
Community Hero

Re: How to place a Variable into Xpath expression?

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 + '*';

...

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors