Converting legacy functions to ECMAScript
ECMAScript follows its own string parsing rules. Hence, while converting legacy functions to ECMAScript, ensure that you adhere to the following guidelines:
- All strings must be enclosed in quotation marks, with appropriate escape sequences:
- Quotation marks within the value of the string must be preceded by a backslash (\")
- Backslashes must be preceded by another backslash (\\) Example legacy function:
setTag(testFileName, C:\myfile.txt)After conversion to ECMAScript:
orsetTag("testFileName", "C:\\myfile.txt")testFileName = "C:\\myfile.txt"
- Replace the
null()function with thenullkeyword.Example legacy function:setTag(testTag, null())After conversion to ECMAScript:
orsetTag("testTag", null)testTag = null - Although the
setTag()function is still valid in ECMAScript, you can assign values directly instead:Example legacy function:setTag(testTag, newValue)After conversion to ECMAScript:testTag = "newValue"ortags["testTag"] = "newValue"Note: A direct reference to a tag namedidmust be qualified withtags. In ECMAScript you can reference a tag with:
When the tag name istags["myTagName"] tags.get("myTagName") tags.myTagName myTagNameid,tags.idresolves correctly during execution. - Similarly, you can replace the legacy
add()function with an operator:Example legacy function:add(%%testTag%%, 1)After conversion to ECMAScript:testTag + 1 - ECMAScript functions do not recognize double percent signs (%%).
Therefore, when you write a function, do not enclose the tags in double
percent signs as you would in a legacy function. HCL OneTest™ API
tags are automatically exposed as ECMAScript local variables.
- In most cases, you can treat the tag name as a local variable: Example legacy function:
resetTags(%%variable%%)After conversion to ECMAScript:resetTags(variable)Example legacy function:xpath(%%xml%%, %%xpathString%%)After conversion to ECMAScript:xpath(xml, xpathString) - Tag names that include symbols or reserved words can cause problems;
in those cases use the tags["tagname"] style
of notation.Same example, after conversion to ECMAScript:
xpath(tags["xml"], tags["xpathString"])
- In most cases, you can treat the tag name as a local variable:
