Commit cf61967d by Qiang Xue

Refactored codeception/BasePage.

parent 4d1390ce
...@@ -6,5 +6,5 @@ use yii\codeception\BasePage; ...@@ -6,5 +6,5 @@ use yii\codeception\BasePage;
class AboutPage extends BasePage class AboutPage extends BasePage
{ {
public static $URL = '?r=site/about'; public $route = 'site/about';
} }
...@@ -6,7 +6,7 @@ use yii\codeception\BasePage; ...@@ -6,7 +6,7 @@ use yii\codeception\BasePage;
class ContactPage extends BasePage class ContactPage extends BasePage
{ {
public static $URL = '?r=site/contact'; public $route = 'site/contact';
/** /**
* contact form name text field locator * contact form name text field locator
......
...@@ -6,7 +6,7 @@ use yii\codeception\BasePage; ...@@ -6,7 +6,7 @@ use yii\codeception\BasePage;
class LoginPage extends BasePage class LoginPage extends BasePage
{ {
public static $URL = '?r=site/login'; public $route = 'site/login';
/** /**
* login form username text field locator * login form username text field locator
......
...@@ -4,5 +4,5 @@ use tests\_pages\AboutPage; ...@@ -4,5 +4,5 @@ use tests\_pages\AboutPage;
$I = new WebGuy($scenario); $I = new WebGuy($scenario);
$I->wantTo('ensure that about works'); $I->wantTo('ensure that about works');
$I->amOnPage(AboutPage::$URL); AboutPage::openBy($I);
$I->see('About', 'h1'); $I->see('About', 'h1');
...@@ -4,9 +4,9 @@ use tests\_pages\ContactPage; ...@@ -4,9 +4,9 @@ use tests\_pages\ContactPage;
$I = new WebGuy($scenario); $I = new WebGuy($scenario);
$I->wantTo('ensure that contact works'); $I->wantTo('ensure that contact works');
$contactPage = ContactPage::of($I);
$I->amOnPage(ContactPage::$URL); $contactPage = ContactPage::openBy($I);
$I->see('Contact', 'h1'); $I->see('Contact', 'h1');
$I->amGoingTo('submit contact form with no data'); $I->amGoingTo('submit contact form with no data');
......
...@@ -4,9 +4,9 @@ use tests\_pages\LoginPage; ...@@ -4,9 +4,9 @@ use tests\_pages\LoginPage;
$I = new WebGuy($scenario); $I = new WebGuy($scenario);
$I->wantTo('ensure that login works'); $I->wantTo('ensure that login works');
$loginPage = LoginPage::of($I);
$I->amOnPage(LoginPage::$URL); $loginPage = LoginPage::openBy($I);
$I->see('Login', 'h1'); $I->see('Login', 'h1');
$I->amGoingTo('try to login with empty credentials'); $I->amGoingTo('try to login with empty credentials');
......
...@@ -4,5 +4,5 @@ use tests\_pages\AboutPage; ...@@ -4,5 +4,5 @@ use tests\_pages\AboutPage;
$I = new TestGuy($scenario); $I = new TestGuy($scenario);
$I->wantTo('ensure that about works'); $I->wantTo('ensure that about works');
$I->amOnPage(AboutPage::$URL); AboutPage::openBy($I);
$I->see('About', 'h1'); $I->see('About', 'h1');
...@@ -4,9 +4,9 @@ use tests\functional\_pages\ContactPage; ...@@ -4,9 +4,9 @@ use tests\functional\_pages\ContactPage;
$I = new TestGuy($scenario); $I = new TestGuy($scenario);
$I->wantTo('ensure that contact works'); $I->wantTo('ensure that contact works');
$contactPage = ContactPage::of($I);
$I->amOnPage(ContactPage::$URL); $contactPage = ContactPage::openBy($I);
$I->see('Contact', 'h1'); $I->see('Contact', 'h1');
$I->amGoingTo('submit contact form with no data'); $I->amGoingTo('submit contact form with no data');
......
...@@ -4,9 +4,9 @@ use tests\functional\_pages\LoginPage; ...@@ -4,9 +4,9 @@ use tests\functional\_pages\LoginPage;
$I = new TestGuy($scenario); $I = new TestGuy($scenario);
$I->wantTo('ensure that login works'); $I->wantTo('ensure that login works');
$loginPage = LoginPage::of($I);
$I->amOnPage(LoginPage::$URL); $loginPage = LoginPage::openBy($I);
$I->see('Login', 'h1'); $I->see('Login', 'h1');
$I->amGoingTo('try to login with empty credentials'); $I->amGoingTo('try to login with empty credentials');
......
...@@ -2,54 +2,72 @@ ...@@ -2,54 +2,72 @@
namespace yii\codeception; namespace yii\codeception;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
/** /**
* Represents a web page to test * BasePage is the base class for page classes that represent Web pages to be tested.
*
* Pages extend from this class and declare UI map for this page via
* static properties. CSS or XPath allowed.
*
* Here is an example:
* *
* ```php * @property string $url the URL to this page
* public static $usernameField = '#username';
* public static $formSubmitButton = "#mainForm input[type=submit]";
* ```
* *
* @author Mark Jebri <mark.github@yandex.ru> * @author Mark Jebri <mark.github@yandex.ru>
* @since 2.0 * @since 2.0
*/ */
abstract class BasePage abstract class BasePage extends Component
{ {
/** /**
* @var string include url of current page. This property has to be overwritten by subclasses * @var string|array the route (controller ID and action ID, e.g. `site/about`) to this page.
* Use array to represent a route with GET parameters. The first element of the array represents
* the route and the rest of the name-value pairs are treated as GET parameters, e.g. `array('site/page', 'name' => 'about')`.
*/ */
public static $URL = ''; public $route;
/** /**
* @var \Codeception\AbstractGuy * @var \Codeception\AbstractGuy the testing guy object
*/ */
protected $guy; protected $guy;
/**
* Constructor.
* @param \Codeception\AbstractGuy the testing guy object
*/
public function __construct($I) public function __construct($I)
{ {
$this->guy = $I; $this->guy = $I;
} }
/** /**
* Basic route example for your current URL * Returns the URL to this page.
* You can append any additional parameter to URL * The URL will be returned by calling the URL manager of the application
* and use it in tests like: EditPage::route('/123-post'); * with [[route]] and the provided parameters.
* @param array $params the GET parameters for creating the URL
* @return string the URL to this page
* @throws InvalidConfigException if [[route]] is not set or invalid
*/ */
public static function route($param) public function getUrl($params = [])
{ {
return static::$URL.$param; if (is_string($this->route)) {
return Yii::$app->getUrlManager()->createUrl($this->route, $params);
} elseif (is_array($this->route) && isset($this->route[0])) {
$route = $this->route[0];
$ps = $this->route;
unset($this->route[0]);
return Yii::$app->getUrlManager()->createUrl($route, array_merge($ps, $params));
} else {
throw new InvalidConfigException('The "route" property must be set.');
}
} }
/** /**
* @param $I * Creates a page instance and sets the test guy to use [[url]].
* @return static * @param \Codeception\AbstractGuy $I the test guy instance
* @param array $params the GET parameters to be used to generate [[url]]
* @return static the page instance
*/ */
public static function of($I) public static function openBy($I, $params = [])
{ {
return new static($I); $page = new static($I);
$I->amOnPage($page->getUrl($params));
return $page;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment