Skip to content
PHPUnit Selenium PageObjects
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



PageObjects is the idea of representing a webpage's services through a model, abstracting away the guts of Selenium and the page's structure.

You can (and should) read all about PageObjects here:

Additionally, this repository comes prepared with an example application and functional test:


You should apply good programming practices to testing. Tests are code. Bad code can contribute considerably to your technical debt. Tests should be DRY.


  1. pear channel-discover
  2. pear install nf/PHPUnit_SeleniumPageObject


Getters and Setters

You should define a getter and setter for each field for usage outside of your functional test. Never manually access the map, locators, or map keys outside of the PageObject.


To remove hard-coded Selenium locators, a protected $map variable is defined on every PageObject. Define your map of elements that you reference here:

protected $map = array(
    'first_name' = 'css=#account_fname',
    'last_name' = 'css=$account_lname',

When you instantiate a PageObject, it calls assertMapConditions which automatically loops through all $map elements and asserts their presence.

Note: Another method exists, assertPreConditions, and is executed before assertMapConditions. Implementing this method allows you to execute methods like waitForElementPresent.

Using The Map

To use a mapped element, simply append ByMap to the end of the method name you want to utilize. For example:

$this->typeByMap('first_name', 'Graham');
$this->typeByMap('last_name', 'Christensen');

Advanced: Model-Based PageObjects

Often, a form on a page directly represents a model class with standard getter and setters. A class has been provided to map a model to a PageObject's $map variable.

Writing aggregate getters and setters for each object would be a tedious:


Setting Fields


Asserting the page's content matches the model

To ease the process and allow development to be much faster, the class provides two methods:

  • setFromModel($model);
  • assertEqualsModel($model, $message = '');

Both of these methods iterate over your $map, converting the keys into camel-case: first_name becomes FirstName, and calls the appropriate getters and setters.


This method takes the camel-case version of the map, and uses the Pageobject's setter methods to set the page's values to the values from the model.

For example, if you have the fields first_name and last_name in your map, setFromModel would effectively be:



This method is very similar to setFromModel, however asserts the content on the page against a model.

For example, if you have the fields first_name and last_name in your map, assertFromModel would effectively be:

    'first_name on the page should match the model.');
    'last_name on the page should match the model.');

Getter and Setter Exceptions

Some of your $map locators aren't valid places to enter data, and even worse to assert its value. One prime example of this would be the locator of the submit button.

To indicate to the assert/setFromModel methods that these need to skip, place its $map key into the $modelSkip array.

For example:

protected $map = array(
    'first_name' => 'css=#first_name',
    'last_name' => 'css=#last_name',
    'save' => 'css=#form_submit'

protected $modelSkip = array (
Something went wrong with that request. Please try again.