Skip to content

Commit d686a57

Browse files
committed
updated
1 parent dc15430 commit d686a57

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
layout: post
3+
title: "Introducing even more features of Codeception 2.2"
4+
date: 2016-03-11 01:03:50
5+
---
6+
7+
Today we continue to show you new features of Codeception 2.2, planned to release this March.
8+
In previous post we've got you acquainted with [Test Dependencies, Params, and Conflicts](http://codeception.com/03-05-2016/codeception-2.2.-upcoming-features.html). This post will continue this overview, and we will start with some nice modules, which might be useful to you.
9+
10+
As you know, in Codeception we try to help developers and testers share their experience by providing set of shared pieces, and not to reinvent the wheel. Modules play exactly that role: you just include modules to fit your project, and write your tests, concentrating on its business logic, and not on implementation. 90% test steps in functional and acceptance tests are covered with out modules. So what do we have prepared for 2.2?
11+
12+
### DataFactory
13+
14+
This module should solve problem of generating and populating data for tests. Right now we have [Db module](http://codeception.com/docs/modules/Db), [MongoDb](http://codeception.com/docs/modules/MongoDb) with really limited functionality, methods like `haveRecord`, `seeRecord` in various frameworks, and [Sequence module](http://codeception.com/docs/modules/Sequence) for generating unique keys for data. However, they didn't provide any way to generate data needed exactly for a particular test.
15+
16+
DataFactory module should solve this with fixture generators, called factories. The original idea take from popular Ruby gem **FactoryGirl** and implemented in php in [FactoryMuffin](https://github.com/thephpleague/factory-muffin) library, which we use. Laravel 5 users are already aware of using factories. Now you can use them with all PHP frameworks with ActiveRecord pattern and in **Doctrine**.
17+
18+
This is how it works. You define rules to generate models:
19+
20+
```php
21+
use League\FactoryMuffin\Faker\Facade as Faker;
22+
23+
$fm->define(User::class)->setDefinitions([
24+
'name' => Faker::name(),
25+
// generate email
26+
'email' => Faker::email(),
27+
'body' => Faker::text(),
28+
29+
// generate a profile and return its Id
30+
'profile_id' => 'factory|Profile'
31+
]);
32+
```
33+
34+
and DataFactory creates them using underlying ORM, inserts them to database once you call
35+
36+
```php
37+
<?php
38+
$I->have(User::class);
39+
```
40+
and deletes them after the test. As we said that will work for Doctrine as well, if you are familiar with Nelmio's Alice, you might find the same idea but with easier syntax to use.
41+
42+
### AngularJS
43+
44+
AngularJS is probably the most popular framework for building single page web applications. It provides its own tool for acceptance testing - [Protractor](https://angular.github.io/protractor/#/) but what if you already use Codeception and you are not so passionate to switch to NodeJS? Well if you actually do, please check out our side-project [CodeceptJS](http://codecept.io) which brings Codeception concepts to JavaScript world. However, in case of Angular testing there is less reasons to switch, as we brought you Protractor experience in new AngularJS module.
45+
46+
It simply wraps WebDriver module, and adds an asynchronous script between steps to ensure that all client-side operations finished before proceed. This way no new actions are taken before Angular finishes rendering. Also you've got new strict locator to use:
47+
48+
```php
49+
<?php
50+
$I->see('davert', ['model' => 'user']);
51+
```
52+
53+
to match elements by their ng-model.
54+
55+
But enough with modules, we have something more than that!
56+
57+
### Examples
58+
59+
In PHPUnit you could have one test to be executed several times with different data, using the data provider mechanism. You can do the same in Codeception inside unit tests. But what about functional and acceptance testing? What if you need to run same scenario but passing different values into it? To be honest, the data provider didn't looked like an elegant way to define a test data. Data was stored in additional method, often few pages below the original test, so it was hard to see the picture in a whole. We introduce a concept of `Example`, similar to what you might have seen in BDD frameworks. Using the `@example` annotation you can define data in test annotation and receive from `Codeception\Example` instance, injected into test:
60+
61+
```php
62+
<?php
63+
/**
64+
* @example(path=".", file="scenario.suite.yml")
65+
* @example(path=".", file="dummy.suite.yml")
66+
* @example(path=".", file="unit.suite.yml")
67+
*/
68+
public function filesExistsAnnotation(FileTester $I, \Codeception\Example $example)
69+
{
70+
$I->amInPath($example['path']);
71+
$I->seeFileFound($example['file']);
72+
}
73+
?>
74+
```
75+
76+
For REST API testing this might look like:
77+
78+
```php
79+
/**
80+
* @example ['/api/', 200]
81+
* @example ['/api/protected', 401]
82+
* @example ['/api/not-found-url', 404]
83+
* @example ['/api/faulty', 500]
84+
*/
85+
public function checkEndpoints(ApiTester $I, \Codeception\Example $example)
86+
{
87+
$I->sendGET($example[0]);
88+
$I->seeResponseCodeIs($example[1]);
89+
}
90+
```
91+
92+
Data in `@example` annotation can be defined using JSON objects, JSON-arrays, or Symfony-style annotation.
93+
And yes, examples work only in Cests.
94+
95+
### Custom Commands
96+
97+
Long requested feature that finally was implemented by [Tobias Matthaiou](https://github.com/sd-tm) allows you ro register custom commands to Codeception runner.
98+
99+
If you ever created Symfony Console commands you will be familiar in creating custom commands for Codeception. You migth probably use to have your own template generators, perform data migrations, etc. You can register one as simple as you do it for extension:
100+
101+
```yaml
102+
extensions:
103+
commands: [MyApp\MyCustomCommand]
104+
```
105+
106+
### Getting current browser and capabilities in tests
107+
108+
The last one, simple yet useful thing that might improve your acceptance testing experience. If you want to have different behavior of tests for different browsers, you can get current browser name from a `$scenario->current` value:
109+
110+
```php
111+
<?php
112+
if ($scenario->current('browser') == 'phantomjs') {
113+
// emulate popups for PhantomJS
114+
$I->executeScript('window.alert = function(){return true;}');
115+
}
116+
?>
117+
```
118+
119+
in a similar manner you have access to all browser capabilities:
120+
121+
```php
122+
<?php
123+
$capabilities = $scenario->current('capabilities');
124+
if (isset($capabilities['platform'])) {
125+
if ($capabilities['platform'] == 'Windows') {
126+
// windows specific hooks
127+
}
128+
}
129+
?>
130+
```
131+
132+
---
133+
134+
That's all for today, but not for Codeception 2.2
135+
The most important and most impressive feature is waiting for you in next post. Subscribe to our [Twitter](http://twitter.com/codeception) to not miss it. Stay tuned!

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy