Using Selenium's PageObject pattern and an Open() method



  • Here is an example of some C# WebDriver code I'm using to model a registration page: public class RegistrationPage : BasePage { private IWebDriver driver; private String URL = "http://mydomain.com/registration.aspx"; // private String pageTitle = "Registration"; public RegistrationPage(IWebDriver driver) : base(driver) { this.driver = driver; /* // if we are not on the Registration page if (!(driver.Title.ToString().Equals(pageTitle))) { throw new Exception(); // Need a more relevant exception }*/ } override public String URLproperty { get { return URL; } set { URL = value; } } // CODE OMITTED ... } Here is a method from my BasePage, which RegistrationPage uses: // Open the URL specified in the implementation of the class public virtual void Open() { driver.Navigate().GoToUrl(this.URLproperty); } And finally here is an example of my test code: [Test] public void SubmitRegistrationWithBlankUserDetails() { RegistrationPage regPage = new RegistrationPage(driver); regPage.Open(); regPage.ClickRegisterButton(); Assert.IsTrue(regPage.SearchForText("You must enter a first name")); } The issue I'm having is implementing the title "sanity check" in the constructor (currently commented out) for RegistrationPage. I've seen this used on the WebDriver wiki (here, code in Java I think: http://code.google.com/p/selenium/wiki/PageObjects), but I'm unsure how to go about it. I see two possible scenarios when running my tests: My test's [SetUp] points the driver to mydomain.com. A link clicked by WebDriver (for example a link to my Registration page, above) passes the driver to the constructor of whatever page I am visiting. Job done, title sanity check can work. Let's say instead of clicking links to get to a certain page I want to start my test at the Registration page. I don't have a neat way of visiting that page without navgiating from the domain specified in [Setup]. My [SetUp] doesn't help (unless I navigate manually to the page) and creating a new PageObject of the Registration page doesn't take me anywhere unless I call the Open() method. Should I simply create a new test plan for the second scenario, one where [Setup] points me to mydomain.com/registration.aspx? Am I misunderstanding something fundamental here?



  • I don't think that you should be defining the URL to the page inside the pageObject. The methods in your page object should actions/ journeys that your pageObjects can take. For example a login pageObject may have 3 methods in it: Login(String username, String password) { // fill in login form // Submit login form return new landingPage(driver); } LoginUnsuccessfully(String username, String password) { // fill in login form // submit login form return new loginPage(driver, lookForLoginFailMessage); } ForgotPassword() { // click forgot password return new ForgotPasswordPage(); } At the start of your page your test should use the driver.get(); (or C# eqivalent) method to navigate to the correct URL, then use the methods in your pageObjects to navigate around your application. TestLogin() { driver.get(app.com/login); Login page login = new LoginPage(driver); LandingPage landingPage = login.login("joebloggs", "password1"); } The 'basePage' class that you have should contain 'journeys' that are common to many pages like a navigation menu. A different 'journey' through the app should be written in another test. But will be able to use the same page objects. Page Object organisation PageObjects one for each page in the website / application Contain all the logic about interacting the page how to fill in a form etc All the public methods should be a function that the page can perform Tests Should only call methods on the page object Each test is a journey through a part of the app This structure allows for logic of the what you are testing to be separate from the WebDriver interactions with the page



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2