Repository navigation
Expand file tree
/
Copy pathPhpBrowser.php
More file actions
246 lines (215 loc) · 8.71 KB
/
Copy pathPhpBrowser.php
File metadata and controls
246 lines (215 loc) · 8.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
namespace Codeception\Module;
use Behat\Mink\Driver\GoutteDriver;
use Codeception\Util\Connector\Goutte;
use Guzzle\Http\Client;
use Codeception\Exception\TestRuntime;
use Symfony\Component\BrowserKit\Request;
/**
* Uses [Mink](http://mink.behat.org) with [Goutte](https://github.com/fabpot/Goutte) and [Guzzle](http://guzzlephp.org/) to interact with your application over CURL.
* Module works over CURL and requires **PHP CURL extension** to be enabled.
*
* Use to perform web acceptance tests with non-javascript browser.
*
* If test fails stores last shown page in 'output' dir.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: davert.codecept@mailican.com
* * relies on [Mink](http://mink.behat.org) and [Guzzle](http://guzzlephp.org/)
*
* *Please review the code of non-stable modules and provide patches if you have issues.*
*
* ## Configuration
*
* * url *required* - start url of your app
* * curl - curl options
*
* ### Example (`acceptance.suite.yml`)
*
* modules:
* enabled: [PhpBrowser]
* config:
* PhpBrowser:
* url: 'http://localhost'
* curl:
* CURLOPT_RETURNTRANSFER: true
*
* ## Public Properties
*
* * session - contains Mink Session
* * guzzle - contains [Guzzle](http://guzzlephp.org/) client instance: `\Guzzle\Http\Client`
*
* All SSL certification checks are disabled by default.
* To configure CURL options use `curl` config parameter.
*
*/
class PhpBrowser extends \Codeception\Util\Mink implements \Codeception\Util\FrameworkInterface {
protected $requiredFields = array('url');
protected $config = array('curl' => array());
protected $curl_defaults = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CERTINFO => false,
);
/**
* @var \Guzzle\Http\Client
*/
public $guzzle;
public function _initialize() {
$client = new Goutte();
$driver = new \Behat\Mink\Driver\GoutteDriver($client);
// build up a Guzzle friendly list of configuration options
// passed in both from our defaults and the respective
// yaml configuration file (if applicable)
$curl_config['curl.options'] = $this->curl_defaults;
foreach ($this->config['curl'] as $key => $val) {
if (defined($key)) $curl_config['curl.options'][constant($key)] = $val;
}
// Guzzle client requires that we set the ssl.certificate_authority config
// directive if we wish to disable SSL verification
if ($curl_config['curl.options'][CURLOPT_SSL_VERIFYPEER] !== true) {
$curl_config['ssl.certificate_authority'] = false;
}
$client->setClient($this->guzzle = new Client('', $curl_config));
$this->session = new \Behat\Mink\Session($driver);
parent::_initialize();
}
public function submitForm($selector, $params) {
$form = $this->session->getPage()->find('css',$selector);
if ($form === null)
throw new TestRuntime("Form with selector: \"$selector\" was not found on given page.");
/** @var \Behat\Mink\Element\NodeElement[] $fields */
$fields = $this->session->getPage()->findAll('css', $selector . ' input:enabled, ' . $selector . ' input[type=hidden]');
$url = '';
foreach ($fields as $field) {
$fieldKey = $field->getAttribute('name');
$value = array_key_exists($fieldKey, $params) ? $params[$fieldKey] : $field->getValue();
$url .= sprintf('%s=%s', $fieldKey, $value) . '&';
}
$fields = $this->session->getPage()->findAll('css', $selector . ' textarea:enabled');
foreach ($fields as $field) {
$fieldKey = $field->getAttribute('name');
$value = array_key_exists($fieldKey, $params) ? $params[$fieldKey] : $field->getValue();
$url .= sprintf('%s=%s',$fieldKey, $value) . '&';
}
$fields = $this->session->getPage()->findAll('css', $selector . ' select:enabled');
foreach ($fields as $field) {
$fieldKey = $field->getAttribute('name');
$value = array_key_exists($fieldKey, $params) ? $params[$fieldKey] : $field->getValue();
$url .= sprintf('%s=%s',$fieldKey, $value) . '&';
}
$url .= '&'.http_build_query($params);
parse_str($url, $params);
$url = $form->getAttribute('action');
$method = $form->getAttribute('method');
$this->call($url, $method, $params);
}
public function sendAjaxPostRequest($uri, $params = array()) {
$this->session->setRequestHeader('X-Requested-With', 'XMLHttpRequest');
$this->call($uri, 'POST', $params);
$this->debug($this->session->getPage()->getContent());
$this->session->setRequestHeader('X-Requested-With', '');
}
public function sendAjaxGetRequest($uri, $params = array()) {
$this->session->setRequestHeader('X-Requested-With', 'XMLHttpRequest');
$query = $params ? '?'. http_build_query($params) : '';
$this->call($uri.$query, 'GET', $params);
$this->debug($this->session->getPage()->getContent());
$this->session->setRequestHeader('X-Requested-With', '');
}
public function seePageNotFound()
{
$this->seeResponseCodeIs(404);
}
public function seeResponseCodeIs($code)
{
$this->assertEquals($code, $this->session->getStatusCode());
}
public function amHttpAuthenticated($username, $password)
{
$this->session->getDriver()->setBasicAuth($username, $password);
}
/**
* Low-level API method.
* If Codeception commands are not enough, use [Guzzle HTTP Client](http://guzzlephp.org/) methods directly
*
* Example:
*
* ``` php
* <?php
* // from the official Guzzle manual
* $I->amGoingTo('Sign all requests with OAuth');
* $I->executeInGuzzle(function (\Guzzle\Http\Client $client) {
* $client->addSubscriber(new Guzzle\Plugin\Oauth\OauthPlugin(array(
* 'consumer_key' => '***',
* 'consumer_secret' => '***',
* 'token' => '***',
* 'token_secret' => '***'
* )));
* });
* ?>
* ```
*
* Not recommended this command too be used on regular basis.
* If Codeception lacks important Guzzle Client methods implement then and submit patches.
*
* @param callable $function
*/
public function executeInGuzzle(\Closure $function)
{
return $function($this->guzzle);
}
public function _setHeader($header, $value)
{
$this->session->setRequestHeader($header, $value);
}
public function _getResponseHeader($header)
{
$headers = $this->session->getResponseHeaders();
if (!isset($headers[$header])) return false;
return $headers[$header];
}
protected function call($uri, $method = 'get', $params = array())
{
if (strpos($uri,'#')) $uri = substr($uri,0,strpos($uri,'#'));
$browser = $this->session->getDriver()->getClient();
if ($browser instanceof Goutte && $method == 'get' && !empty($params)) {
$uri .= '?' . http_build_query($params);
$browser->request($method, $uri, array());
} else {
$browser->request($method, $uri, $params);
}
$this->debugPageInfo();
}
public function _failed(\Codeception\TestCase $test, $fail) {
$fileName = str_replace('::','-',$test->getFileName());
file_put_contents(\Codeception\Configuration::logDir().basename($fileName).'.page.fail.html', $this->session->getPage()->getContent());
}
protected function debugPageInfo()
{
/** @var $request Request **/
$request = $this->session->getDriver()->getClient()->getRequest();
$this->debugSection($request->getMethod(), $this->session->getCurrentUrl().' '.json_encode($request->getParameters()));
if (count($request->getCookies())) $this->debugSection('Cookies', json_encode($request->getCookies()));
$this->debugSection('Headers', json_encode($this->session->getDriver()->getResponseHeaders()));
$this->debugSection('Status', $this->session->getStatusCode());
}
public function seeCheckboxIsChecked($checkbox)
{
$node = $this->findField($checkbox);
if (!$node) {
$this->fail(", checkbox not found");
}
$this->assertEquals('checked', $node->getAttribute('checked'));
}
public function dontSeeCheckboxIsChecked($checkbox)
{
$node = $this->findField($checkbox);
if (!$node) {
$this->fail(", checkbox not found");
}
$this->assertNull($node->getAttribute('checked'));
}
}