When are TestObjects bound to an element?

At what point in the process of creating and using a TestObject (object repo → instantiation with findTestObject → use in an actual method like WebUI.click()) does it actually search for a page element? This came up in a discussion about using “final” with TestObjects and whether that would cause it to become stale upon page refresh. Beyond that though, I wanted to know more generally how TestObjects work “under the hood.”

I know that TestObjects wrap WebElement, so I guess another way to ask this would be “at what point does a TestObject call driver.findElement() to get a WebElement object)?”

No. An instance of TestObject class never calls driver.findElement(). A TestObject instance is merely a container of XPath string. It looks just like the Selenium’s well-known By class.

You can read the source of WebUI.verifyElementPresent(TestObject to, ) keyword here.

And you can trance down into:

and ultimately to this

A Keyword class internally calls WebUICommonHelper#findWebElementsWithSmartXPath(), which (at Line#1034) gets an XPath locator out of a TestObject, creates a By object, and it uses the By object to call driver.findElement().

1 Like