2

I am using Selenium webdriver using the Page Object Model.

My question is, where would it be best practice to define the navigation step selector? Should it be in the page object class or in each of the test classes?

2

As always, multiple solutions are possible.

Example 1:

The most common and perhaps logical example you'll find is the one where navigation is seen as a Browser action. It's not a PageObject method because navigating to a certain URL has nothing to do with the page's content behind it.

public void TestMethod()
{
     Browser.GoTo("www.google.com");
     Pages.GoogleHome.Search("Selenium test");
     Pages.GoogleHome.ValidateResultCount();
}

Example 2:

Of course, there are contrasting opinions on where validations should occur:

There are differences of opinion on whether page objects should include assertions themselves, or just provide data for test scripts to do the assertions. Advocates of including assertions in page objects say that this helps avoid duplication of assertions in test scripts, makes it easier to provide better error messages, and supports a more TellDontAsk style API. Advocates of assertion-free page objects say that including assertions mixes the responsibilities of providing access to page data with assertion logic, and leads to a bloated page object.

You could propose a similar alternative for navigation, arguing that each page knows its own URL and thus your test methods might look like:

public void TestMethod()
{
     Pages.GoogleHome.GoTo();
     Pages.GoogleHome.Search("Selenium test");
     Pages.GoogleHome.ValidateResultCount();
}

The GoTo method then in turn directs the browser to navigate, and possibly also checks if the page has been succesfully loaded.

  • @Robben No problem. If your question was solved by this, please close it by accepting my answer. – FDM Mar 29 '16 at 5:17
1

I've struggled with this too.

I do put them in the PageObject object

We have very dynamic flows and I decided that creating lots of separate css navigation objects for each test was too much work to maintain given that they were often (but not always..) common across many web flows. Sometimes i had to append _extension_called_something for cases where one flow is different but I still choose to put them in the same PageObject used for reference so that future developer don't have to remember shared vs. local

  • 1
    The other decision I had to make was whether, if they were separated, would there be separate files for the navigation elements for each flow. This seemed like a maintenance headache also so I've kept with the all in one file for now strategy. I think the separate flows files could work though. There might be some repetition but having the files separated that way might make it lot easier to change the thing you care about without worrying about breaking other stuff. – Michael Durrant Mar 28 '16 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.