Before creating tests in Selenium, we should look for design
patterns to be followed in the test.
Defining Design pattern to be used in automation helps in easier maintenance of the project once the number of tests increases. Design Pattern should be used in such a manner that rework due to of any code changes are minimum, creating new test uses the existing code.
Following are a few strategies or design patterns used in
Selenium for making tests easier to create and maintain:
1. Page Object Model
2. Using Page Factory in Page object
3. Loadable components
We will discuss each of the patterns in details in next
articles, but to start with, we will brief on the design patterns in this
article.
When we write code for tests in Selenium, we can break down
the code in such a manner that code maintenance is better. Suppose we have two
tests. One tests for an image visible on home Page post login and another verifies
welcome text post logging in the application.
Writing the same code in two different tests will require code to be changed in two different places in case object property changes in login Page and at times in case we have large number of tests in the test suite, it will take considerable time to fix the automation code. We should refactor the code and create smaller private methods that are used in different tests.
So now, we can make change at one place and it will be
reflected in many tests by refactoring the tests.
In Page Object Model, we create individual classes for each of the pages with all the methods pertaining to the Page. These methods specific to a page covers both positive and negative scenarios specific to page.
Page Object Model reduces the duplication of code, improves readability and increases robustness of the test. Also the code is lot more maintainable, which is specifically useful in case properties of objects in the application changes frequently.
Page Factory uses the factory class from web driver’s support library to define objects in the page in a better and simpler manner. We declare some fields on a PageObject that are Web Elements or List<Web Element>.initialize the page objects.
We can use Loadable components in the Page design providing a standard way of ensuring that pages
are loaded successfully.
Together with this, we can
use best practices to ensure the quality of code is highly robust, reusable and
maintainable. We will discuss in details in upcoming articles how to code using
Page Object Model, using Page Factory, Loadable Components in Selenium.