First Step – Understanding a Page Object
As we all know web page mainly consists of 3 elements
1. Elements which are used for inputs like textbox, checkbox, radio buttons etc.
2. Elements on which actions can be performed like buttons, links, or any clickable objects on webpage
3. Static items on webpage like text or any images etc..
Now most of your test cases are around these elements and their expected functionality on a webpage.
We need to have a mechanism by which we should be able to access all these elements by defining a single object of a webpage and using it to do all the actions as we required.
Here is the thumb rule,
1. All the input elements we will define as properties in which we can use getters and setters
2. All the elements on which we can perform action, we will create a function for it.
3. Static text and Images we don’t have to do anything about it because we can test it anyhow.
Let’s take example of Google Search page
As you can see in a picture, we have elements which can be clicked and in which we can enter the values
Let’s create a page object for it. Please see the code below for it.
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium;
public class GoogleSearchPage
{
protected IWebDriver browser;
public GoogleSearchPage()
{
browser = new InternetExplorerDriver();
}
public string SearchText
{
get
{
return browser.FindElement(By.Name("q")).Value;
}
set
{
browser.FindElement(By.Name("q")).SendKeys(value);
}
}
public void ClickSeachButton()
{
driver.FindElement(By.Name("btnG")).Click();
}
}
Now whenever we will create a object for GoogleSearchPage we will have a ready property for searching a text and at the same we also defined a function for clicking a button.
Now how defining a function will help instead of directly using a single line code inside it.
When we are defining a function for actions we can define multiple actions to be performed under single function avoiding repetitive steps to go into test script
Lets take example of gmail login. Please see the PageObject Gmail Login
public class GoogleMailPage
{
protected IWebDriver browser;
public GoogleMailPage ()
{
browser = new InternetExplorerDriver();
browser.Url = http://www.gmail.com
}
public string Username
{
get
{ return browser.FindElement(By.Name("Email")).Text;
}
set { browser.FindElement(By.Name("Email")).SendKeys(value); }
}
public string Password
{
get { return browser.FindElement(By.Name("Passwd")).Text; }
set {
browser.FindElement(By.Name("Passwd")).SendKeys(value);
}
}
public void LogIn(string username, string pass)
{
Username=username;
Password = pass;
browser.FindElement(By.Name("signIn")).Submit();
}
public void LogOut()
{
browser.FindElement(By.LinkText("Sign out")).Click();
// m_browser_IE.FindElements("");
}
|}
Now lets say we have to create a test script for login using above code how I will create it
It simple and will involve minimum steps as below
GoogleMailPage Gpage = new GoogleMailPage()
Gpage.Login(“test”,”12345”)
See how simple and clean test script we have prepared.. This is easy to maintain and with less code inside it.
Now you got the concept of PageObject.. if you are clear on it then we will understand the concept of automation library which we will be designing using page object… in next post.
No comments:
Post a Comment