PHPUnit + Behat/Mink + Page Object: The Rockstar Combination of Testing

Last month, we had discussion about implementing page object pattern in Behat/Mink framework at London Behat Users meetup . Page object pattern is a cool way to make tests maintainable, reusable and readable. Everyone was interested to know more about Page Object Pattern.

In this short tutorial, we will implement Mink and PHPUnit  combination for functional testing. Mink and PHPUnit combined with Pageness (Page Object) can be used for maintainable and readable tests. Mink is a web acceptance testing framework for PHP application. Mink has clean API’s which allows browsers emulation and browser controls. Mink has headless browser emulators as well as browser controls which covers all browsers interactions. PHPUnit is testing framework for PHP applications.


Lets start with some installations assuming PHP, Pear package is already installed.

Install PHPUnit

Now, upgrade pear package & install PHPUnit

Install Behat/Mink

Mink is ready to use just including in PHP classes.

[sourcecode language=”css”]require_once 'mink/autoload.php';[/sourcecode]

PHPUnit-Mink Framework on GitHub

PHPUnit-Mink framework designed to use combination of PHPUnit, Mink and Page Object Pattern to write functional tests with various browser emulators like Goutte, Selenium, Sahi and WebDriver. PHPUnit-Mink Framework has used Mink and PHPUnit to write tests. Driver support for Selenium, Sahi, WebDriver for browser emulation. Test Report Generation which can plugged in Continuous Integration server. Page Objects which can be used directly in tests. Abstracted common elements up in the framework in order to make it maintainable.

How to use:

  • Clone GitHub Repository

  • Start your Driver

Sahi Driver :

Download sahi zip file from SourceForge

Now launch Sahi Server using command below:

Selenium Driver:

You need to download selenium server jar file and execute following command:

In this tutorial, we are using sahi driver.

  • Now run tests using ANT

Directory structure

  1. conf    : YAML files can be used with Behat
  2. core    : Abstracted common elements/ methods
  3. Page   : Page objects (reusable Methods for page)
  4. report :  Generate Junit, Agile doc reports
  5. tests    :  PHPUnit tests using Mink Api’s

Don’t forget to include Mink and PHPUnit in Test like this:

Page Objects Pattern

There are some areas within application UI where your test interacts with. These areas can be identified by their functionality, they offer e.g login, registration, search etc. Page object pattern models as a object within you test code. While writing tests, we just need to create an object of the page and access methods and variables withing that page objects. This approach is useful when UI changes, we need to make changes in page objects not in the tests. Tests become readable and maintainable using page objects.

Page objects classes generally:

  1. Set of public methods that page offer which can be reused later.
  2. don’t make any assertion. Assertions can be added later into tests not in the page objects
  3. checks if user is on the same page that page object is created.
  4. don’t represent entire page but some important functions of that particular page.

GitHub & Other sources of Page object pattern

Read more about page object on  google wiki. There are couple of GitHub repositories that explains page object pattern, Saunter and pelenium

Writing Page Objects:

Common methods can be abstracted in the ‘page’ directory. You can specify driver of your choice. Now we have test with Sahi session. Example page looks like this

Using Page Objects in Tests

You can use pages just by creating new objects and accessing variables,methods in the test. Example of test looks like this

Writing Data-Driven Tests

You can write data driven tests by using PHPUnit and Mink combination. Data can be placed in CSV or in an array. Example data-driven test looks like this

Starting Engines

Before running tests, you need to start Mink drivers, here we are using Sahi. You can update tests to run Selenium driver. Launch driver from command line like this:

  • Sahi : Navigate to Sahi directory and run:

  • Selenium

Running Tests with PHPUnit

Now you can run wikisearch test with PHPUnit like this :

Running Tests with Ant

You can run tests using apache ANT. Navigate to project root directory (PHPUnit-Mink) and run an

Once all tests finished running, you will see build successful message. This build can be easily plugged into Continuous Integration server like Jenkins.


Functional testing become much easier with use of Mink with PHPUnit. Page Object pattern can be used with PHPUnit-Mink to make tests reusable and readable.

You can find source code on GitHub PHPUnit-Mink repository. Happy PHP Testing !