Forum Discussion

tony_forde's avatar
tony_forde
New Contributor
2 years ago

How to passing text project variable to xpath in name mapping

Hi,

 

I'm trying to parameterise the xpath for an object in my name mapping. The default value in name mapping is:

 

//span[text()='Theme 1']

 

In my web app, I have dropdowns for "Theme 1", "Theme 2", "Theme 3"... so I've set this up as a project variable:

 

Project.Variables.Theme

 

In my test I set the project variable to "Theme 1", "Theme 2", "Theme 3"... whatever required.

 

And in the name mapping I've tried, unsuccessfully, to use this:

 

//span[text()='" + Project.Variables.Theme + "']

 

But it doesn't work ğŸ˜ž Any ideas?

 

Thanks in advance,

 

Tony

4 Replies

  • Carol665yn's avatar
    Carol665yn
    Occasional Contributor

    McKesson Ordering

    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.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    In script code, you can do

    function Test()
    {
        // Project.Variables.Name = "Theme1"
        var strXPath = aqString.Format("//span[text()='%s']", Project.Variables.Name);
        Log.Message(strXPath);
    }

    But it's not possible to do //span[text()='" + Project.Variables.Theme + "'] in Name Mappings. TC will just search that entire property value.

  • tony_forde's avatar
    tony_forde
    New Contributor

    Hi folks, thanks for the suggestions. I can set a project variable to required xpath, e.g.

     

    Project.Variables.Theme1 = "//span[text()='Theme1']"

     

    But the problem is populating this variable to the name mapping like below. I've tried everything. 

     

     

    Is it even possible in TC? Seems crazy not to be able to manipulate dynamic xpath.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Can you try this (enclose the variable in %)

    or

     

    Replace a selector or the part of it with the project variable name, in the following format: %var_name%. If the specified variable does not exist in your project, your test will report an error.