Introduction
Page Object Model, also known as POM, is a design pattern in Selenium which has gained more popularity in the market for test automation development for the maintenance of code, such as reusability, extensibility, and avoiding code duplication etc.
Page Object Model is used for creating Control properties or Object Repository for controls on a webpage. For each webpage which we want to automate there should be a separate class such as if we are performing the automation for the login page, we need to maintain all the login page control properties in the separated class file. If we consider a login page, there are controls available in a login page such as UserName, Password, Login, ForgetPassword etc.
Each control will have unique control properties such as Name, Control ID, TagName, XPath, ClassName, CssSelector etc. To get the control properties we need to inspect the element by clicking F12 or right the webpage and clicking on Inspect Element.
There we can hover over the control to find a control property for each control. If the control doesn't have a control property then we need to use XPath. In order to take XPath right click on the control's element which is in the Inspect Element window, then click on Copy-->Copy XPath.
If we want to automate a login page then we need to create a separate class as LoginObjectProperties and maintain all the login related control properties in LoginObjectProperties class.
Similarly, if we want to automate the registration page, we will have control under registration page like Enter Email, FirstName, LastName, City, ZipCode, Country etc. For registration page, we need to create a separate class called RegisterationObjectProperties and keep all the registration related control properties in RegisterationObjectProperties class.
The maintenance of a separate class for object properties or control properties is used for reducing the duplication of code as well as to reuse the code.
Example For Page Object Model,
In the above program I am performing the automation on the C# Corner signup page with the help of Page Object Model design pattern. I have used control properties static class for declaring all the control properties related to C# Corner sign up page.
Similarly, in PageObjectModelMethods class, I am accessing Webdriver which I am passing from DriverInitialization class, userRegisteration() method.
In PageObjectModelMethods class I have the functionality of all the controls available in C# Corner signup page by accessing ControlProperties static class variables like below.
- public void SignUpClick() {
- webDriver.FindElement(ControlProperties.signupClick).Click();
- WaitTime();
- }
- public void EnterEmail(string userEmail) {
- webDriver.FindElement(ControlProperties.email).Clear();
- webDriver.FindElement(ControlProperties.email).SendKeys(userEmail);
- WaitTime();
- }
- public void FirstName(string userFirstName) {
- webDriver.FindElement(ControlProperties.firstName).Clear();
- webDriver.FindElement(ControlProperties.firstName).SendKeys(userFirstName);
- WaitTime();
- }
- public void LastName(string userLastName) {
- webDriver.FindElement(ControlProperties.lastName).Clear();
- webDriver.FindElement(ControlProperties.lastName).SendKeys(userLastName);
- WaitTime();
- }
In the above code once the webpage is navigated to C# Corner's home page I am performing a click on SignUp button by accessing the static class variable such as ControlProperties.SignupClick which contains the control's unique property. I am finding the control with FindElement() method and once the element is identified I am clicking on the control. Here I am performing waitTime for a control by using WebDriverWait class.
Similarly, for entering Email control, I am accessing the value as ControlProperties.email and finding the control with FindElement() method.
Once the element is identified, I am clearing the control if any value exists with Clear() method and performing SendKeys with the provided value.
Similarly, for FirstName and LastName, I am accessing the value from static ControlProperties class such as ControlProperties.firstName and ControlProperties.lastName and performing clear() and SendKeys() to the control.
- public void Password(string userPassword) {
- webDriver.FindElement(ControlProperties.password).Clear();
- webDriver.FindElement(ControlProperties.password).SendKeys(userPassword);
- WaitTime();
- }
- public void ConfirmPassword(string userConfirmPassword) {
- webDriver.FindElement(ControlProperties.confirmPassword).Clear();
- webDriver.FindElement(ControlProperties.confirmPassword).SendKeys(userConfirmPassword);
- WaitTime();
- }
- public void CountryDopDown(string countryName) {
- IWebElement countryDrop = webDriver.FindElement(ControlProperties.countryDropdown);
- countryDrop.Click();
- SelectElement select = new SelectElement(countryDrop);
- select.SelectByValue(countryName);
- WaitTime();
- }
- public void ZipCode(string userZipCode) {
- webDriver.FindElement(ControlProperties.zipCode).Clear();
- webDriver.FindElement(ControlProperties.zipCode).SendKeys(userZipCode);
- WaitTime();
- }
In the above code, in order to automate the Password, ConfirmPassword, CountryDropDown, and ZipCode, I have used the above methods. For each method, I am accessing the static variable which is declared inside the static ControlProperties class which has the control's unique property.
Once the control is identified, I am clearing the control with Clear/() method and entering the provided value using SendKeys() method.
- public void City(string userCity) {
- webDriver.FindElement(ControlProperties.city).Clear();
- webDriver.FindElement(ControlProperties.city).SendKeys(userCity);
- WaitTime();
- }
- public void SecurityQuestion(string userSecurityQuestion) {
- IWebElement securityQues = webDriver.FindElement(ControlProperties.securityQuestion);
- securityQues.Click();
- Thread.Sleep(3000);
- SelectElement select = new SelectElement(securityQues);
- select.SelectByValue(userSecurityQuestion);
- WaitTime();
- }
- public void SecurityAnswer(string userSecurityAnswer) {
- webDriver.FindElement(ControlProperties.securityAnswer).Clear();
- webDriver.FindElement(ControlProperties.securityAnswer).SendKeys(userSecurityAnswer);
- WaitTime();
- }
- public void Captcha() {
-
- }
- public void SendUpdates() {
- webDriver.FindElement(ControlProperties.sendMeUpdates).Click();
- WaitTime();
- }
- public void IAcceptTerms() {
- webDriver.FindElement(ControlProperties.iAccept).Click();
- WaitTime();
- }
- public void RegisterMe() {
- webDriver.FindElement(ControlProperties.registerMe).Click();
- WaitTime();
- }
In the above code for City, SecurityAnswer, I am doing the same thing I did for earlier controls. For securityQuestion, I am accessing the control property from static class ControlProperties. Once the control property is identified, I am passing IWebElement object reference to SelectElement class as a parameter. Once the list of values is identified, I am finding it with the given value.
Captcha controls cannot be automated with Selenium directly which uses some third party dll. SendUpdates and I Accept Terms are two checkboxes which can be clicked by using Click() method.
- public void HomeClick() {
- webDriver.FindElement(ControlProperties.homeClick).Click();
- WaitTime();
- }
- public void SearchClick() {
- webDriver.FindElement(ControlProperties.searchClick).Click();
- WaitTime();
- }
- public void AuthorSearchClick() {
- webDriver.FindElement(ControlProperties.authorSearchClick).Click();
- WaitTime();
- }
- public void AuthorFirstName(string userFirstName) {
- webDriver.FindElement(ControlProperties.authorFirstName).Clear();
- webDriver.FindElement(ControlProperties.authorFirstName).SendKeys(userFirstName);
- WaitTime();
- }
- public void AuthorSearchButton() {
- webDriver.FindElement(ControlProperties.searchBtn).Click();
- Thread.Sleep(3000);
- WaitTime();
- }
- public void ProfileClick() {
- webDriver.FindElement(ControlProperties.profileClick).Click();
- WaitTime();
- }
- public void ArticlesClick() {
- webDriver.FindElement(ControlProperties.articlesClick).Click();
- WaitTime();
- }
- public void FirstArticleClick() {
- webDriver.FindElement(ControlProperties.firstArticleClick).Click();
- WaitTime();
- }
- public void WaitTime() {
- WebDriverWait webDriverWait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
- }
From the above code, once the registration automation is done, I am clicking on the home button; then clicking on search button which is available on the top right. Once we will have two tabs that are Content and People. I am clicking on the People tab and giving the FirstName as provided by the user and clicking on the Search button. Once the author's profile is highlighted, I am clicking on the author's profile and clicking on Articles Link which displays a list of articles from those I am clicking on the First article which is available on the top.
- public class DriverInitialization {
- IWebDriver Webdriver;
- public DriverInitialization() {
- ChromeOptions chromeOptions = new ChromeOptions();
- chromeOptions.AddArgument("disable-infobars");
- Webdriver = new ChromeDriver(@ "D:\chromedriver_win32", chromeOptions);
- Webdriver.Navigate().GoToUrl("https://www.c-sharpcorner.com/");
- Webdriver.Manage().Window.Maximize();
- WebDriverWait webDriverWait = new WebDriverWait(Webdriver, TimeSpan.FromSeconds(40));
- }
- [TestMethod]
- public void UserRegisteration() {
-
- PageObjectModelMethods register = new PageObjectModelMethods(Webdriver);
- register.SignUpClick();
- register.EnterEmail("[email protected]");
- register.FirstName("Khaja");
- register.LastName("Moizuddin");
- register.Password("1234567890");
- register.ConfirmPassword("1234567890");
- register.CountryDopDown("India");
- register.ZipCode("500076");
- register.City("Hyderabad");
- register.SecurityQuestion("2");
- register.SecurityAnswer("Valuemomentum Software Services");
- register.SendUpdates();
- register.IAcceptTerms();
- register.RegisterMe();
- register.HomeClick();
- register.SearchClick();
- register.AuthorSearchClick();
- register.AuthorFirstName("Khaja");
- register.AuthorSearchButton();
- register.ProfileClick();
- register.ArticlesClick();
- register.FirstArticleClick();
- Webdriver.Quit();
- }
- }
From the above code in DriverIntialization class i am using DriverIntialization constructor for intializing the chrome driver by passing the chrome driver exe path and navigating to the C# Corner website and maximizing it using Maximize() method.
In UserRegisterationMethod() I am passing the web driver from PageObjectModelMethods class initialization of a class. This PageObjectModelMethods is assigning the web driver to its local Webdriver as Webdriver= webdriver; and, it can be accessed by all the methods inside the PageObjectModelMethods class.
Once the PageObjectModelMethods are initialized with the webdriver, I am calling each method of a PageObjectModelMethods class by using its reference variable as shown in the above code.
Conclusion
The use of the Page Object Model is that if any of the control properties or value is changed then we can change the value by going to the respective class without disturbing the other Classes or Methods.
With this design pattern approach, we can reduce code duplication and complexity and increases reusability and easy maintenance of code.
Thanks & I hope this helps you.