Repository navigation
Expand file tree
/
Copy pathZombieJS.php
More file actions
133 lines (121 loc) · 4.19 KB
/
Copy pathZombieJS.php
File metadata and controls
133 lines (121 loc) · 4.19 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
<?php
namespace Codeception\Module;
/**
* Uses Mink to manipulate Zombie.js headless browser (http://zombie.labnotes.org/)
*
* Note, all methods take CSS selectors to fetch elements.
* For links, buttons, fields you can use names/values/ids of elements.
* For form fields you can use input[name=fieldname] notation.
*
* ## Status
*
* * Maintainer: **synchrone**
* * Stability: **stable**
* * Contact: https://github.com/synchrone
* * relies on [Mink](http://mink.behat.org)
*
*
* ## Installation
*
* In order to talk with zombie.js server, you should install and configure zombie.js first:
*
* * Install node.js by following instructions from the official site: http://nodejs.org/.
* * Install npm (node package manager) by following instructions from the http://npmjs.org/.
* * Install zombie.js with npm:
* ``` $ npm install -g zombie@1```
*
* After installing npm and zombie.js, you’ll need to add npm libs to your **NODE_PATH**. The easiest way to do this is to add:
*
* ``` export NODE_PATH="/PATH/TO/NPM/node_modules" ```
* into your **.bashrc**.
*
* Don't forget to turn on Db repopulation if you are using database.
*
* ## Configuration
*
* * url *required*- url of your site
* * host - simply defines the host on which zombie.js will be started. It’s **127.0.0.1** by default.
* * port - defines a zombie.js port. Default one is **8124**.
* * node_bin - defines full path to node.js binary. Default one is just **node**
* * script - defines a node.js script to start zombie.js server. If you pass a **null** the default script will be used. Use this option carefully!
* * threshold - amount of milliseconds (1/1000 of second) for the process to wait (as of \Behat\Mink\Driver\Zombie\Server)
* * autostart - whether zombie.js should be started automatically. Defaults to **true**
*
* ### Example (`acceptance.suite.yml`)
*
* modules:
* enabled: [ZombieJS]
* config:
* ZombieJS:
* url: 'http://localhost/'
* host: '127.0.0.1'
* port: 8124
*
* ## Public Properties
*
* * session - contains Mink Session
*/
use Behat\Mink\Mink,
Behat\Mink\Session,
Behat\Mink\Driver\ZombieDriver,
Behat\Mink\Driver\NodeJS\Server\ZombieServer;
class ZombieJS extends \Codeception\Util\MinkJS
{
protected $requiredFields = array('url');
protected $config = array(
'host' => '127.0.0.1', 'port' => 8124,
'node_bin' => null, 'script' => null,
'threshold' => 20000,
'autostart' => true
);
/** @var Session */
public $session;
/** @var ZombieServer */
protected $server;
/** @var ZombieDriver */
protected $driver;
public function _initialize()
{
$this->server = new ZombieServer(
$this->config['host'],$this->config['port'],
$this->config['node_bin'],$this->config['script'],
$this->config['threshold'] * 1000
);
$this->driver = new ZombieDriver($this->server);
$this->session = new Session($this->driver);
parent::_initialize();
}
public function _failed(\Codeception\TestCase $test, $error) {
$this->_after($test);
}
public function _getUrl() {
return 'http://'.$this->config['host'].':'.$this->config['port'];
}
/**
* @param string $url The URL to make HEAD request to
* @return array Header-Name => Value array
*/
public function headRequest($url){
$headers = $this->server->evalJS(sprintf(<<<JS
var http = new browser.window.XMLHttpRequest();
http.open('HEAD', '%s');
http.onreadystatechange = function(){
if(http.readyState==4){
stream.end('HTTP/1.0 '+http.status+' '+http.statusText+'\\n'+http.getAllResponseHeaders());
}
};
http.send(null);
JS
,addslashes($url))
);
if(class_exists('\Guzzle\Parser\Message\MessageParser'))
{
$p = new \Guzzle\Parser\Message\MessageParser();
$parts = $p->parseResponse($headers);
return $parts['headers'];
}
else{
throw new \Exception("Could not parse response headers. Please install Guzzle");
}
}
}