1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2020 SURFnet B.V. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\Behat; |
20
|
|
|
|
21
|
|
|
use Behat\Mink\Driver\Selenium2Driver; |
22
|
|
|
use Behat\Mink\Exception\ExpectationException; |
23
|
|
|
use Behat\MinkExtension\Context\MinkContext as BaseMinkContext; |
24
|
|
|
use DOMDocument; |
25
|
|
|
use DOMXPath; |
26
|
|
|
use RobRichards\XMLSecLibs\XMLSecurityDSig; |
27
|
|
|
use RuntimeException; |
28
|
|
|
use SAML2\XML\mdui\Common; |
29
|
|
|
use SAML2\XML\shibmd\Scope; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Mink-enabled context. |
33
|
|
|
*/ |
34
|
|
|
class MinkContext extends BaseMinkContext |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var array a list of window names identified by the name the tester refers to them in the step definitions. |
38
|
|
|
* @example ['My tab' => 'WindowNameGivenByBrowser', 'My other tab' => 'WindowNameGivenByBrowser'] |
39
|
|
|
*/ |
40
|
|
|
private $windows = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @Then /^the response should contain \'([^\']*)\'$/ |
44
|
|
|
*/ |
45
|
|
|
public function theResponseShouldContain($string) |
46
|
|
|
{ |
47
|
|
|
$this->assertSession()->responseContains($string); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @Then /^the response should match xpath \'([^\']*)\'$/ |
52
|
|
|
*/ |
53
|
|
|
public function theResponseShouldMatchXpath($xpath) |
54
|
|
|
{ |
55
|
|
|
$document = new DOMDocument(); |
56
|
|
|
if ($this->getSession()->getDriver() instanceof Selenium2Driver) { |
57
|
|
|
// Chrome uses a user friendly viewer, get the xml from the dressed document and assert on that xml. |
58
|
|
|
$xml = $this->getSession()->evaluateScript("document.getElementById('webkit-xml-viewer-source-xml').innerHTML"); |
59
|
|
|
} else { |
60
|
|
|
$xml = $this->getSession()->getPage()->getContent(); |
61
|
|
|
} |
62
|
|
|
$document->loadXML($xml); |
63
|
|
|
|
64
|
|
|
$xpathObj = new DOMXPath($document); |
65
|
|
|
$xpathObj->registerNamespace('ds', XMLSecurityDSig::XMLDSIGNS); |
66
|
|
|
$xpathObj->registerNamespace('mdui', Common::NS); |
67
|
|
|
$xpathObj->registerNamespace('mdash', Common::NS); |
68
|
|
|
$xpathObj->registerNamespace('shibmd', Scope::NS); |
69
|
|
|
$nodeList = $xpathObj->query($xpath); |
70
|
|
|
|
71
|
|
View Code Duplication |
if (!$nodeList || $nodeList->length === 0) { |
|
|
|
|
72
|
|
|
$message = sprintf('The xpath "%s" did not result in at least one match.', $xpath); |
73
|
|
|
throw new ExpectationException($message, $this->getSession()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Then /^the response should not match xpath \'([^\']*)\'$/ |
79
|
|
|
*/ |
80
|
|
|
public function theResponseShouldNotMatchXpath($xpath) |
81
|
|
|
{ |
82
|
|
|
$document = new DOMDocument(); |
83
|
|
|
$document->loadXML($this->getSession()->getPage()->getContent()); |
84
|
|
|
|
85
|
|
|
$xpathObj = new DOMXPath($document); |
86
|
|
|
$xpathObj->registerNamespace('ds', XMLSecurityDSig::XMLDSIGNS); |
87
|
|
|
$xpathObj->registerNamespace('mdui', Common::NS); |
88
|
|
|
$nodeList = $xpathObj->query($xpath); |
89
|
|
|
|
90
|
|
View Code Duplication |
if ($nodeList && $nodeList->length > 0) { |
|
|
|
|
91
|
|
|
$message = sprintf( |
92
|
|
|
'The xpath "%s" resulted in "%d" matches, where it should result in no matches"', |
93
|
|
|
$xpath, |
94
|
|
|
$nodeList->length |
95
|
|
|
); |
96
|
|
|
throw new ExpectationException($message, $this->getSession()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @Given /^I should see URL "([^"]*)"$/ |
102
|
|
|
*/ |
103
|
|
|
public function iShouldSeeUrl($url) |
104
|
|
|
{ |
105
|
|
|
$this->assertSession()->responseContains($url); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Given /^I should not see URL "([^"]*)"$/ |
110
|
|
|
*/ |
111
|
|
|
public function iShouldNotSeeUrl($url) |
112
|
|
|
{ |
113
|
|
|
$this->assertSession()->responseNotContains($url); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @Given /^I open (\d+) browser tabs identified by "([^"]*)"$/ |
118
|
|
|
*/ |
119
|
|
|
public function iOpenTwoBrowserTabsIdentifiedBy($numberOfTabs, $tabNames) |
120
|
|
|
{ |
121
|
|
|
// Make sure the browser is ready (without this other browser interactions fail) |
122
|
|
|
$this->getSession()->visit($this->locatePath('#')); |
123
|
|
|
|
124
|
|
|
$tabs = explode(',', $tabNames); |
125
|
|
|
if (count($tabs) != $numberOfTabs) { |
126
|
|
|
throw new RuntimeException( |
127
|
|
|
'Please identify all tabs you are opening in order to refer to them at a later stage' |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
foreach ($tabs as $tab) { |
132
|
|
|
$this->getMink() |
133
|
|
|
->getSession() |
134
|
|
|
->executeScript("window.open('/','_blank');"); |
135
|
|
|
|
136
|
|
|
$windowsNames = $this->getSession()->getWindowNames(); |
137
|
|
|
|
138
|
|
|
if (!$windowsNames) { |
139
|
|
|
throw new RuntimeException('The windows where not opened correctly.'); |
140
|
|
|
} |
141
|
|
|
// Grab the window name (which is the last one added to the window list) |
142
|
|
|
$windowName = array_pop($windowsNames); |
143
|
|
|
// Keep track of the opened windows in order allow switching between them |
144
|
|
|
$this->windows[trim($tab)] = $windowName; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @Given /^I switch to "([^"]*)"$/ |
151
|
|
|
*/ |
152
|
|
|
public function iSwitchToWindow($windowName) |
153
|
|
|
{ |
154
|
|
|
// (re) set the default session to the chrome session. |
155
|
|
|
$this->switchToWindow($windowName); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function switchToWindow($windowName) |
159
|
|
|
{ |
160
|
|
|
if (!isset($this->windows[$windowName])) { |
161
|
|
|
throw new RuntimeException(sprintf('Unknown window/tab name "%s"', $windowName)); |
162
|
|
|
} |
163
|
|
|
$this->getSession()->switchToWindow($this->windows[$windowName]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.