How-To¶
Using DefaultPageLocator¶
The default page locator uses the following strategy to locate pages:
- uppercases the first letter of each word and joining them to a CamelCase like class name
- prepending configured namespaces defined via page_namespace_prefix
- 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php
use QATools\QATools\PageObject\Exception\PageFactoryException;
use QATools\QATools\PageObject\PageLocator\IPageLocator;
class MappingPageLocator implements IPageLocator
{
protected $pages = array(
'Login Page' => '\\shop\\pages\\LoginPage',
'Registration Page' => '\\shop\\pages\\LoginPage',
'Landing Page' => '\\shop\\pages\\LoginPage',
'Account Overview Page' => '\\shop\\account\\pages\\AccountPage',
);
public function resolvePage($name)
{
if ( !isset($this->pages[$name]) ) {
throw new PageFactoryException(
'Couldn\'t locate ' . $name,
PageFactoryException::TYPE_PAGE_CLASS_NOT_FOUND
);
}
return $this->pages[$name];
}
}
|
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');
...
?>