Similar to Java bindings, Selenium WebDriver provides the PageFactory
class in .NET bindings to implement the Page Object model.
In this recipe, we will implement the Page Object model for the BMI Calculator page using the PageFactory
class in C#.
To implement Page Object model .NET, perform the following steps:
using System; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.PageObjects; namespace PageFactoryTests { public class BmiCalcPage { static string Url = " http://cookbook.seleniumacademy.com/bmicalculator.html"; private static string Title = "BMI Calculator"; [FindsBy(How = How.Id, Using = "heightCMS")] [CacheLookup] private IWebElement HeightField; [FindsBy(How = How.Id, Using = "weightKg")] private IWebElement WeightField; [FindsBy(How = How.Id, Using = "Calculate")] private IWebElement CalculateButton; [FindsBy(How = How.Name, Using = "bmi")] private IWebElement BmiField; [FindsBy(How = How.Name, Using = "bmi_category")] private IWebElement BmiCategoryField; private IWebDriver driver; public BmiCalcPage() { driver = new ChromeDriver(@"C:ChromeDriver"); PageFactory.InitElements(driver, this); } public void Load() { driver.Navigate().GoToUrl(Url); } public void Close() { driver.Close(); } public bool IsLoaded { get { return driver.Title.Equals(Title); } } public void CalculateBmi(String height, String weight) { HeightField.SendKeys(height); WeightField.SendKeys(weight); CalculateButton.Click(); } public String Bmi { get { return BmiField.GetAttribute("value"); } } public String BmiCategory { get { return BmiCategoryField.GetAttribute("value"); } } } }
BmiCalcPage
class, let's create a test for the calculation feature, as follows:using NUnit.Framework; namespace PageFactoryTests { public class BmiCalcTests { [TestCase] public void TestBmiCalculator() { BmiCalcPage bmiCalcPage = new BmiCalcPage(); bmiCalcPage.Load(); Assert.IsTrue(bmiCalcPage.IsLoaded); bmiCalcPage.CalculateBmi("181", "80"); Assert.AreEqual("24.4", bmiCalcPage.Bmi); Assert.AreEqual("Normal", bmiCalcPage.BmiCategory); bmiCalcPage.Close(); } } }
In this example, the BmiCalcPage
class provides various operations and properties from the BMI Calculator page to the test. The elements on the page are defined as instances of the IWebDriver
interface with a FindsBy
annotation, as follows:
[FindsBy(How = How.Id, Using = "heightCMS")] private IWebElement HeightField;
When the page is initialized by the PageFactory.InitElements()
method, these annotations are used to search the elements on the page.
The BmiCalcPage
class implements methods to open and close the BMI Calculator page. This will provide a high level of abstraction to the test.
The BmiCalcPage
class also implements the IsLoaded
property, which will tell a test if the BMI Calculator page is loaded into the browser. This class also defines properties for the Bmi
and BmiCategory
fields, which provide values from these fields to test rather than complete access to the underlying elements, as follows:
public bool IsLoaded { get { return driver.Title.Equals(Title); } } public String Bmi { get { return BmiField.GetAttribute("value"); } } public String BmiCategory { get { return BmiCategoryField.GetAttribute("value"); } }
It also provides a CalculateBmi()
method, which takes the height and weight as an argument and interacts with underlying elements to perform actions.
3.147.84.169