How-To

Using DefaultPageLocator

The default page locator uses the following strategy to locate pages:

  1. uppercases the first letter of each word and joining them to a CamelCase like class name

  2. prepending configured namespaces defined via page_namespace_prefix

  3. returning first found class

<?php
...
$page_locator = new DefaultPageLocator(array('\\shop\\pages', '\\shop\\account\\pages'));
$page_class = $page_locator->resolvePage('Login Page');
$page = new $page_class($page_factory);
...
?>

Depending on existence either \shop\pages\LoginPage or \shop\account\pages\LoginPage will be returned.

Defining a custom locator

In some cases it might be necessary to build a custom page locator. For example to map page names to specific classes.

 1<?php
 2use QATools\QATools\PageObject\Exception\PageFactoryException;
 3use QATools\QATools\PageObject\PageLocator\IPageLocator;
 4
 5class MappingPageLocator implements IPageLocator
 6{
 7    protected $pages = array(
 8        'Login Page' => '\\shop\\pages\\LoginPage',
 9        'Registration Page' => '\\shop\\pages\\LoginPage',
10        'Landing Page' => '\\shop\\pages\\LoginPage',
11        'Account Overview Page' => '\\shop\\account\\pages\\AccountPage',
12    );
13
14    public function resolvePage($name)
15    {
16        if ( !isset($this->pages[$name]) ) {
17            throw new PageFactoryException(
18                'Couldn\'t locate ' . $name,
19                PageFactoryException::TYPE_PAGE_CLASS_NOT_FOUND
20            );
21        }
22
23        return $this->pages[$name];
24    }
25}

Now it is possible to either locate the page manually by its name:

<?php
...
$page_locator = new MappingPageLocator();
$registration_page_class = $page_locator->resolvePage('Registration Page');
$registration_page = new $registration_page_class($page_factory);
...
?>

or replace default locator with new one during PageFactory construction time.

<?php
...
$container = new Container();

$container['page_locator'] = function () {
    return new MappingPageLocator();
};

$page_factory = new PageFactory($session, $container);

$registration_page = $page_factory->getPage('Registration Page');
...
?>