Step Objects and Page Objects


In this video we will look at how to make our Codeception tests easier to read, use, and maintain by way of two different but related organisational techniques - Page Objects and Step Objects.

A browser tool like Firebug or Chrome's Developer Tools are going to make your life a whole lot easier when using these techniques.

Page Objects

As a very quick summary, Page Objects are a way to group all the commonly used parts of a specific page in our site, such as the pages URL, the Username field, or the Password field, or the Repeated Password field, for example.

The advantage of doing this is to centralise all the things that might change inadvertently throughout our projects life cycle, but that we don't want to have to do messy find and replaces through multiple tests, or worse, multiple test files to update to the new paths.

To generate a page object, if you've been following along with this course, you're going to want to run a command similar to the following:

php vendor/codeception/codeception/codecept generate:pageobject LoginPage

Codeception will then spit out an 'empty' PageObject file into the /tests/_pages directory ready for your customisation.

The names you give your PageObject variables should be in context with whatever is on your page. In our example it's a login page, so $usernameField and $passwordField make sense, as these will be fields on the page that we are interested in.

The contents of these variables can be various things to identify what you're targeting, but personally I find CSS id's and class names to be easiest. XPath is a bit of a nightmare, and names change too frequently to be reliable in the long term.

This is where Chrome Developer Toolbar or Firebug will really help you out.

PageObjects are really pretty straightforward. They help you centralise and manage common parts of a page in your test suite. That's about it. But don't be fooled by their simplicity, they really are very useful, and once you start using them, not only will your tests become cleaner, but your maintenance will become easier, and that means more time spent doing more interesting things.

Step Objects

StepObjects are a little more involved.

In summary, they are a way of centralising and managing commonly repeating actions in your tests. Login is a great example of this, as you will likely need to perform the same Steps over and over in your tests to ensure you're user is logged in, and the last thing you want is to be repeating yourself. Even in tests, you should be *[DRY].

Again, if you've been following along and have things installed and setup the way I do, the command you will need is similar to the following:

php vendor/codeception/codeception/codecept generate:stepobject acceptance UserLogin

You'll be prompted for a Step name here, so be sure to think up something suitable. Or not. You can always change it. Nothing is ever set in stone.

As you'll see from the output, the StepObject files get dumped out into a _steps directory, but that will be a sub directory of whatever suite name you gave in the command. In our case, our StepObject will be output to /tests/acceptance/_steps/UserLoginSteps.php

In the video (around the 6:00 minute mark onwards), you will start to see just how powerful a combination of PageObjects and StepObjects can be to simplify the flow of your test environment.

*[DRY]: Don't Repeat Yourself

Code For This Course

Get the code for this course.

Code For This Video

Get the code for this video.

Episodes