1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\DrupalExtension; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Mink; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Provides helpful methods to interact with the Mink session. |
9
|
|
|
* |
10
|
|
|
* This is making the functionality from RawMinkContext available for reuse in |
11
|
|
|
* classes that do not extend it. |
12
|
|
|
* |
13
|
|
|
* @see \Behat\MinkExtension\Context\RawMinkContext |
14
|
|
|
*/ |
15
|
|
|
trait MinkAwareTrait |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The Mink sessions manager. |
20
|
|
|
* |
21
|
|
|
* @var \Behat\Mink\Mink |
22
|
|
|
*/ |
23
|
|
|
protected $mink; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The parameters for the Mink extension. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $minkParameters; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets the Mink sessions manager. |
34
|
|
|
* |
35
|
|
|
* @param \Behat\Mink\Mink $mink |
36
|
|
|
* The Mink sessions manager. |
37
|
|
|
*/ |
38
|
|
|
public function setMink(Mink $mink) |
39
|
|
|
{ |
40
|
|
|
$this->mink = $mink; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the Mink sessions manager. |
45
|
|
|
* |
46
|
|
|
* @return \Behat\Mink\Mink |
47
|
|
|
* The Mink sessions manager. |
48
|
|
|
*/ |
49
|
|
|
public function getMink() |
50
|
|
|
{ |
51
|
|
|
return $this->mink; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns the Mink session. |
56
|
|
|
* |
57
|
|
|
* @param string|null $name |
58
|
|
|
* The name of the session to return. If omitted the active session will |
59
|
|
|
* be returned. |
60
|
|
|
* |
61
|
|
|
* @return \Behat\Mink\Session |
62
|
|
|
* The Mink session. |
63
|
|
|
*/ |
64
|
|
|
public function getSession($name = null) |
65
|
|
|
{ |
66
|
|
|
return $this->getMink()->getSession($name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the parameters provided for Mink. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getMinkParameters() |
75
|
|
|
{ |
76
|
|
|
return $this->minkParameters; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets parameters provided for Mink. |
81
|
|
|
* |
82
|
|
|
* @param array $parameters |
83
|
|
|
*/ |
84
|
|
|
public function setMinkParameters(array $parameters) |
85
|
|
|
{ |
86
|
|
|
$this->minkParameters = $parameters; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns a specific mink parameter. |
91
|
|
|
* |
92
|
|
|
* @param string $name |
93
|
|
|
* |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
|
|
public function getMinkParameter($name) |
97
|
|
|
{ |
98
|
|
|
return isset($this->minkParameters[$name]) ? $this->minkParameters[$name] : null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Applies the given parameter to the Mink configuration. |
103
|
|
|
* |
104
|
|
|
* Consider that this will only be applied in scope of the class that is |
105
|
|
|
* using this trait. |
106
|
|
|
* |
107
|
|
|
* @param string $name The key of the parameter |
108
|
|
|
* @param string $value The value of the parameter |
109
|
|
|
*/ |
110
|
|
|
public function setMinkParameter($name, $value) |
111
|
|
|
{ |
112
|
|
|
$this->minkParameters[$name] = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns Mink session assertion tool. |
117
|
|
|
* |
118
|
|
|
* @param string|null $name |
119
|
|
|
* The name of the session to return. If omitted the active session will |
120
|
|
|
* be returned. |
121
|
|
|
* |
122
|
|
|
* @return \Behat\Mink\WebAssert |
123
|
|
|
*/ |
124
|
|
|
public function assertSession($name = null) |
125
|
|
|
{ |
126
|
|
|
return $this->getMink()->assertSession($name); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Visits provided relative path using provided or default session. |
131
|
|
|
* |
132
|
|
|
* @param string $path |
133
|
|
|
* @param string|null $sessionName |
134
|
|
|
*/ |
135
|
|
|
public function visitPath($path, $sessionName = null) |
136
|
|
|
{ |
137
|
|
|
$this->getSession($sessionName)->visit($this->locatePath($path)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Locates url, based on provided path. |
142
|
|
|
* Override to provide custom routing mechanism. |
143
|
|
|
* |
144
|
|
|
* @param string $path |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function locatePath($path) |
149
|
|
|
{ |
150
|
|
|
$startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/'; |
151
|
|
|
|
152
|
|
|
return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Save a screenshot of the current window to the file system. |
157
|
|
|
* |
158
|
|
|
* @param string $filename |
159
|
|
|
* Desired filename, defaults to <browser>_<ISO 8601 date>_<randomId>.png. |
160
|
|
|
* @param string $filepath |
161
|
|
|
* Desired filepath, defaults to upload_tmp_dir, falls back to |
162
|
|
|
* sys_get_temp_dir(). |
163
|
|
|
*/ |
164
|
|
|
public function saveScreenshot($filename = null, $filepath = null) |
165
|
|
|
{ |
166
|
|
|
// Under Cygwin, uniqid with more_entropy must be set to true. |
167
|
|
|
// No effect in other environments. |
168
|
|
|
$filename = $filename ?: sprintf('%s_%s_%s.%s', $this->getMinkParameter('browser_name'), date('c'), uniqid('', true), 'png'); |
169
|
|
|
$filepath = $filepath ? $filepath : (ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir()); |
170
|
|
|
file_put_contents($filepath . '/' . $filename, $this->getSession()->getScreenshot()); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|