Skip to content
xahzzerp edited this page Apr 23, 2024 · 15 revisions

Each of the Page Object Pattern classes should:

  • inherit class ProjectPageBase public class InternetPage : ProjectPageBase,

  • contain proper constructor

public YourPageClass(DriverContext driverContext) : base(driverContext)
{
}
  • contain all necessary element locators which you need to implement and use in your tests.
private readonly ElementLocator pageHeader = new ElementLocator(Locator.XPath, "//h3[.='Login Page']"),
                                userNameForm = new ElementLocator(Locator.CssSelector, "Input[id=username]"),
                                passwordForm = new ElementLocator(Locator.CssSelector, "Input[id=password]"),
                                loginButton = new ElementLocator(Locator.XPath, "//form[@id='login'/button"),
                                message = new ElementLocator(Locator.XPath, "//a[@class='close']/..");
  • contain all methods which allow you to use implemented locators in tests
        public void EnterUserName(string userName)
        {
            Logger.Info(CultureInfo.CurrentCulture, "User name '{0}'", userName);
            this.Driver.GetElement(this.userNameForm).SendKeys(userName);
        }

Examples of Page object clases:

namespace Ocaramba.Tests.PageObjects.PageObjects.TheInternet
{
    using System;
    using System.Globalization;

    using NLog;

    using Ocaramba;
    using Ocaramba.Extensions;
    using Ocaramba.Types;
    using Ocaramba.Tests.PageObjects;
 public class InternetPage : ProjectPageBase
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Locators for elements
        /// </summary>
        private readonly ElementLocator
            linkLocator = new ElementLocator(Locator.CssSelector, "a[href='/{0}']");

        public InternetPage(DriverContext driverContext) : base(driverContext)
        {
        }

        /// <summary>
        /// Methods for this HomePage
        /// </summary>
        public InternetPage OpenHomePage()
        {
            var url = BaseConfiguration.GetUrlValue;
            this.Driver.NavigateTo(new Uri(url));
            Logger.Info(CultureInfo.CurrentCulture, "Opening page {0}", url);
            return this;
        }
        
        public FormAuthenticationPage GoToFormAuthenticationPage()
        {
            this.Driver.GetElement(this.linkLocator.Format("login")).Click();
            return new FormAuthenticationPage(this.DriverContext);
        }
   }
}
namespace Ocaramba.Tests.PageObjects.PageObjects.TheInternet
{
    using System;
    using System.Globalization;

    using NLog;

    using Ocaramba;
    using Ocaramba.Extensions;
    using Ocaramba.Types;
    using Ocaramba.Tests.PageObjects;

    public class FormAuthenticationPage : ProjectPageBase
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Locators for elements
        /// </summary>
        private readonly ElementLocator pageHeader = new ElementLocator(Locator.XPath, "//h3[.='Login Page']"),
                                        userNameForm = new ElementLocator(Locator.CssSelector, "Input[id=username]"),
                                        passwordForm = new ElementLocator(Locator.CssSelector, "Input[id=password]"),
                                        loginButton = new ElementLocator(Locator.XPath, "//form[@id='login']/button"),
                                        message = new ElementLocator(Locator.XPath, "//a[@class='close']/..");

        public FormAuthenticationPage(DriverContext driverContext)
            : base(driverContext)
        {
            Logger.Info("Waiting for page to open");
            this.Driver.IsElementPresent(this.pageHeader, BaseConfiguration.ShortTimeout);
        }

        public string GetMessage 
        {
            get
            {
                Logger.Info("Try to get message");
                var text = this.Driver.GetElement(this.message, BaseConfiguration.MediumTimeout, 0.1, e => e.Displayed & e.Enabled, "Tying to get welcome message every 0.1 s").Text;
                var index = text.IndexOf("!", StringComparison.Ordinal);
                text = text.Remove(index + 1);
                Logger.Info(CultureInfo.CurrentCulture, "Message '{0}'", text);
                return text;
            }
        }

        public void EnterPassword(string password)
        {
            Logger.Info(CultureInfo.CurrentCulture, "Password '{0}'", password);
            this.Driver.GetElement(this.passwordForm).SendKeys(password);
            this.Driver.WaitForAjax();
        }

        public void EnterUserName(string userName)
        {
            Logger.Info(CultureInfo.CurrentCulture, "User name '{0}'", userName);
            this.Driver.GetElement(this.userNameForm).SendKeys(userName);
        }

        public void LogOn()
        {
            Logger.Info(CultureInfo.CurrentCulture, "Click on Login Button");
            this.Driver.GetElement(this.loginButton).Click();;
        }
    }
}
Clone this wiki locally