Page Object Model For Selenium WebDriver

 Click Here to  Download Complete Project


Page Object Model Framework in Selenium:

              Page Object Model Framework has now a days become very popular test automation framework in the industry and many companies are now adopting it because of its easy test maintenance and reduces the duplication of code. 
  • The main advantage of Page Object Model is that if the UI element / Controls changes for any page, it doesn't require us to change any Tests . 
  • We just need to change only the code within the page objects. This framework is mostly used with Selenium. 
  • It is one Design Pattern for Selenium Code.
  • It Provides the clean separation page code from Test code.

             In Page Object Model we are going to Create a Separate class for separate Web Page. In that class we are going to map UI elements as a object and their respective action on these controls such as "signIn" or "sign-Up" or "Forget password".

Each action on a page (controls) can be represented as a method in their respective class. so Finally we can say that - "Each page has respective class containing UI element(Locators of each element on that page ) and their respective actions as a "Method" in that class.  

See in a below screen shot-
        Here I am considering facebook applicatios "HomePage". I have Used POM framework to Test Facebook Application. I have written Three Test. we will see in later part......
 


UI Element and their Locators:

        static IWebDriver driver = null;
        static IWebElement element = null;
        //(UIObject) Locators for Login/SignIn
        static By userName = By .Id("email");
        static By Password = By .Id("pass");
        static By LogIn = By .XPath("//input[@type='submit'][@value='Log In']");
              
        // (UIObject) Locators for to Register a New User on Facebook Application
        static By FirstName = By .Id("u_0_1");
        static By LastName = By .Id("u_0_3");
        static By Email = By .Id("u_0_5");
        static By ReEmail = By .Id("u_0_8");
        static By NewPassword = By .Id("u_0_a");
        static By Month = By .Id("month");
        static By Day = By .Id("day");
        static By Year = By .Id("year");
        static By GenderFemale = By .Id("u_0_d");
        static By GenderMale = By .Id("u_0_e");
        static By SignUp = By .Id("u_0_i");

Respective Action on UI element:
         /// <summary>
        /// Set the user Name to be Logged in.
        /// </summary>
        /// <param name="UserName">
UserName to be Log-In</param>
        public static void setUserName(String UserName)
        {
            driver.FindElement(userName).SendKeys(UserName);
        }

        /// <summary>
        /// Set the Password, in the password field of facebook application.
        /// </summary>
        /// <param name="UserPassword">
Password of user wants to Log-in.</param>
        public static void setUserPassword(String UserPassword)
        {
            driver.FindElement(Password).SendKeys(UserPassword);
        }

        /// <summary>
        /// Clcik on Login In Button.
        /// </summary>
        public static void ClickLogin()
        {
            driver.FindElement(LogIn).Click();
        }

Advantages of Framework:

  • The biggest advantage of the software framework is that it reduces the time and energy in developing any software.
  • There is clean separation between test code and page specific code such as locators.
  • Code Maintainability
  • More Readability.
  • Less Error Prone.
  • Seperation of Concern.
  • Framework follow design pattern, so when you use these frameworks you’ve to follow their coding convention which makes your code clean and extensible for future purpose.
  • Framework separates business logic from user interface making the code cleaner and extensible.
  • Frameworks help you to develop the project rapidly, if you know one framework well then you’ll never worry about the project deadline.

