jeetpal
jeetpal

Reputation: 3

Performance comparison of @FindBy() and driver.findElement()

I am using selenium framework to automate a web application wherein basically, driver.findElement() is commonly used. but I got the suggestion that @FindBy() works faster than driver.findElement(). suggest me which is better to use ?

Upvotes: 0

Views: 373

Answers (1)

Grasshopper
Grasshopper

Reputation: 9058

When the code - PageFactory.initElements(....) is run it creates Proxy objects for all the fields which are annotated with @FindBy (or even @FindBys and @FindAll). So initially no searching of WebElements are performed.

Then if something like element.sendKeys(...) is run, the actual WebElement is searched using driver.findElement(...) before sendKeys() is run. Then if the same sendKeys() code is run the element is found again.

But if you add the CacheLookup annotation to the field then the second lookup is not performed but element is returned from the cache. So there is a performance gain. But the problem occurs in a javascript or ajax heavy page, stale element exception can show up.

For any non-trivial application testing use the PageObjectModel framework. Makes things organized and not littered with findElements and locators, even if you do not use the CacheLookUp annotation.

Upvotes: 2

Related Questions