How to locate Web elements in XLT scripts


#1

How to locate Web Elements properly ? While generating scripts, click() is containing xpath value by default. right ?
But while running , I am getting the following error…!

No element found for locator: <button id=“add-to-cart” type=“submit” title=“Add to Cart” …

Then i passed getWebDriver().findElement(By.id(“add-to-cart”)) as string value…then also i m getting the same error…!

Is there any better suggession ?


#2

Could you please describe what you do? Do you use the Script Developer and generate Java code or do you execute Script Test Cases or do you write the stuff manually? Please also note that that the default execution takes place using HtmlUnit which emulates an browser but is not 100% correct in terms of CSS or JavaScript. If you record in Firefox and you want to reply in Firefox, use the FirefoxDriver.


#3

I m using XLT Script Developer plugin in Firefox only.
There i put the tick to Generate wrapped JUnit class-and export it in Eclipse XLT testsuite-template project as per your docs.

I found a solution by appending click() at last as seen in selenium scripts.

getWebDriver().findElement(By.id(“add-to-cart”)).click();

But Is that the proper way to locate that webdriver in XLT ?


#4

The JUnit-Wrapper classes are pure stubs that delegate all work to XLT’s script interpreter which parses and executes the commands contained in the XML file (the test script). These wrapper classes are overwritten each time you save your script in Script Developer; thus any modification you make there will get lost upon save.

According to your code snippets I assume that you want to code the tests by yourself, or customize them at least. So, you will need to export your tests to Java using XLT’s Scripting API.
Then, you can simply do

click(id=add-to-cart)

instead of

getWebDriver().find(By.id('add-to-cart')).click()

All commands that you can use in Script Developer can also be used in XLT’s Scripting API. And each command that interacts with elements support element locators. Per default, elements are located by ID-or-XPath which means that the passed string is interpreted as ID first and in case no element was found, it is interpreted as XPath.