How to setup Page Object Model In Visual Studio ( Selenium with C# & NUnit )

Pre-requisite:

  • Visual studio Any version (I have used 2012).
  • NUnit 2.6.4
  • Selenium webdriver (selenium-server-standalone-2.46.0.jar)

Steps to Create:

Open Visual Studio
1- Select New Project -> select Visual C# -> select Class Library. give Name as PageObjectModel.
     Add the  references for Selenium

2- Select the Solution and right click add->New Project->Test->Unit Test.
Add the references for NUnit Testing Framework.

Description:
  • The Class Library project contains the selenium code ( Webpage UI Element and their respective actions )
.
The Next, Test project contains the Test  code only.

  • The  standard says Your class Library should be depends on selenium and your Test should be independent.


Project Structure:
    See the below screen shot for Project structure in a project. 



=============================================================================================
Class library project Contains Following files:

Browser.cs : Common file used to set the Browser while executing the Test.

HomePage.cs: Contains the "Login" and "sign-Up" Actions

AfterLogin.cs: Contains the "UpdateStatusOnfacebook" & "Loggedout" actions.

RegisterPage.cs: Contains the "getTitle" action.

Test Project contains following files:

AfterLoginTest.cs: Test the functionality of Successful "Login" and after "UpdateStatusOnfacebook".

HomePage.cs:Test the functionality of "Login" successful and "RegisterNewUser".

Project Code:

File Browser.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;


namespace PageObjectModel
{
    public class Browser
    {
        static IWebDriver driver = null ;

        public static String BrowserName = null ;


        /// <summary>
        /// set the Browser in which Application should be opened.
        /// </summary>
        /// <param name="BName">Enter the Name of Browser</param>
        /// <returns>Newly created instance of Respective Browser</returns>

        public static IWebDriver SetBrowserName(String BName)
        {
            switch (BName)
            {
                case "firefox":
                    {
                        driver = new FirefoxDriver();
                        break;
                    }
                case "chrome":
                    {

                        driver = new ChromeDriver("G:\\Selenium\\LatestDriver\\chromedriver_win32");
                        break;
                    }
                case "ie":
                    {
                        driver = new InternetExplorerDriver("G:\\Selenium\\LatestDriver\\IEDriverServer_Win32_2.46.0");
                        break;
                    }
                default:
                    {
                        driver = null;
                        break;
                    }
            }
            return driver;
        }

        /// <summary>
        /// Close the current Browser/Application
        /// </summary>

        public static void Close()
        {
            driver.Close();
        }
    }
}


File HomePage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;


namespace PageObjectModel
{
    public class HomePage
    {

        static IWebDriver driver = null;
        static IWebElement element = null;
        //(UIObject) Locators for Login/SignIn
        static By userName = By .Id("email");
        static By Password = By .Id("pass");
        static By LogIn = By .XPath("//input[@type='submit'][@value='Log In']");


        // (UIObject) Locators for to Register a New User on Facebook Application
        static By FirstName = By .Id("u_0_1");
        static By LastName = By .Id("u_0_3");
        static By Email = By .Id("u_0_5");
        static By ReEmail = By .Id("u_0_8");
        static By NewPassword = By .Id("u_0_a");
        static By Month = By .Id("month");
        static By Day = By .Id("day");
        static By Year = By .Id("year");
        static By GenderFemale = By .Id("u_0_d");
        static By GenderMale = By .Id("u_0_e");
        static By SignUp = By .Id("u_0_i");


        /// <summary>
        /// Constructor to initialize set the browser for Homepage
        /// </summary>
        /// <param name="driverr">Tell us on which Browser the application is going to open</param>

        public HomePage(IWebDriver driverr)
        {
            driver = driverr;
        }

        /// <summary>
        /// Set the Browser on which the application is to be Run/Test.
        /// </summary>
        /// <param name="BrowserName">Name of browser to be Set</param>
        /// <returns>Instance of newely set Browser</returns>

        public static IWebDriver setbrowser(String BrowserName)
        {
            driver = Browser.SetBrowserName(BrowserName);
            return driver;
        }

        /// <summary>
        /// Set the URL of an Application to be Test
        /// </summary>
        /// <param name="url">URL of Your Application under Test.</param>

        public static void SetURL(String url)
        {
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl(url);
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        }

        /// <summary>
        /// Set the user Name to be Logged in.
        /// </summary>
        /// <param name="UserName">UserName to be Log-In</param>

        public static void setUserName(String UserName)
        {
            driver.FindElement(userName).SendKeys(UserName);
        }

        /// <summary>
        /// Set the Password, in the password field of facebook application.
        /// </summary>
        /// <param name="UserPassword">Password of user wants to Log-in.</param>

        public static void setUserPassword(String UserPassword)
        {
            driver.FindElement(Password).SendKeys(UserPassword);
        }

        /// <summary>
        /// Clcik on Login In Button.
        /// </summary>

        public static void ClickLogin()
        {
            driver.FindElement(LogIn).Click();
        }

        /// <summary>
        /// Click on Logged in Button after filling UserName and Password field.
        /// </summary>
        /// <param name="userName">User Name of User</param>
        /// <param name="Password">Password of User.</param>
        /// <returns>Return/Directed to the Next page after successful of Registering the User.</returns>

        public static AfterLogin ClickOnLogin(String userName, String Password)
        {
            setUserName(userName);
            setUserPassword(Password);
            ClickLogin();
            return new AfterLogin(driver);
        }

        /// <summary>
        /// Get the Title/Constant String from Home Page to Identify each page from each other.
        /// </summary>
        /// <returns></returns>

        public static String getTitle()
        {
            return driver.Title;
        }

        //II Test Part 
        // second part of Action in a HomePage is  Register a User

        /// <summary>
        /// Set the New User name to be Register.
        /// </summary>
        /// <param name="firstName">Enter First name of User</param>

        public static void SetRegisterUserFirstName(String firstName)
        {
            driver.FindElement(FirstName).SendKeys(firstName);
        }

        /// <summary>
        /// Set the New User Last Name to be Register.
        /// </summary>
        /// <param name="lastName">last Name of New User</param>

        public static void SetRegisterUserLastName(String lastName)
        {
            driver.FindElement(LastName).SendKeys(lastName);
        }

        /// <summary>
        /// Set the Email of New user for Verification.
        /// </summary>
        /// <param name="eMail">Enter Email of New user for verification</param>

        public static void SetEmail(String eMail)
        {
            driver.FindElement(Email).SendKeys(eMail);
        }

        /// <summary>
        /// Re-Enter the mail
        /// </summary>
        /// <param name="reEmail">Re-Enter the mail of new user </param>

        public static void SetReEMail(String reEmail)
        {
            driver.FindElement(ReEmail).SendKeys(reEmail);
        }

        /// <summary>
        /// Set the Password for Facebook login for new user
        /// </summary>
        /// <param name="newPassword">Enter New password</param>

        public static void SetNewPassword(String newPassword)
        {
            driver.FindElement(NewPassword).SendKeys(newPassword);
        }

        /// <summary>
        /// Select the Month from Drop Down List.
        /// </summary>
        /// <param name="month">Enter your Birth Month  </param>

        public static void SelectMonth(String month)
        {

            SelectElement select = new SelectElement(driver.FindElement(Month));
            foreach (IWebElement item in select.Options)
            {
                if (item.Text.Contains(month))
                    item.Click();
            }
        }

        /// <summary>
        /// select  the year of Your Birth from Drop DownList.
        /// </summary>
        /// <param name="year">Enter Your year of Birth.</param>

        public static void SelectYear(String year)
        {
            SelectElement select = new SelectElement(driver.FindElement(Year));
            foreach (IWebElement item in select.Options)
            {
                if (item.Text.Contains(year))
                    item.Click();
            }
        }

        /// <summary>
        /// Select Birth day from drop DownList.
        /// </summary>
        /// <param name="day">Enter your Birthday.</param>

        public static void SelectDay(String day)
        {
            SelectElement select = new SelectElement(driver.FindElement(Day));
            foreach (IWebElement item in select.Options)
            {
                if (item.Text == day)
                    item.Click();
            }
        }

        /// <summary>
        /// select Your Gender
        /// </summary>
        /// <param name="gender">select Male / Female</param>

        public static void selectGender(String gender)
        {
            String genderr = gender.ToLower();
            element = driver.FindElement(GenderMale);
            if (element.Text.ToLower() == genderr)
            {
                element.Click();
            }
            else
            {
                element = driver.FindElement(GenderFemale);
                element.Click();
            }
            element = null;
        }

        /// <summary>
        /// Click on signUp Button
        /// </summary>

        public static void ClickSignUp()
        {
            driver.FindElement(SignUp).Click();
        }

        /// <summary>
        /// Register  the new User with His/Her details.
        /// </summary>
        /// <param name="firstName">Name of User.</param>
        /// <param name="lastname">Last Name Of User.</param>
        /// <param name="eMail">Existing Email of User.</param>
        /// <param name="reEmail">Re-Enter the Existing E-mail.</param>
        /// <param name="newPassword">Enter New Password to access facebook Application</param>
        /// <param name="month">month of Birth.</param>
        /// <param name="day">Birth Day</param>
        /// <param name="year">Birth Year</param>
        /// <param name="gender">male/Female</param>
        /// <returns>Return or Redirect to register Page.</returns>

        public static Registerpage RegisterNewUser(string firstName, String lastname, String eMail, String reEmail, string newPassword, String month, string day, string year, String gender)
        {
            SetRegisterUserFirstName(firstName);
            SetRegisterUserLastName(lastname);
            SetEmail(eMail);
            SetReEMail(reEmail);
            SetNewPassword(newPassword);
            SelectMonth(month);
            SelectDay(day);
            SelectYear(year);
            selectGender(gender);
            ClickSignUp();
            System.Threading.Thread.Sleep(5000);
            return new Registerpage(driver);
        }


    }
}


Click Here to  Download Complete Project

 

See Also:

Post a Comment

0 Comments