Page Object Model in Selenium C#
Selenium
test automation scripts become more complex as the web applications add more
features and web pages. With every new page added, new test scenarios are
included in the Selenium test automation scripts. With this increase in LOC
(Lines of Code), code maintenance can. Become very tedious and time-consuming.
Also, the Repetitive use of web locators and their respective test methods can
make the test code difficult to read.
Instead
of spending time updating the same set of locators in multiple Selenium test
automation scripts, a design pattern such as the Page Object Model can be used
for the development and maintenance of code.
In
this Selenium C# tutorial, I’d implement Page Object Model is a widely used
design pattern in Selenium that:
·
Minimizes the usage of duplicated code.
·
Makes the code more readable & maintainable as
there is immense focus on aspects such as reusability, extensibility, etc.
What is Page Object Model (POM) In Selenium C#?
Page
Object Model in Selenium C# is a design pattern that is extensively used by the
Selenium community for automation tests. The basic design principle that the
Page Object Model in Selenium C# follows is that a central object repository
should be created for controls on a web page. Hence, each web page will be
represented by a separate class.
The
Page Objects (or page classes) contains the elements of the corresponding web
page along with necessary methods to access the elements on the page. Hence,
Selenium test automation implementation that uses the Page Object Model in
Selenium C# will constitute different classes for each web page thereby making
code maintenance easier.
For
example, if automation for a login page & check-out page is to be
performed, our implementation will have a class each for login & check-out.
The controls for the login page are in the ‘login page’ class and controls for
the check-out page are in the ‘check out page’ class.
The
Selenium test automation scripts do not interact directly with web elements on
the page, instead, a new layer (i.e. page class/page object) resides between
the test code and UI on the web page.
In
complex Selenium test automation scenarios, Selenium test automation scripts
based on Page Object Model can have several page classes (or page objects). It
is recommended that you follow a common nomenclature while coming up with file
names (representing page objects) as well as the methods used in the
corresponding classes.
Advantages of Page Object Model in Selenium C#
Below
are some of the major advantages of using the Page Object Model in Selenium C#
·
Better Maintenance – With
separate page objects (or page classes) for different web pages, functionality
or web locator changes will have a less impact on the change in test scripts.
This makes the code cleaner and more maintainable as Selenium test automation
implementation is spread across separate page classes.
·
Minimal Changes Due To UI
Updates – The effect of changes in the web locators
will only be limited to the page classes, created for automated browser testing
of those web pages. This reduces the overall effort spent in changing test
scripts due to frequent UI updates.
·
Reusability – The
page object methods defined in different page classes can be reused across
Selenium test automation scripts. This, in turn, results in a reduction of code
size as there is the increased usage of reusability with Page Object Model in
Selenium C#.
Apart
from these advantages of Page Object Model in Selenium C#, another plus point
of using this design pattern I’d like to point out in this Selenium C# tutorial
is that it simplifies the visualization of the functionality and model of the
web page as both these entities are located in separate page classes.
Take this
certification to master the fundamentals of Selenium automation testing
with C# and prove your credibility as a tester.
What is Page Factory?
Page
Factory, an extension to Page Objects, is primarily used for initialization of
the web elements defined in the page classes (or page objects). Web elements
used with Page Objects have to be initialized before they can be used further
and Page Factory simplifies the initialization with the initElements method.
Shown
below are some of the ways in which initElements function can be used:
Method 1
Method 2
Method 3
Using @FindsBy annotation, every WebElement variable is initialized by the Page Factory based on the locators configured to locate the element on the web page
Each
time a method is called on the WebElement variable, e.g. elem_search_text in
the code snippet above for this Selenium C# tutorial, the Selenium WebDriver
will find the element on the current web page before simulating the UI
interaction.
The
@CacheLookup annotation is used to cache the looked up field that was earlier
read using the @FindsBy annotation. This annotation is very useful in scenarios
where we know that the elements on the webpage can always be found, and we
won’t be spending much time on that particular webpage, i.e. we would
eventually navigate to a new page.
The
@FindsBy annotation supports many other strategies; however, most of these
strategies are not relevant in the context of cross browser testing or
automated browser testing. More details about Page Factory are available on the official
page of Page Factory.
How
to Implement Page Object Model in Selenium C#
For Selenium
test automation script development, we use the Visual Studio IDE (Community
Edition) which can be downloaded from here. In case you want to know more
about setting up selenium in visual studio, you can refer to our previous
article on the topic.
Page Object Model – Directory Structure
Though
the choice of the directory structure purely depends on the test requirements,
it is important that a uniform structure is followed so that there are no
ambiguities in development & enhancements.
We
use the directory structure shown below for demonstrating the usage of the Page
Object Model with C# and Selenium.
As
shown in the structure above, the directory
Project-Directory\Src\PageObject\Pages contains the Page Classes/Page Objects
for the different web pages. The directory Project-Directory\Test\Scripts
contains the actual test code implementation. The file(s) present in this
directory will use the C# test framework (NUnit/xUnit/MSTest) for automated
browser testing.

Comments
Post a Comment