|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Codeception 2.1 Is Here" |
| 4 | +date: 2015-06-30 01:03:50 |
| 5 | +--- |
| 6 | + |
| 7 | +We are finally ready to show you the Codeception 2.1. Since RC we stabilized its codebase, and we encourage you to start all your new projects with Codeception 2.1. As well as migrate old ones. |
| 8 | + |
| 9 | +If you didn't track for the changes in master we will list all the new features here: |
| 10 | + |
| 11 | +* We added [Recorder](https://github.com/Codeception/Codeception/tree/master/ext#codeceptionextensionrecorder) extension, which is probably the most fancy feature you may try. Using it you can record test execution history by saving a screenshot of each step. This is handy for running tests on CI, debugging tests executed via *PhantomJS* or showing nice reports to your boss. |
| 12 | + |
| 13 | +  |
| 14 | + |
| 15 | +* **Updated to Guzzle 6**. Codeception can now work both with Guzzle v5 and Guzzle v6. PhpBrowser chooses right connector depending on Guzzle version installed. |
| 16 | +* **PSR-4**: all support classes moved to `tests/_support` by default. Actors, Helpers, PageObjects, StepObjects, GroupObjects to follow PSR-4 naming style. New `AcceptanceTester`, `FunctionalTester`, `UnitTester` classes are expected to be extended with custom methods. For instance, you can define some common behaviors there. For instance, it is a good idea to place `login` method into the actor class: |
| 17 | + |
| 18 | +{{% highlight php %}} |
| 19 | +<?php |
| 20 | +class AcceptanceTester extends \Codeception\Actor |
| 21 | +{ |
| 22 | + use _generated\AcceptanceTesterActions; |
| 23 | +
|
| 24 | + public function login() |
| 25 | + { |
| 26 | + $this->amOnPage('/login'); |
| 27 | + $this->fillField('name', 'jon'); |
| 28 | + $this->fillField('password', '123345'); |
| 29 | + $this->click('Login'); |
| 30 | + } |
| 31 | +} |
| 32 | +?> |
| 33 | +{{% endhighlight %}} |
| 34 | + |
| 35 | +* **Dependency Injection**: support classes can be injected into tests. Support classes can be injected into each other too. |
| 36 | + |
| 37 | +{{% highlight php %}} |
| 38 | +<?php |
| 39 | +class UserCest |
| 40 | +{ |
| 41 | + // inject page objects into Cests |
| 42 | + function updatePassword(\Page\User $page, AcceptanceTester $I) |
| 43 | + { |
| 44 | + $page->openProfile(); |
| 45 | + $page->editProfile(); |
| 46 | + $I->fillField($this->page->oldPasswordField, '123456'); |
| 47 | + $I->fillField($this->page->newPasswordField, '654321'); |
| 48 | + $I->fillField($this->page->passwordFieldRepeat, '654321'); |
| 49 | + $I->click($this->page->saveBtn); |
| 50 | + $I->see('Password has been updated'); |
| 51 | + } |
| 52 | + |
| 53 | + // inject step object into Cest |
| 54 | + function adminUpdatePassword(\Step\Admin $I) |
| 55 | + { |
| 56 | + $I->enterAdminArea(); |
| 57 | + $I->changePassword('654321'); |
| 58 | + $I->see('Password has been updated'); |
| 59 | + } |
| 60 | +} |
| 61 | +?> |
| 62 | +{{% endhighlight %}} |
| 63 | + |
| 64 | +* **Module config simplified**: Modules can be configured in `enabled` section of suite config. Take a look of this sample declaration of Api suite, there is no `config` section inside modules. |
| 65 | + |
| 66 | +``` |
| 67 | +modules: |
| 68 | + enabled: |
| 69 | + - WebDriver: |
| 70 | + url: http://codeception.com |
| 71 | + browser: firefox |
| 72 | + - \Helper\Acceptance |
| 73 | +``` |
| 74 | +* **Dependencies**: module can explicitly define dependencies and expect their injection. REST, SOAP and Doctrine2 modules rely on another module which should be explicitly set via `depends` config param. |
| 75 | + |
| 76 | +``` |
| 77 | +class_name: ApiTester |
| 78 | +modules: |
| 79 | + enabled: |
| 80 | + - REST: |
| 81 | + url: https://api.github.com/ |
| 82 | + depends: PhpBrowser |
| 83 | + - \Helper\Api |
| 84 | +``` |
| 85 | +As you can see, you don't need to specify `PhpBrowser` in `enabled` section, you can set it only via `depends`. |
| 86 | + |
| 87 | +* **Conflicts**: module can define conflicts with each other. Modules that share similar interfaces like `WebDriver`, `PhpBrowser`, and framework modules won't run together. You should avoid enabling more than one of those modules in suite config to avoid confusion. If you enable `Laravel5` and `WebDriver` and execute `$I->amOnPage('/')` you can't be sure how this command is exected - will it open a browser window using WebDriver protocol, or it will be handled by Laravel framework directly. |
| 88 | + |
| 89 | +* **Current** modules, environment, and test name can be received in scenario. |
| 90 | + |
| 91 | +{{% highlight php %}} |
| 92 | +<?php |
| 93 | +$scenario->current('name'); // returns current test name |
| 94 | +$scenario->current('modules'); // returns current modules |
| 95 | +$scenario->current('env'); // returns environment |
| 96 | + |
| 97 | +// can be used like this |
| 98 | +if ($scenario->current('env') == 'firefox') { |
| 99 | + // do something for firefox only |
| 100 | +} |
| 101 | +// naming screenshot |
| 102 | +$I->makeScreenshot($scenario->current('name').'_screenshot.png'); |
| 103 | +?> |
| 104 | +{{% endhighlight %}} |
| 105 | + |
| 106 | + |
| 107 | +* **Environment Matrix**: You can run tests combining several environments by separating their names by comma: |
| 108 | + |
| 109 | +``` |
| 110 | +codecept run --env dev,phantom --env dev,chrome --env dev,firefox |
| 111 | +``` |
| 112 | + |
| 113 | +Environments can now be stored in separate configuration files in `tests/_envs` dir created with `generate:environment` command: |
| 114 | + |
| 115 | +``` |
| 116 | +tests/_envs |
| 117 | +|── chrome.yml |
| 118 | +|── phantom.yml |
| 119 | +|── firefox.yml |
| 120 | +|── dev.yml |
| 121 | +``` |
| 122 | + |
| 123 | +* Cept files should avoid setting their metadata via `$scenario` methods. Instead of calling `$scenario->skip()`, `$scenario->group('firefox')`, etc, it is recommended to set scenario settings with annotations `// @skip`, `// @group firefox`. However, you can use `$scenario->skip` whenever you need to do it on some condition, like |
| 124 | + |
| 125 | +{{% highlight php %}} |
| 126 | +<?php |
| 127 | +if (substr(PHP_OS, 0, 3) == 'Win') $scenario->skip() |
| 128 | +?> |
| 129 | +{{% endhighlight %}} |
| 130 | + |
| 131 | +* Improved HTML reports |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +* **Modules can be partially loaded**. If you need only some actions to be included into tester objects. For instance, you want to have REST API tests with Laravel5 module. In this case you probably don't want methods from Laravel module like `amOnPage` or `see` to be included into the `ApiTester` as they interact with HTML pages, which we are not supposed to use. But you still need Laravel ORM methods like `seeRecord` to verify that changes were saved to database. In this case you can enable only ORM methods of Laravel5 module. |
| 136 | + |
| 137 | +``` |
| 138 | +modules: |
| 139 | + enabled: |
| 140 | + - Laravel5: |
| 141 | + part: ORM |
| 142 | + - REST: |
| 143 | + part: Json |
| 144 | + depends: Laravel5 |
| 145 | +``` |
| 146 | + |
| 147 | +As for REST module you can load methods only for API format you are using. You can choose either XML or Json, so only methods for Json or XML will be loaded. |
| 148 | + |
| 149 | +* Whenever you have steps grouped inside actor class, pageobject, or stepobject, you can the step executed with its substeps in console or HTML report. |
| 150 | + |
| 151 | + |
| 152 | +And lots of other notable improvements which you can see in [changelog](https://github.com/Codeception/Codeception/blob/master/CHANGELOG.md). |
| 153 | + |
| 154 | +Starting from Codeception 2.1 we **recommend using Cest** as a default format for all scenario-driven acceptance and functional, or api tests. Guides were rewritten to reflect new improved approaches to testing that you should practice by using Codeception. |
| 155 | + |
| 156 | +## Upgrading |
| 157 | + |
| 158 | +* It is pretty easy to upgrade from 2.0. The only thing you should start with is to rebuild your actor classes by running `codecept build` command. The old actor classes (`*Tester` or `*Guy`) in suite directories should be deleted manually. |
| 159 | +* REST, SOAP, Doctrine2 module will need to be configured to use a dependent module as it was shown above. You will get a detailed exception with configuration example once you execute tests with those modules. |
| 160 | +* Helpers, PageObjects, StepObjects are expected to follow PSR-4 standard and use namespaces. It is recommended to rename classes to replace suffixes with namespaces, like `UserPage` => `Page\User`. However, those changes are **not required**, so you can keep your support objects without changes. |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +If you are using 2.0 and you won't plan to upgrade in nearest future, you can still use releases from 2.0 branch. Minor fixes and improvements will still be accepted there. Also the site will contain documentation for 2.0 and 2.1 versions. |
| 165 | + |
| 166 | +Try Codeception 2.1 today by installing it via Composer: |
| 167 | + |
| 168 | +``` |
| 169 | +composer require "codeception/codeception:*" --dev |
| 170 | +``` |
| 171 | + |
| 172 | +or by downloading it as [Phar archive](http://codeception.com/codecept.phar) |
| 173 | + |
| 174 | +And provide a feedback! |
0 commit comments