mcdcorp / opentest

Open source test automation tool for web applications, mobile apps and APIs
https://getopentest.org
MIT License
446 stars 107 forks source link

Dynamic locator #649

Open adrian2096 opened 2 weeks ago

adrian2096 commented 2 weeks ago

hi, im trying to make a dynamic locators like this

data/locators.yaml

DobleValue:
  newValue: {xpath: "//td[contains(@title,'${value}')]"}

the newValue locator is in the data/locators.yaml and im trying to use javascript to replace the '${value}' with a value that i send as a parameter, i try it to use like this but is not working, is there anyway how to make what i want?

 - script: |
      var locatorString = $data("data/locators").DobleValue.newValue
      var newLocator = locatorString.replace("${plan}",$macroArgs.plan)

      $runMacro("macro", {
        locator: newLocator 
      });

Thank you!

adrianth commented 2 weeks ago

The error you have in there is that $data("data/locators").DobleValue.newValue is a JavaScript object, not a string, so you cannot use the replace method on it. You need to do the replace on the xpath property instead:

- script: |
     var locator = $data("data/locators").DobleValue.newValue
     locator.xpath = locator.xpath.replace("${value}",$macroArgs.plan)

     $runMacro("macro", {
       locator: locator 
     });
adrian2096 commented 2 weeks ago

it works, thanks for the help