Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @CacheLookup work in WebDriver?

I'm not sure that I understand the caching principle :

@CacheLookup
@FindBy(how = How.ID, using = namespace + signifLvl)
private WebElement sigLvl;

If we use this Annotation way, ElementLocator is being used and the first time one refer to the field, the element is found driver.findElement(by) and cached via ElementLocator, so that next time we refer to it, it is returned from the cache.

It looks it depends on the lifetime of the ElementLocator & PageObject instance.

Also it doesn't relate to direct driver.findElement(By); calls.

I'm assuming, that WebElement is like a pointer/reference to the element, right ? So that if the element changes in browser, it is reflected to the WebElement right away. As it is in JavaScript. Because all RemoteWebElement's methods regarding element's state are executing command/request to browser.

So that the changes are reflected even in the cached element, right ?

like image 837
lisak Avatar asked Jun 02 '11 14:06

lisak


3 Answers

Page Factory works on the principle of configuring the proxies when Page Factory is initialized and every time you use a WebElement it will go and search for the element.

Now what cachelookup does is it stores elements having @cachelookup annotation applied over it and then stores this element for further reference/s. For example:

  public class SearchPage {
 // The element is now looked up using the name attribute,
  // and we never look it up once it has been used the first time
  @FindBy(how = How.NAME, using = "q")
  @CacheLookup
  private WebElement searchBox;
  public void searchFor(String text) {
  // We continue using the element just as before
   searchBox.sendKeys(text);
  searchBox.submit();
   } }

What this annotation does is it stores the value the searchBox element and now there is no need to search for this element on webpage again.

like image 50
Khyati Sehgal Avatar answered Sep 29 '22 15:09

Khyati Sehgal


Imho the question should rather be : What is the element pointer/id is about ?

As WebElement doesn't have a state, only methods that call browser. @CacheLookup is only a shortcut for public WebElement el = driver.findElement(By); when initializing WebDriver's PageObject, for instance.

After you have the instance, you are executing its methods, that call browser.

The WebElement ID corresponds to a JS element instance. if you go like this on client JS :

var node1 = document.createElement('a');

and then append it somewhere, remove it from there, append it some place else, etc. and it is still the same node1 instance, the WebElement instance still points to the node1 element, because it is the same JS node instance.

like image 41
lisak Avatar answered Sep 29 '22 16:09

lisak


I know this answer is late in the party. I have been trying to understand @Cachelookup myself and came up with certain tests and conclusions. This topic is hard to explain in short here. So please take a look at the article which tries to understand the inner workings of @CacheLookup and also the performance improvements that we get by using this annotation. Article is on ToolsQA here

like image 22
virusrocks Avatar answered Sep 29 '22 14:09

virusrocks