From cf61967d7650950e0b892b02b7feab051bf1de6a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 17 Dec 2013 23:43:43 -0500 Subject: [PATCH] Refactored codeception/BasePage. --- apps/basic/tests/_pages/AboutPage.php | 4 +- apps/basic/tests/_pages/ContactPage.php | 2 +- apps/basic/tests/_pages/LoginPage.php | 2 +- apps/basic/tests/acceptance/AboutCept.php | 2 +- apps/basic/tests/acceptance/ContactCept.php | 4 +- apps/basic/tests/acceptance/LoginCept.php | 4 +- apps/basic/tests/functional/AboutCept.php | 2 +- apps/basic/tests/functional/ContactCept.php | 4 +- apps/basic/tests/functional/LoginCept.php | 4 +- extensions/yii/codeception/BasePage.php | 64 ++++++++++++++++++----------- 10 files changed, 55 insertions(+), 37 deletions(-) diff --git a/apps/basic/tests/_pages/AboutPage.php b/apps/basic/tests/_pages/AboutPage.php index ed5eb1b..5f9021f 100644 --- a/apps/basic/tests/_pages/AboutPage.php +++ b/apps/basic/tests/_pages/AboutPage.php @@ -6,5 +6,5 @@ use yii\codeception\BasePage; class AboutPage extends BasePage { - public static $URL = '?r=site/about'; -} \ No newline at end of file + public $route = 'site/about'; +} diff --git a/apps/basic/tests/_pages/ContactPage.php b/apps/basic/tests/_pages/ContactPage.php index 8149436..4fd6fa8 100644 --- a/apps/basic/tests/_pages/ContactPage.php +++ b/apps/basic/tests/_pages/ContactPage.php @@ -6,7 +6,7 @@ use yii\codeception\BasePage; class ContactPage extends BasePage { - public static $URL = '?r=site/contact'; + public $route = 'site/contact'; /** * contact form name text field locator diff --git a/apps/basic/tests/_pages/LoginPage.php b/apps/basic/tests/_pages/LoginPage.php index 74725d1..8493d51 100644 --- a/apps/basic/tests/_pages/LoginPage.php +++ b/apps/basic/tests/_pages/LoginPage.php @@ -6,7 +6,7 @@ use yii\codeception\BasePage; class LoginPage extends BasePage { - public static $URL = '?r=site/login'; + public $route = 'site/login'; /** * login form username text field locator diff --git a/apps/basic/tests/acceptance/AboutCept.php b/apps/basic/tests/acceptance/AboutCept.php index b6be2f3..deecee7 100644 --- a/apps/basic/tests/acceptance/AboutCept.php +++ b/apps/basic/tests/acceptance/AboutCept.php @@ -4,5 +4,5 @@ use tests\_pages\AboutPage; $I = new WebGuy($scenario); $I->wantTo('ensure that about works'); -$I->amOnPage(AboutPage::$URL); +AboutPage::openBy($I); $I->see('About', 'h1'); diff --git a/apps/basic/tests/acceptance/ContactCept.php b/apps/basic/tests/acceptance/ContactCept.php index 107d12d..25f5735 100644 --- a/apps/basic/tests/acceptance/ContactCept.php +++ b/apps/basic/tests/acceptance/ContactCept.php @@ -4,9 +4,9 @@ use tests\_pages\ContactPage; $I = new WebGuy($scenario); $I->wantTo('ensure that contact works'); -$contactPage = ContactPage::of($I); -$I->amOnPage(ContactPage::$URL); +$contactPage = ContactPage::openBy($I); + $I->see('Contact', 'h1'); $I->amGoingTo('submit contact form with no data'); diff --git a/apps/basic/tests/acceptance/LoginCept.php b/apps/basic/tests/acceptance/LoginCept.php index b6ce5f9..5d6a387 100644 --- a/apps/basic/tests/acceptance/LoginCept.php +++ b/apps/basic/tests/acceptance/LoginCept.php @@ -4,9 +4,9 @@ use tests\_pages\LoginPage; $I = new WebGuy($scenario); $I->wantTo('ensure that login works'); -$loginPage = LoginPage::of($I); -$I->amOnPage(LoginPage::$URL); +$loginPage = LoginPage::openBy($I); + $I->see('Login', 'h1'); $I->amGoingTo('try to login with empty credentials'); diff --git a/apps/basic/tests/functional/AboutCept.php b/apps/basic/tests/functional/AboutCept.php index 419b3fe..1875c2e 100644 --- a/apps/basic/tests/functional/AboutCept.php +++ b/apps/basic/tests/functional/AboutCept.php @@ -4,5 +4,5 @@ use tests\_pages\AboutPage; $I = new TestGuy($scenario); $I->wantTo('ensure that about works'); -$I->amOnPage(AboutPage::$URL); +AboutPage::openBy($I); $I->see('About', 'h1'); diff --git a/apps/basic/tests/functional/ContactCept.php b/apps/basic/tests/functional/ContactCept.php index 9c7cab3..ddc7ca6 100644 --- a/apps/basic/tests/functional/ContactCept.php +++ b/apps/basic/tests/functional/ContactCept.php @@ -4,9 +4,9 @@ use tests\functional\_pages\ContactPage; $I = new TestGuy($scenario); $I->wantTo('ensure that contact works'); -$contactPage = ContactPage::of($I); -$I->amOnPage(ContactPage::$URL); +$contactPage = ContactPage::openBy($I); + $I->see('Contact', 'h1'); $I->amGoingTo('submit contact form with no data'); diff --git a/apps/basic/tests/functional/LoginCept.php b/apps/basic/tests/functional/LoginCept.php index 7a76e18..770b823 100644 --- a/apps/basic/tests/functional/LoginCept.php +++ b/apps/basic/tests/functional/LoginCept.php @@ -4,9 +4,9 @@ use tests\functional\_pages\LoginPage; $I = new TestGuy($scenario); $I->wantTo('ensure that login works'); -$loginPage = LoginPage::of($I); -$I->amOnPage(LoginPage::$URL); +$loginPage = LoginPage::openBy($I); + $I->see('Login', 'h1'); $I->amGoingTo('try to login with empty credentials'); diff --git a/extensions/yii/codeception/BasePage.php b/extensions/yii/codeception/BasePage.php index d2e3b77..3a9f8f8 100644 --- a/extensions/yii/codeception/BasePage.php +++ b/extensions/yii/codeception/BasePage.php @@ -2,54 +2,72 @@ namespace yii\codeception; +use Yii; +use yii\base\Component; +use yii\base\InvalidConfigException; + /** - * Represents a web page to test - * - * Pages extend from this class and declare UI map for this page via - * static properties. CSS or XPath allowed. - * - * Here is an example: + * BasePage is the base class for page classes that represent Web pages to be tested. * - * ```php - * public static $usernameField = '#username'; - * public static $formSubmitButton = "#mainForm input[type=submit]"; - * ``` + * @property string $url the URL to this page * * @author Mark Jebri * @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; + /** + * Constructor. + * @param \Codeception\AbstractGuy the testing guy object + */ public function __construct($I) { $this->guy = $I; } /** - * Basic route example for your current URL - * You can append any additional parameter to URL - * and use it in tests like: EditPage::route('/123-post'); + * Returns the URL to this page. + * The URL will be returned by calling the URL manager of the application + * 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 - * @return static + * Creates a page instance and sets the test guy to use [[url]]. + * @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; } }