Forum Discussion

avantijoshi's avatar
avantijoshi
New Contributor
2 years ago

Can we parameterize xpath in Test Complete

I wanted to make a dynamic xpath for an object in such a way where I can pass value through parameter.

Ex.  //a[text()='City']    -> And City value suppose to pass through parameter. Can anyone guide me on this?

3 Replies

  • Kitt's avatar
    Kitt
    Regular Contributor

    You can do this in several ways. If you use Name Mapping, you can pass a Project variable into your object identifiers as //a[text(), '%projectVar1%'] (NOTE: you can only use Project variables and NOT Project Suite variables in Name Mapping). The other methods of implementation might depend on your project setup and whether you are using scripting, keyword tests, or BDD for example - but in the same way allow you to use Project/ProjectSuite variables or pass in your own parameters or arguments into your test. Here are a few of those examples below:

     

    SCRIPT

    function findDynamicObject (var1) {
       Sys.Browser("*").Page("*").FindElement("//a[text(), '" + var1 + "']").Click();  
    }
    
    function testDynamicObject() {
      findDynamicObject("City");
      findDynamicObject("State");
      findDynamicObject("Zip");
    }

     

    BDD

    // bdd feature file (gherkin/cucumber)
    Scenario Outline: Find a Dynamic Object
      Given The user is logged into the application
      When The user clicks on the dynamic object containing <text>
      Then The user is redirected to somewhere
    Exmaples:
      | text    |
      | "City"  |
      | "State" |
      | " Zip"  |
    
    // bdd script
    Given("The user is logged into the application", function() {
      // open application and log in
    });
    When("The user clicks on the dynamic object containing {arg}", function (param1){
      Sys.Browser("*").Page("*").FindElement("//a[text(), '" + param1 + "']").Click(); 
    });
    Then("The user is redirected to somewhere", function() {
      // verify redirect or somehting else
    });

     

    KEYWORD

    see Smartbear [reference].

    • avantijoshi's avatar
      avantijoshi
      New Contributor

      Hello,

       

      Thank you for quick response.

      Basically I tried by the way you suggested. In my project we are majorly focusing on following 99% Keyword Test.

      For that I made the generic xpath ,created Project level variable, pass that variable through Keyword Value but now its showing me an error as below. 

      My xpath is : 

      //a[text()='%AddRemoveQuoteNumber%']/..//preceding-sibling::td/input

       

      Can you please suggest how to add Variable name in Xpath, since I tried before by the given way as below,

      //a[text(), '%projectVar1%'] 

       

       

       

       

      • Kitt's avatar
        Kitt
        Regular Contributor

        Could you possibly share a screenshot of the object selectors in the project's Name Mapping, as well as the project variables? If your project's local variable is not set when TC attempts to find the mapped object, it would throw that error - or if you have not saved and updated your changes. Try refreshing the mapped object and see if TC can find it using the project variable (you need to have the tested app open and the object visible before you refresh)

        This might also depend on what type of project variable you are using. Here's a good post about that [here]. And a good reference for working with project variables in keyword tests [here].