1 | <?php |
||
15 | class GenericFeatureContext extends MinkContext |
||
16 | { |
||
17 | /** @var int UI generic delay duration in milliseconds */ |
||
18 | const DELAY = 1000; |
||
19 | /** @var 0.1 sec spin delay */ |
||
20 | const SPIN_DELAY = 100000; |
||
21 | /** @var int UI javascript generic delay duration in milliseconds */ |
||
22 | const JS_DELAY = self::DELAY / 5; |
||
23 | /** @var int UI spin function timeout for ex 30*0.1s = 15 sec timeout */ |
||
24 | const SPIN_TIMEOUT = 150; |
||
25 | |||
26 | /** @var mixed */ |
||
27 | protected $session; |
||
28 | |||
29 | /** @var array Pages collection */ |
||
30 | protected $pages = []; |
||
31 | |||
32 | /** |
||
33 | * Initializes context. |
||
34 | * |
||
35 | * Every scenario gets its own context instance. |
||
36 | * You can also pass arbitrary arguments to the |
||
37 | * context constructor through behat.yml. |
||
38 | * |
||
39 | * @param mixed $session |
||
40 | */ |
||
41 | public function __construct($session = null) |
||
47 | |||
48 | /** |
||
49 | * Get Symfony service instance. |
||
50 | * |
||
51 | * @param string $serviceName Service identifier |
||
52 | * @param string $session Behat Symfony session name |
||
53 | * |
||
54 | * @return object Symfony service instance |
||
55 | */ |
||
56 | public function getSymfonyService(string $serviceName, string $session = 'symfony2') |
||
64 | |||
65 | /** |
||
66 | * Spin function to avoid Selenium fails. |
||
67 | * |
||
68 | * @param callable $lambda |
||
69 | * @param null $data |
||
70 | * @param int $delay |
||
71 | * @param int $timeout |
||
72 | * |
||
73 | * @throws \Exception |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function spin(callable $lambda, $data = null, $delay = self::SPIN_DELAY, $timeout = self::SPIN_TIMEOUT) |
||
100 | |||
101 | /** |
||
102 | * @AfterStep |
||
103 | * |
||
104 | * @param AfterStepScope $scope |
||
105 | */ |
||
106 | public function takeScreenShotAfterFailedStep(AfterStepScope $scope) |
||
120 | |||
121 | /** |
||
122 | * Find all elements by CSS selector. |
||
123 | * |
||
124 | * @param string $selector CSS selector |
||
125 | * |
||
126 | * @throws \InvalidArgumentException If element not found |
||
127 | * |
||
128 | * @return \Behat\Mink\Element\NodeElement[] |
||
129 | */ |
||
130 | protected function findAllByCssSelector(string $selector) |
||
150 | |||
151 | /** |
||
152 | * Find element by CSS selector. |
||
153 | * |
||
154 | * @param string $selector CSS selector |
||
155 | * |
||
156 | * @throws \InvalidArgumentException If element not found |
||
157 | * |
||
158 | * @return \Behat\Mink\Element\NodeElement |
||
159 | */ |
||
160 | public function findByCssSelector(string $selector) : NodeElement |
||
164 | |||
165 | /** |
||
166 | * @Given /^I set browser window size to "([^"]*)" x "([^"]*)"$/ |
||
167 | * |
||
168 | * @param int $width Browser window width |
||
169 | * @param int $height Browser window height |
||
170 | */ |
||
171 | public function iSetBrowserWindowSizeToX($width, $height) |
||
175 | |||
176 | /** |
||
177 | * @Given /^I wait "([^"]*)" milliseconds for response$/ |
||
178 | * |
||
179 | * @param int $delay Amount of milliseconds to wait |
||
180 | */ |
||
181 | public function iWaitMillisecondsForResponse($delay = self::DELAY) |
||
185 | |||
186 | /** |
||
187 | * Click on the element with the provided xpath query. |
||
188 | * |
||
189 | * @When I click on the element :arg1 |
||
190 | * |
||
191 | * @param string $selector CSS element selector |
||
192 | * |
||
193 | * @throws \InvalidArgumentException |
||
194 | */ |
||
195 | public function iClickOnTheElement(string $selector) |
||
200 | |||
201 | /** |
||
202 | * Checks, that current page PATH is equal to specified |
||
203 | * Example: Then I should be on "/" |
||
204 | * Example: And I should be on "/bats" |
||
205 | * Example: And I should be on "http://google.com" |
||
206 | * |
||
207 | * @param string $page Page for assertion |
||
208 | */ |
||
209 | public function assertPageAddress($page) { |
||
215 | |||
216 | /** |
||
217 | * @When /^I hover over the element "([^"]*)"$/ |
||
218 | * |
||
219 | * @param string $selector CSS element selector |
||
220 | * |
||
221 | * @throws \InvalidArgumentException |
||
222 | */ |
||
223 | public function iHoverOverTheElement(string $selector) |
||
227 | |||
228 | /** |
||
229 | * Fill in input with the provided info. |
||
230 | * |
||
231 | * @When I fill in the input :arg1 with :arg2 |
||
232 | * |
||
233 | * @param string $selector CSS element selector |
||
234 | * @param string $value Element value for filling in |
||
235 | * |
||
236 | * @throws \InvalidArgumentException |
||
237 | */ |
||
238 | public function iFillInTheElement(string $selector, string $value) |
||
242 | |||
243 | /** |
||
244 | * @When I scroll vertically to :arg1 px |
||
245 | * |
||
246 | * @param mixed $yPos Vertical scrolling position in pixels |
||
247 | */ |
||
248 | public function iScrollVerticallyToPx($yPos) |
||
252 | |||
253 | /** |
||
254 | * @When I scroll horizontally to :arg1 px |
||
255 | * |
||
256 | * @param mixed $xPos Horizontal scrolling position in pixels |
||
257 | */ |
||
258 | public function iScrollHorizontallyToPx($xPos) |
||
262 | |||
263 | /** |
||
264 | * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/ |
||
265 | * |
||
266 | * @param string $field Field name |
||
267 | * @param string $value Field value |
||
268 | */ |
||
269 | public function iFillHiddenFieldWith(string $field, string $value) |
||
276 | |||
277 | /** |
||
278 | * @Then I check custom checkbox with :id |
||
279 | * |
||
280 | * @param string $id Checkbox identifier |
||
281 | * |
||
282 | * @throws \InvalidArgumentException If checkbox with provided identifier does not exists |
||
283 | */ |
||
284 | public function iCheckCustomCheckboxWith(string $id) |
||
297 | |||
298 | /** |
||
299 | * @Then I drag element :selector to :target |
||
300 | * |
||
301 | * @param string $selector Source element for dragging |
||
302 | * @param string $target Target element to drag to |
||
303 | * |
||
304 | * @throws \InvalidArgumentException |
||
305 | */ |
||
306 | public function dragElementTo(string $selector, string $target) |
||
312 | |||
313 | /** |
||
314 | * Fill in input with the provided info. |
||
315 | * |
||
316 | * @When I fill in the element :arg1 with value :arg2 using js |
||
317 | * |
||
318 | * @param string $selector CSS element selector |
||
319 | * @param string $value Element value for filling in |
||
320 | */ |
||
321 | public function iFillInTheElementUsingJs(string $selector, string $value) |
||
325 | |||
326 | /** |
||
327 | * @Given /^I should be on "(?P<page>(?:[^"]|\\")*)" page$/ |
||
328 | * |
||
329 | * @param string $page Page name |
||
330 | * |
||
331 | * @throws \Exception If page is not defined |
||
332 | */ |
||
333 | public function iShouldBeOnPage(string $page) |
||
342 | |||
343 | /** |
||
344 | * Add pages collection. |
||
345 | * |
||
346 | * @param array $pages Pages collection |
||
347 | */ |
||
348 | public function addPages(array $pages) |
||
352 | } |
||
353 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: