Repository navigation
Expand file tree
/
Copy pathZombieJS.php
More file actions
140 lines (126 loc) · 4.6 KB
/
Copy pathZombieJS.php
File metadata and controls
140 lines (126 loc) · 4.6 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
<?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@0.13.0 @```
* Note: Behat/Mink states that there are compatibility issues with zombie > 0.13, and their manual
* says to install version 0.12.15, BUT it has some bugs, so you'd rather install 0.13
*
* 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**.
*
* Also note that this module requires php5-http PECL extension to parse returned headers properly
*
* 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 _after(\Codeception\TestCase $test) {
//that call does not really terminate node process
//@see https://github.com/symfony/symfony/issues/5499
$this->session->stop();
//so we kill it ourselves
exec('killall '.pathinfo($this->server->getNodeBin(),PATHINFO_BASENAME).' > /dev/null 2>&1');
}
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(){
stream.end(http.getAllResponseHeaders());
};
http.send(null);
JS
,addslashes($url))
);
if (method_exists('\http\Header', 'parse')) {
return \http\Header::parse(str_replace("\n","\r\n",$headers));
} else {
return http_parse_headers(str_replace("\n","\r\n",$headers));
}
}
}