17
Selenium Page Object Patterns Test Automation Narayanan Palani

Selenium page objectpatterns

Embed Size (px)

Citation preview

Page 1: Selenium page objectpatterns

Selenium Page Object Patterns

Test AutomationNarayanan Palani

Page 2: Selenium page objectpatterns
Page 3: Selenium page objectpatterns

6.1 What is Page Object Design Pattern?

• Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication.

• A page object is an object-oriented class that serves as an interface to a page of your AUT.

• The tests then use the methods of this page object class whenever they need to interact with that page of the UI.

• The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change.

• Subsequently all changes to support that new UI are located in one place.

• Page Objects are meant to encapsulate the messy internal state of a page.

• In a simple form of Page Object Model, Create two packages – one will contain one java file which contains the objects of all the pages in an application and other package will contains all smoke test java files which will utilize those objects.

Page 4: Selenium page objectpatterns

6.2 Creating Objects In Page Object Design Pattern

• Create all the page objects in one java file (or create the objects on page level under one java folder )and then create the test file in other file (or under other java folder). There is a Page Factory in the support package that provides support for this pattern. Using Page Factory under test file, we can access these page objects in other java file.

Page 5: Selenium page objectpatterns

Page Factory

• In order to support Page Object Design Pattern. Web Driver Supports a library that contains a factory class.

• There is a Page Factory in the Support package that provides support for this pattern.

• Using Page Factory under test file, we can access these page objects in other java file.

• While creating any object, if you use the Page Factory , you can assume the fields are initialised, else NullPonterExceptions will be thrown.

Page 6: Selenium page objectpatterns

Contd..

Page 7: Selenium page objectpatterns

@FindBy and @FindBys Annotation

• Fields are decorated if and only if they have @FindBy or @FindBys annotation.

• Default search strategy "by id or name" that works for WebElement fields is hardly suitable for lists because it is rare to have several elements with the same id or name on a page.

• If you never use a WebElement field in a PageObject, there will never be a call to "findElement" for it.

Page 8: Selenium page objectpatterns

Contd…

Page 9: Selenium page objectpatterns

Contd…

Page 10: Selenium page objectpatterns

InitElement Methods

• Page Factory class provides four variations of InItElements methods which can be used based on the project requirement.

• Based on the requirement, we can use one or all of them in a single framework so that we can attain the respective functionality.

1. ElementLocatorFactory:

InitElements method will contain an “ElementLocatorFactory”.

ElementLocatorFactory: is used for providing the mechanism for fniding elements.methods.

Syntax:

• initElements(ElementLocatorFactory factory, java.lang.Object page)

Page 12: Selenium page objectpatterns

InitElement Methods

3 .Page Class

.InitElements method which will contain a page class.

Syntax:

• initElements(WebDriver driver,java.lang.Class<T> pageClass)

• Instantiate an instance of the given class, and set a proxy for each of the WebElement and List fields that have been declared, assuming that the field name is also the HTML element's "id" or "name". Means there will be an element that can be located using the xpath expression.

Page 14: Selenium page objectpatterns

6.3 Advantages of PODM

The following are the advantages of Page Object Design Pattern:

• There is clean separation between test code and page specific code such as locators (or their use if you’re using a UI map) and layout.

• There is single repository for the services or operations offered by the page rather than having these services scattered through out the tests.

In both cases this allows any modifications required due to UI changes to all be made in one place.

Page 15: Selenium page objectpatterns

6.3 Dis Advantages of PODM

The following are the disadvantages of Page Object Design Pattern:

• There is no separation between the test method and the AUTs locators (IDs in this example); both are intertwined in a single method. If the AUT’s UI changes its identifiers, layout, or how a login is input and processed, the test itself must change.

• The id-locators would be spread in multiple tests, all tests that had to use this login page.

Page 16: Selenium page objectpatterns
Page 17: Selenium page objectpatterns

REFERENCE