Thursday, January 16, 2020

Design Patterns used in Selenium with Java

Which has better Performance Page Object model or Page Factory .


Page Factory-

1) Import Package -

import package ‘org.openqa.selenium.support.PageFactory

2)We use Page Factory pattern to initialize web elements which are defined in Page Objects.

 CreateMerchandisepageobject createMerch = PageFactory.initElements(driver, CreateMerchandisepageobject.class);
 

3)Every time when a method is called on a WebElement, the driver will first find it on the current page and then simulate the action on the WebElement. There are cases where we will be working with a basic page, and we know that we will find the element on the page every time we look for it, In such cases we can use annotation ‘@CacheLookup‘ which is another annotation in page factory

 

As you have just seen it is possible to implement the page object model without any support from Selenium. Luckily Selenium does offer the possibility to create Page Objects by using the Selenium integrated Page Factory. With the Selenium Page Factory, you can make your page objects even shorter and you will get a performance improvement as Selenium handles most of the heavy lifting.
Let us take a look at how we can modify our Login Page object from above to use the Selenium Page Factory pattern. Please note that with the code below Selenium will cache the lookup of all elements. You would only do this if you are certain that the elements do not change during the lifetime of the page object. On an AJAX-heavy application, you should avoid this and have Selenium lookup the element each time it is accessed.


 1)Difference between Page Object and Page Factory -

Page Object -In Page Object we use By Class

public class BasePage {

private By username = By.id("username");
private By password = By.id("password");
private By loginBtn = By.name("loginbtn");




In Page Factory we use Annotation
@FindBy
@CacheLook - but it can also result in Stale element Exception or should not be used in Ajax calls
- in such cases we can use @Ajaxfactory.


public class BasePage {
  @FindBy(id= "username") private WebElement userName;
  @FindBy(id= "password") private WebElement password;
  @FindBy(id= "login") private WebElement loginBtn;

---------------------------*-----------------------------------------------------
Refer Links :
https://crossbrowsertesting.com/blog/selenium/selenium-design-patterns/




No comments:

Post a Comment