-
Notifications
You must be signed in to change notification settings - Fork 130
Page Object class
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);
}
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();
}
}
}
- Home
- Getting started
- Parallel tests execution
- MsTest DataDriven tests from Xml and CSV files
- NUnit DataDriven tests from Xml, CSV and Excel files
- Comparing files by NUnit DataDriven tests
- Visual Testing
- Screen shots: full desktop, selenium. PageSource saving
- Verify-asserts without stop tests
- Downloading files
- Helpers
- Override browser profile preferences, install browser extensions, Headless mode
- Debugging Test.Automation framework
- Logging
- Performance measures
- Webdriver Extends
- More common locators
- Selenium-Grid-support
- Advanced Browser Capabilities and Options
- AngularJS synchronization
- Update App.config or appsettings.json
- Cross browser parallel test execution with testing-Cloud-Providers\SeleniumGrid
- Verifying Javascript Errors from browser
- Enabling Performance Log for Chrome
- Azure DevOps Support
- Edge browser Support
- Downloading and running Selenium Grid with Powershell
- Run Ocaramba tests with Docker container
- HTTP auth in Internet explorer
- ExtentReports Support