Skip to content

Commit 36b593e

Browse files
committed
updated
1 parent 4702ff1 commit 36b593e

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
layout: post
3+
title: "Drive your Browser Tests with Codeception"
4+
date: 2017-07-09 01:03:50
5+
---
6+
7+
In last few weeks Codeception received updates aimed to empower acceptance testing.
8+
We try to make PHP a better place for browser tests. As you know, QA engineers often prefer other languages like Java or Python for test automation. But PHP is not that bad by itself, it is simpler, it is faster in most cases, it has great IDEs. And for sure, we have Codeception. With it you can write the most complex acceptance tests in a simple scenario-driven manner.
9+
10+
So what was add in the last release?
11+
12+
## RunProcess
13+
14+
[RunProcess extension](http://codeception.com/extensions#RunProcess) was introduced.
15+
Use it to easily start/stop Selenium Server, ChromeDriver, or other required services for a test suite.
16+
17+
Enable it in suite config or in environment:
18+
19+
```yaml
20+
# acceptance.suite.yml
21+
extensions:
22+
enabled:
23+
- Codeception\Extension\RunProcess:
24+
- php -S 127.0.0.1:8000
25+
- java -jar selenium-server.jar
26+
27+
# or inside environment config
28+
# in this case, run tests as
29+
#
30+
# codecept run --env selenium
31+
env:
32+
selenium:
33+
extensions:
34+
enabled:
35+
- Codeception\Extension\RunProcess:
36+
- php -S 127.0.0.1:8000
37+
- java -jar selenium-server.jar
38+
```
39+
40+
Depending on the environment (dev host, CI server) you can easily switch setups if you use environment configs.
41+
42+
## SmartWaits
43+
44+
This is the new unique feature of Codeception which incorporates [implicit waits](http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits). By itself, the implicit wait was available in Codeception with `wait: ` option in WebDriver config. However, it was not usable, because by design it slowed down test execution. In this release, we introduce the **SmartWait concept**. Implicit waits are used only when they are really needed and disabled for all other cases. This makes tests extremely stable and fast.
45+
46+
Thus, a test won't fail if expected element didn't yet appear on a page but waits for it a few seconds more. Just set `wait: 5` to WebDriver config to try it and [read the documentation](http://codeception.com/docs/03-AcceptanceTests#SmartWait).
47+
48+
## Customization
49+
50+
Codeception took to much of browser control. Let give it back to you.
51+
With `start: false` option you can disable autostart browser before a test, and create a browser session manually. Codeception doesn't provide actions to start browser session inside a test (because it is supposed you already have one in a test), but you can write a custom method in a helper:
52+
53+
```php
54+
<?php
55+
// support/Helper/Acceptance.php:
56+
//
57+
public function startBrowser()
58+
{
59+
$this->getModule('WebDriver')->_initializeSession();
60+
}
61+
62+
public function changeBrowser($browserName)
63+
{
64+
$this->getModule('WebDriver')->_restart(['browser' => $browser]);
65+
}
66+
67+
public function closeBrowser()
68+
{
69+
$this->getModule('WebDriver')->_closeSession();
70+
}
71+
```
72+
73+
Then these methods can be used inside a test.
74+
75+
```php
76+
<?php
77+
// I prepare something for a test
78+
$I->startBrowser();
79+
// I do something inside a browser
80+
$I->closeBrowser();
81+
```
82+
83+
If you use [BrowserStack](https://www.browserstack.com/) you can use this features to set a test name dynamically to [capabilities](https://www.browserstack.com/automate/capabilities). Here is how can you do it in Acceptance Helper:
84+
85+
```php
86+
<?php
87+
// inside Helper\Acceptance
88+
//
89+
public function _before(TestInterface $test)
90+
{
91+
$webDriver = $this->getModule('WebDriver');
92+
$webDriver->_reconfigure(['name' => $test->getMetadata->getName()]);
93+
$webDriver->_initializeSession();
94+
}
95+
```
96+
Please don't forget to set `start: false` in config option, so browser wouldn't be started twice!
97+
98+
## @prepare
99+
100+
What if you need to change the configuration for a specific test?
101+
Let's say you want to run all tests in Firefox but this specific one in Chrome? Or something like that.
102+
103+
We added new annotation `@prepare` which can be used in Cest and Test formats. It will execute a method which can change the configuration before the module is called.
104+
105+
```php
106+
<?php
107+
/**
108+
* @prepare switchToFirefox
109+
*/
110+
public function testSomethingInFirefox(AcceptanceTester $I)
111+
{
112+
$I->amOnPage('/');
113+
$I->click('Detect Browser');
114+
$I->see('Firefox');
115+
}
116+
117+
protected function switchToFirefox(\Codeception\Module\WebDriver $webDriver)
118+
{
119+
$webDriver->_reconfigure(['browser' => 'firefox']);
120+
}
121+
```
122+
123+
This `@prepare` can be used not only for browser tests but everywhere. Use it wisely!
124+
125+
---
126+
127+
Is that all? Almost.
128+
129+
We also updated WebDriver docs to include more options for [Local Testing](http://codeception.com/docs/modules/WebDriver#Local-Testing), like ChromeDriver. We also published a [reference on running Chrome in Headless mode](http://phptest.club/t/how-to-run-headless-chrome-in-codeception/1544).
130+
131+
Update to the latest `2.3.4` version to try all the new features.
132+
133+
We worked hard to bring all this stuff to you. Now is your turn: please help to spread the word and encourage more of your colleagues to use PHP and Codeception for web testing. Setting up Codeception for web tests nowadays is as simple as running:
134+
135+
```
136+
codecept init acceptance
137+
```
138+
139+
Happy testing!

0 commit comments

Comments
 (0)