You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is possible to set individual browser configuration for each test in a test case by creating a `\aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration` class instance in `setUp` method of the test case.
57
-
58
-
```php
59
-
<?php
60
-
61
-
use aik099\PHPUnit\BrowserTestCase;
62
-
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
63
-
64
-
class PerBrowserConfigTest extends BrowserTestCase
65
-
{
66
-
67
-
protected function setUp()
68
-
{
69
-
// to create regular browser configuration via BrowserConfigurationFactory
It is possible to define a single browser configuration to be used for each test in test case. This can be done by defining static `$browsers` class variable as an array, where each item represents a single browser configuration. In that case each of the tests in a test case would be executed using each of defined browser configurations.
99
-
100
-
```php
101
-
<?php
102
-
103
-
use aik099\PHPUnit\BrowserTestCase;
104
-
105
-
class CommonBrowserConfigTest extends BrowserTestCase
106
-
{
107
-
108
-
public static $browsers = array(
109
-
array(
110
-
'host' => 'localhost',
111
-
'port' => 4444,
112
-
'browserName' => 'firefox',
113
-
'baseUrl' => 'http://www.google.com',
114
-
),
115
-
array(
116
-
'host' => 'localhost',
117
-
'port' => 4444,
118
-
'browserName' => 'chrome',
119
-
'baseUrl' => 'http://www.google.com',
120
-
),
121
-
);
122
-
123
-
public function testUsingBrowsersArray()
124
-
{
125
-
echo sprintf("I'm executed using '%s' browser", $this->getBrowser()->getBrowserName());
126
-
}
127
-
128
-
}
129
-
```
130
-
131
-
## Sharing Session Between Tests
132
-
As a benefit of shared browser configuration, that was described above is an ability to not only share browser configuration, that is used to create [Mink](Behat/Mink) session, but to actually share created sessions between all tests in a test case. This can be done by adding `sessionStrategy` option to browser configuration.
133
-
134
-
```php
135
-
<?php
136
-
137
-
use aik099\PHPUnit\BrowserTestCase;
138
-
139
-
class CommonBrowserConfigTest extends BrowserTestCase
140
-
{
141
-
142
-
public static $browsers = array(
143
-
array(
144
-
'host' => 'localhost',
145
-
'port' => 4444,
146
-
'browserName' => 'firefox',
147
-
'baseUrl' => 'http://www.google.com',
148
-
'sessionStrategy' => 'shared',
149
-
),
150
-
);
151
-
152
-
}
153
-
```
154
-
155
-
## Using Browser Aliases
156
-
All previous examples demonstrate various ways how browser configuration can be defined, but they all have same downside - server connection details stay hard-coded in test case classes. This could become very problematic if:
157
-
158
-
* same test cases needs to be executed on different servers (e.g. each developer runs them on his own machine)
159
-
* due change of server connection details each test case class needs to be changed
160
-
161
-
To solve this problem a browser aliases were introduced. Basically a browser alias is predefined browser configuration, that is available in the test case by it's alias. How it can be used:
162
-
163
-
1. create base test case class, by extending BrowserTestCase class in the project with `getBrowserAliases` method in it. That method will return an associative array of a browser configurations (array key acts as alias name)
164
-
2. in any place, where browser configuration is defined use `'alias' => 'alias_name_here'` instead of actual browser configuration
165
-
3. feel free to override any part of configuration defined in alias
166
-
4. nested aliases are also supported
167
-
168
-
```php
169
-
<?php
170
-
171
-
use aik099\PHPUnit\BrowserTestCase;
172
-
173
-
abstract class BrowserAliasTest extends BrowserTestCase
174
-
{
175
-
176
-
public function getBrowserAliases()
177
-
{
178
-
return array(
179
-
'example_alias' => array(
180
-
'host' => 'localhost',
181
-
'port' => 4444,
182
-
'browserName' => 'firefox',
183
-
'baseUrl' => 'http://www.google.com',
184
-
),
185
-
);
186
-
}
187
-
188
-
}
189
-
190
-
191
-
class ConcreteTest extends BrowserAliasTest
192
-
{
193
-
194
-
public static $browsers = array(
195
-
array(
196
-
'alias' => 'example_alias',
197
-
198
-
),
199
-
array(
200
-
'alias' => 'example_alias',
201
-
'browserName' => 'chrome',
202
-
),
203
-
);
204
-
}
205
-
206
-
```
207
-
208
-
## Using "Sauce Labs"
209
-
When using "Sauce Labs" account to perform Selenium server-based testing you need to specify `'type' => 'saucelabs', 'api_username' => '...', 'api_key' => '...'` instead of `host` or `port` settings. In all other aspects all will work the same as if all tests are running locally.
210
-
211
-
## Remote Code Coverage
212
-
Browser tests are executed on different machine, then one, where code coverage information is collected (and tests are executed). To solve that problem this library uses remote coverage collection. Following steps needs to be performed before using this feature:
213
-
214
-
### On Remote Server
215
-
Remote server is web-server, where website used in tests is located.
216
-
217
-
1. Install [Xdebug](http://xdebug.org/) PHP extension on web-server
218
-
2. Copy `library/aik099/PHPUnit/RemoteCoverage/RemoteCoverageTool.php` into web-server's DocumentRoot directory.
219
-
3. Include following code before your application bootstraps:
This is machine, where PHPUnit tests are being executed.
228
-
229
-
By default the `baseUrl` setting from browser configuration is used as the `remote code coverage information url`. However if a need exists to set alternative url on per-test basis, then place following code in the `setUp` method of the test case class, that extends `BrowserTestCase` class:
230
-
231
-
```php
232
-
$this->setRemoteCoverageScriptUrl('http://host/'); // `host` should be replaced with web server's url
233
-
```
234
-
235
-
### How This Works
236
-
1. each test sets a special cookie on website under test
237
-
2. when cookie is present, then `RemoteCoverageTool.php` script collects coverage information and stores it on disk
238
-
3. once test finishes, then `http://host/?rct_mode=output` url is accessed on remote server, which in turn returns collected coverage information
239
-
4. remote coverage information is then joined with coverage information collected locally on test machine
240
-
241
-
## Browser Configuration in Details
242
-
Each browser configuration consists of the following settings:
243
-
244
-
| Name | Description |
245
-
|---|---|
246
-
|`host`| host, where Selenium Server is located (defaults to `localhost`) |
247
-
|`port`| port, on which Selenium Server is listening for incoming connections (defaults to `4444`) |
248
-
|`timeout`| connection timeout of the server in seconds (defaults to `60`) |
249
-
|`browserName`| name of browser to use (e.g. `firefox`, `chrome`, etc., defaults to `firefox`) |
There are also corresponding `set` and `get` methods for each of mentioned above settings, that allow to individually change them before test has started (from `setUp` method).
255
-
256
-
## Using Composer
257
-
258
-
1. Define the dependencies in your ```composer.json```:
20
+
Define the dependencies in your ```composer.json```:
259
21
```json
260
22
{
261
23
"require": {
@@ -264,7 +26,7 @@ There are also corresponding `set` and `get` methods for each of mentioned above
0 commit comments