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
|
|
|
* Sets Mink instance. |
30
|
|
|
* |
31
|
|
|
* @param Mink $mink Mink session manager |
32
|
|
|
*/ |
33
|
|
|
public function setMink(Mink $mink) |
34
|
|
|
{ |
35
|
|
|
$this->mink = $mink; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns Mink instance. |
40
|
|
|
* |
41
|
|
|
* @return Mink |
42
|
|
|
*/ |
43
|
|
|
public function getMink() |
44
|
|
|
{ |
45
|
|
|
if (null === $this->mink) { |
46
|
|
|
throw new \RuntimeException( |
47
|
|
|
'Mink instance has not been set on Mink context class. ' . |
48
|
|
|
'Have you enabled the Mink Extension?' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->mink; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the parameters provided for Mink. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getMinkParameters() |
61
|
|
|
{ |
62
|
|
|
return $this->minkParameters; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sets parameters provided for Mink. |
67
|
|
|
* |
68
|
|
|
* @param array $parameters |
69
|
|
|
*/ |
70
|
|
|
public function setMinkParameters(array $parameters) |
71
|
|
|
{ |
72
|
|
|
$this->minkParameters = $parameters; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns specific mink parameter. |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function getMinkParameter($name) |
83
|
|
|
{ |
84
|
|
|
return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Applies the given parameter to the Mink configuration. Consider that all parameters get reset for each |
89
|
|
|
* feature context. |
90
|
|
|
* |
91
|
|
|
* @param string $name The key of the parameter |
92
|
|
|
* @param string $value The value of the parameter |
93
|
|
|
*/ |
94
|
|
|
public function setMinkParameter($name, $value) |
95
|
|
|
{ |
96
|
|
|
$this->minkParameters[$name] = $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns Mink session. |
101
|
|
|
* |
102
|
|
|
* @param string|null $name name of the session OR active session will be used |
103
|
|
|
* |
104
|
|
|
* @return Session |
105
|
|
|
*/ |
106
|
|
|
public function getSession($name = null) |
107
|
|
|
{ |
108
|
|
|
return $this->getMink()->getSession($name); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns Mink session assertion tool. |
113
|
|
|
* |
114
|
|
|
* @param string|null $name name of the session OR active session will be used |
115
|
|
|
* |
116
|
|
|
* @return WebAssert |
117
|
|
|
*/ |
118
|
|
|
public function assertSession($name = null) |
119
|
|
|
{ |
120
|
|
|
return $this->getMink()->assertSession($name); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Visits provided relative path using provided or default session. |
125
|
|
|
* |
126
|
|
|
* @param string $path |
127
|
|
|
* @param string|null $sessionName |
128
|
|
|
*/ |
129
|
|
|
public function visitPath($path, $sessionName = null) |
130
|
|
|
{ |
131
|
|
|
$this->getSession($sessionName)->visit($this->locatePath($path)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Locates url, based on provided path. |
136
|
|
|
* Override to provide custom routing mechanism. |
137
|
|
|
* |
138
|
|
|
* @param string $path |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function locatePath($path) |
143
|
|
|
{ |
144
|
|
|
$startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/'; |
145
|
|
|
|
146
|
|
|
return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Save a screenshot of the current window to the file system. |
151
|
|
|
* |
152
|
|
|
* @param string $filename Desired filename, defaults to |
153
|
|
|
* <browser_name>_<ISO 8601 date>_<randomId>.png |
154
|
|
|
* @param string $filepath Desired filepath, defaults to |
155
|
|
|
* upload_tmp_dir, falls back to sys_get_temp_dir() |
156
|
|
|
*/ |
157
|
|
|
public function saveScreenshot($filename = null, $filepath = null) |
158
|
|
|
{ |
159
|
|
|
// Under Cygwin, uniqid with more_entropy must be set to true. |
160
|
|
|
// No effect in other environments. |
161
|
|
|
$filename = $filename ?: sprintf('%s_%s_%s.%s', $this->getMinkParameter('browser_name'), date('c'), uniqid('', true), 'png'); |
162
|
|
|
$filepath = $filepath ?: (ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir()); |
163
|
|
|
file_put_contents($filepath . '/' . $filename, $this->getSession()->getScreenshot()); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|