A great design pattern for separating Web Page elements and actions from actual tests is the page object model. Tests won’t have to worry about creating or locating WebElements this way. Annotations @FindBy and @FindAll are used to identify WebElements in Page Objects. A very significant and underutilised annotation that can speed up our tests is @CacheLookup.
As its name suggests, @CacheLookup aids in managing when to cache and when not to cache a WebElement. When this annotation is used on a WebElement, Selenium is told to store a cache of the WebElement rather than constantly looking for it on the WebPage. We can save a ton of time by doing this.
A test automation framework often implements a user-designed Page Object Model. It’s vital to remember that POM describes the Object repository, which is independent of the test designs in every way. This POM creates a centralised object repository environment so that we can easily alter the objects whenever the company proposes a new UI design. We also talked about the main benefits of developing automation tests using the Page Object Model as opposed to alternative methods.
Instead of being a user-designed POM, Page Factory Model is directly implemented in the Selenium framework. One of the most cutting-edge methods for managing the central page object store is this one.
In this article, we’ll talk about @CacheLookup’s advantages and try to put a number on how much efficiency this annotation improves. The fundamentals of the @CacheLookup annotation, however, will not be covered in this lesson. Check out the selenium training courses to learn more about @CacheLookUp and Page Object Model.
Analysis of performance with @CacheLookup
Now let’s look at how we can speed up our tests by avoiding the extra lookup request. The clever individuals on the Selenium WebDriver team were aware of this issue and have a fantastic fix. We now have the @CacheLookup annotation from them. Selenium will only return the cached version of a WebElement if it is annotated with this annotation. Selenium will not attempt to search for the WebElement on the Webpage. The cached version is created in the very first lookup to the Web page, after the first lookup, all other lookup requests will be filled by the cached element.
How To Utilise Cache In Selenium For Faster Execution In Page Factory Mode
When you are working on the majority of the static content of the website, a cache is advantageous. WebDriver will first determine whether the same cached element is present before proceeding with the action, so it won’t need to load the cached element repeatedly. To implement the cache of static components in our Selenium project, we use the @CacheLookup annotation.
What does the Page Factory Model’s @CacheLookUp Annotation do?
When the DOM is static, we inform WebDriver via the Page Factory Model to maintain a reference to the static objects so that it won’t need to reload the cached objects and can instead carry out the action immediately. Here, the initElements() method is crucial for storing the cached data. In the tutorial’s following section, we will go into more detail about this approach.
Due to the high level of dynamic nature of AJAX-based applications, the @CacheLoopup method will not function. Therefore, we use AJAXElementLocatorFactory to work on the AJAX-based application.
When to use and when not to use @Cachelookup annotation
Using the Cached version of WebElement is advantageous, but not for all elements. Let’s attempt to comprehend these two crucial points.
1.Stale Element and Stale Element Exception
Even though it can be tempting to use the @CacheLookup annotation on every element, dynamic elements should not be covered by it. We refer to elements that frequently renew themselves as dynamic elements. For instance, a timer text that updates its value once every second. Another illustration may be a stock ticker that updates every few seconds. These components are poor picks for the @CacheLookup annotation.
The explanation is fairly straightforward: because these web page elements change frequently, caching is not a good option for them. We will get a Stale element error if we cache one version of an element and it changes a few seconds later.
2.Static Element
When loading a web page, static elements @CacheLookup are very helpful for items that don’t change on the page. The majority of the items on the web page are of this sort. Therefore, to speed up the test execution for such elements, as they won’t change, we should utilise the @Cachelookup annotation.
ConclusionÂ
Understanding @CacheLookUp and what it implies to Selenium is vital for a Selenium tester. A good Selenium training will allow you to learn more about this topic.