|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Behat MinkExtension. |
|
5
|
|
|
* (c) Konstantin Kudryashov <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Behat\MinkExtension\Context; |
|
12
|
|
|
|
|
13
|
|
|
use Behat\Mink\Mink; |
|
14
|
|
|
use Behat\Mink\WebAssert; |
|
15
|
|
|
use Behat\Mink\Session; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Raw Mink context for Behat BDD tool. |
|
19
|
|
|
* Provides raw Mink integration (without step definitions) and web assertions. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Konstantin Kudryashov <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class RawMinkContext implements MinkAwareContext |
|
24
|
|
|
{ |
|
25
|
|
|
private $mink; |
|
26
|
|
|
private $minkParameters; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Currently supports PHPSTORM. |
|
30
|
|
|
* |
|
31
|
|
|
* @beforeScenario |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setUpXdebugIfIdeIsConfigured() |
|
34
|
|
|
{ |
|
35
|
|
|
if (isset($_SERVER['XDEBUG_CONFIG'])) { |
|
36
|
|
|
$this->getSession()->setCookie('XDEBUG_SESSION', 'xdebug'); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Sets Mink instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Mink $mink Mink session manager |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setMink(Mink $mink) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->mink = $mink; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns Mink instance. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Mink |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getMink() |
|
56
|
|
|
{ |
|
57
|
|
|
if (null === $this->mink) { |
|
58
|
|
|
throw new \RuntimeException( |
|
59
|
|
|
'Mink instance has not been set on Mink context class. ' . |
|
60
|
|
|
'Have you enabled the Mink Extension?' |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->mink; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns the parameters provided for Mink. |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getMinkParameters() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->minkParameters; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets parameters provided for Mink. |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $parameters |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setMinkParameters(array $parameters) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->minkParameters = $parameters; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns specific mink parameter. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name |
|
91
|
|
|
* |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getMinkParameter($name) |
|
95
|
|
|
{ |
|
96
|
|
|
return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Applies the given parameter to the Mink configuration. Consider that all parameters get reset for each |
|
101
|
|
|
* feature context. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $name The key of the parameter |
|
104
|
|
|
* @param string $value The value of the parameter |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setMinkParameter($name, $value) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->minkParameters[$name] = $value; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns Mink session. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string|null $name name of the session OR active session will be used |
|
115
|
|
|
* |
|
116
|
|
|
* @return Session |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getSession($name = null) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getMink()->getSession($name); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns Mink session assertion tool. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string|null $name name of the session OR active session will be used |
|
127
|
|
|
* |
|
128
|
|
|
* @return WebAssert |
|
129
|
|
|
*/ |
|
130
|
|
|
public function assertSession($name = null) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->getMink()->assertSession($name); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Visits provided relative path using provided or default session. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $path |
|
139
|
|
|
* @param string|null $sessionName |
|
140
|
|
|
*/ |
|
141
|
|
|
public function visitPath($path, $sessionName = null) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->getSession($sessionName)->visit($this->locatePath($path)); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Locates url, based on provided path. |
|
148
|
|
|
* Override to provide custom routing mechanism. |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $path |
|
151
|
|
|
* |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function locatePath($path) |
|
155
|
|
|
{ |
|
156
|
|
|
$startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/'; |
|
157
|
|
|
|
|
158
|
|
|
return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Save a screenshot of the current window to the file system. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $filename Desired filename, defaults to |
|
165
|
|
|
* <browser_name>_<ISO 8601 date>_<randomId>.png |
|
166
|
|
|
* @param string $filepath Desired filepath, defaults to |
|
167
|
|
|
* upload_tmp_dir, falls back to sys_get_temp_dir() |
|
168
|
|
|
*/ |
|
169
|
|
|
public function saveScreenshot($filename = null, $filepath = null) |
|
170
|
|
|
{ |
|
171
|
|
|
// Under Cygwin, uniqid with more_entropy must be set to true. |
|
172
|
|
|
// No effect in other environments. |
|
173
|
|
|
$filename = $filename ?: sprintf('%s_%s_%s.%s', $this->getMinkParameter('browser_name'), date('c'), uniqid('', true), 'png'); |
|
174
|
|
|
$filepath = $filepath ?: (ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir()); |
|
175
|
|
|
file_put_contents($filepath . '/' . $filename, $this->getSession()->getScreenshot()); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|