Use of Page Object model:
- Page object model is the effective way to store the elements.So that we can overcome the headache of storing page elements in duplicate manner .No need to store the same element again and again.
- For Every page we will create individual class and we will store the elements for that particular page.
- For example Login button has same values in different pages .We can store once and will use multiple times. So that we can avoid duplicity of elements.
- While writing the scenarios we will call the particular page elements and we will do our scripting.
Let's have a clear structure of page object model implementation:
Example : I am going to write elements for BigBasket application.
- First of all will create a C# console project.And will install the necessary jars for appium.
- Will create a folder like Test data .Inside TestData will create class like BigBasket Elememnt class.
- Inside BigBasket element class i will write store the elements.
Example:
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Appium__Automation.TestData
{
public class BigBasketElements
{
//Login Page Elements
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/btn_login")]
public IWebElement LoginHomepage { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/email_input")]
public IWebElement EmailField { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/edit_text_passwd")]
public IWebElement PasswordField { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/btn_login")]
public IWebElement LoginButton { get; set; }
//Dasgboard Page Eleements
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/txtPinnedTitle")]
public IWebElement DashBoardValidation { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/action_search")]
public IWebElement searchIcon { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/searchView")]
public IWebElement searchTextBox { get; set; }
[FindsBy(How = How.XPath, Using = "//*[@resource-id = 'com.bigbasket.mobileapp:id/txtTerm' and @text= 'milk']")]
public IWebElement milkOption { get; set; }
//[FindsBy(How = How.XPath, Using = "//*[@class = 'android.widget.TextView' and @resource-id = 'com.bigbasket.mobileapp:id/btnAddToBasket'")]
//public IWebElement NandiniGoodLife { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/btnAddToBasket")]
public IWebElement NandiniGoodLife { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/viewIncBasketQty")]
public IWebElement NandiniGoodLifeADDquantity { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/txtInBasket")]
public IWebElement Noquantity { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/applyFilter")]
public IWebElement Filter { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/filter_category_expend_collapse_btn")]
public IWebElement CollapseButton { get; set; }
[FindsBy(How = How.XPath, Using = "//*[@text= 'Beverages']")]
public IWebElement BeveragesButton { get; set; }
[FindsBy(How = How.XPath, Using = "//*[@text= 'Branded Foods']")]
public IWebElement BrandedFoodsButton { get; set; }
[FindsBy(How = How.Id, Using = "com.bigbasket.mobileapp:id/btnApply")]
public IWebElement DoneButtonCatagories { get; set; }
[FindsBy(How = How.XPath, Using = "//*[@text= 'Sort by']")]
public IWebElement sortByTab { get; set; }
}
}
Now I will use those elements while writing scripts for functionalities
I will create one new class for writing scripts for BigBasket Functionalities where I am going to use Bigbasket elements .
using Appium__Automation.TestCases.NativeApps;
using Appium__Automation.TestData;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ImageFormat = System.Drawing.Imaging.ImageFormat;
namespace Appium__Automation.TestCases.AndroidApps.BigBasket
{
public class BigBasketFunctionalities : ITestCase
{
public static AppiumDriver<AndroidElement> driver;
KeyFunctions KeyGeneric = new KeyFunctions();
DesiredCapabilities capabilities = new DesiredCapabilities();
BigBasketElements BBElement = new BigBasketElements();
int i = 0;
public void Execute()
{
try
{
// BigBasket LogIn
KeyGeneric.CapabilitiesBigBasket(capabilities);
driver = new AndroidDriver<AndroidElement>(new Uri(ConfigurationManager.AppSettings["URL"]), capabilities);
Thread.Sleep(10000);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
PageFactory.InitElements(driver, BBElement);
BBElement.LoginHomepage.Click();
BBElement.EmailField.SendKeys("xxxxxx@gmail.com");
BBElement.PasswordField.SendKeys("xxxxxx");
driver.Navigate().Back();
BBElement.LoginButton.Click();
Thread.Sleep(20000);
// Home Page Validation
string Dashboardtext = BBElement.DashBoardValidation.Text;
Console.WriteLine(Dashboardtext);
Assert.AreEqual("Shop By Category", Dashboardtext);
// Click on search Icon
BBElement.searchIcon.Click();
// Enter Text on Search field
IWebElement milkele = BBElement.searchTextBox;
milkele.SendKeys("milk" + "\t");
// Click on milk field
BBElement.milkOption.Click();
Thread.Sleep(5000);
// Click on Add milk
/* BBElement.NandiniGoodLife.Click();
// Click more quantity
BBElement.NandiniGoodLifeADDquantity.Click();
string Itemcount = BBElement.Noquantity.Text;
Console.WriteLine(Itemcount);
Console.ReadLine();*/
BBElement.Filter.Click();
BBElement.CollapseButton.Click();
BBElement.BeveragesButton.Click();
BBElement.BrandedFoodsButton.Click();
BBElement.DoneButtonCatagories.Click();
((AndroidDriver<AndroidElement>)driver).PressKeyCode(AndroidKeyCode.Keycode_HOME);
}
catch
{
Screenshot image = ((ITakesScreenshot)driver).GetScreenshot();
//Save the screenshot
image.SaveAsFile("C:\\Users\\105798\\Documents\\Visual Studio 2013\\Projects\\Appium_ Automation\\Appium_ Automation\\ScreenShot\\Screenshot.Png", ImageFormat.Png);
}
}
}
}