|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Modules/Browser |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv2\Modules\Browser; |
|
45
|
|
|
|
|
46
|
|
|
use Prose\Prose; |
|
47
|
|
|
use Storyplayer\SPv2\Modules\Browser; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Test the current contents of the browser |
|
51
|
|
|
* |
|
52
|
|
|
* @category Libraries |
|
53
|
|
|
* @package Storyplayer/Modules/Browser |
|
54
|
|
|
* @author Stuart Herbert <[email protected]> |
|
55
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
56
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
57
|
|
|
* @link http://datasift.github.io/storyplayer |
|
58
|
|
|
*/ |
|
59
|
|
|
class ExpectsBrowser extends Prose |
|
60
|
|
|
{ |
|
61
|
|
|
protected function initActions() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->initDevice(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function doesntHave() |
|
67
|
|
|
{ |
|
68
|
|
|
$action = function($requiredCount, $elements, $elementName, $elementDesc) { |
|
69
|
|
|
|
|
70
|
|
|
$log = usingLog()->startAction("$elementDesc '$elementName' must not exist"); |
|
71
|
|
|
|
|
72
|
|
|
// how many elements actually exist? |
|
73
|
|
|
$actualCount = count($elements); |
|
74
|
|
|
|
|
75
|
|
|
// this gets a little complicated |
|
76
|
|
|
switch ($requiredCount) { |
|
77
|
|
|
// this satisfies something like: |
|
78
|
|
|
// |
|
79
|
|
|
// expectsBrowser()->doesntHave()->anyFieldsWithId('XXX'); |
|
80
|
|
|
case null: |
|
81
|
|
|
case 'any': |
|
82
|
|
|
if ($actualCount === 0) { |
|
83
|
|
|
$log->endAction("0 element(s) found"); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "0 element(s) to exist", "$actualCount element(s) exist"); |
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
case 'several': |
|
89
|
|
|
if ($actualCount < 3 || $actualCount > 9) { |
|
90
|
|
|
$log->endAction($actualCount . " element(s) found"); |
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "must not be several element(s)", "several element(s) exist"); |
|
95
|
|
|
|
|
96
|
|
|
// this satisfies something like: |
|
97
|
|
|
// |
|
98
|
|
|
// expectsBrowser()->doesntHave()->fiveFieldsWithId('XX'); |
|
99
|
|
View Code Duplication |
default: |
|
100
|
|
|
if ($actualCount != $requiredCount) { |
|
101
|
|
|
$log->endAction($actualCount . " element(s) found"); |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "$requiredCount element(s) must not exist", "$actualCount element(s) exist"); |
|
106
|
|
|
} |
|
107
|
|
|
}; |
|
108
|
|
|
|
|
109
|
|
|
return new MultiElementAction( |
|
110
|
|
|
$action, |
|
111
|
|
|
"doesntHave", |
|
112
|
|
|
$this->getTopElement() |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function has() |
|
117
|
|
|
{ |
|
118
|
|
|
$action = function($requiredCount, $elements, $elementName, $elementDesc) { |
|
119
|
|
|
|
|
120
|
|
|
$log = usingLog()->startAction("$elementDesc '$elementName' must exist"); |
|
121
|
|
|
|
|
122
|
|
|
$actualCount = count($elements); |
|
123
|
|
|
switch($requiredCount) { |
|
124
|
|
|
// this satisfies something like: |
|
125
|
|
|
// |
|
126
|
|
|
// expectsBrowser()->has()->fieldWithId('XX'); |
|
127
|
|
|
case null: |
|
128
|
|
View Code Duplication |
case 'any': |
|
129
|
|
|
if ($actualCount > 0) { |
|
130
|
|
|
$log->endAction($actualCount . " element(s) found"); |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "at least one element to exist", "$actualCount element(s) exist"); |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
case 'several': |
|
137
|
|
|
if ($actualCount > 2 && $actualCount < 10) { |
|
138
|
|
|
$log->endAction($actualCount . " element(s) found"); |
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "several element to exist", "$actualCount element(s) exist"); |
|
143
|
|
|
|
|
144
|
|
|
// this satisfies something like: |
|
145
|
|
|
// |
|
146
|
|
|
// expectsBrowser()->has()->oneFieldWithId('XX'); |
|
147
|
|
View Code Duplication |
default: |
|
148
|
|
|
if ($actualCount == $requiredCount) { |
|
149
|
|
|
$log->endAction($actualCount . " element(s) found"); |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "$requiredCount element(s) to exist", "$actualCount element(s) exist"); |
|
154
|
|
|
} |
|
155
|
|
|
}; |
|
156
|
|
|
|
|
157
|
|
|
return new MultiElementAction( |
|
158
|
|
|
$action, |
|
159
|
|
|
"has", |
|
160
|
|
|
$this->getTopElement() |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function hasTitle($title) |
|
165
|
|
|
{ |
|
166
|
|
|
// what are we doing? |
|
167
|
|
|
$log = usingLog()->startAction("page title must be {$title}"); |
|
168
|
|
|
|
|
169
|
|
|
// get the browser title |
|
170
|
|
|
$browserTitle = Browser::fromBrowser()->getTitle(); |
|
171
|
|
|
|
|
172
|
|
|
if ($title != $browserTitle) { |
|
173
|
|
|
throw new E5xx_ExpectFailed('BrowserExpects::title', $title, $browserTitle); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// all done |
|
177
|
|
|
$log->endAction(); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function hasTitles($titles) |
|
181
|
|
|
{ |
|
182
|
|
|
// what are we doing? |
|
183
|
|
|
$titlesString = implode('; or ', $titles); |
|
184
|
|
|
$log = usingLog()->startAction("page title must be one of: {$titlesString}"); |
|
185
|
|
|
|
|
186
|
|
|
// get the browser title |
|
187
|
|
|
$browserTitle = fromBrowser()->getTitle(); |
|
188
|
|
|
|
|
189
|
|
|
if (!in_array($browserTitle, $titles)) { |
|
190
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, $titlesString, $browserTitle); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// all done |
|
194
|
|
|
$log->endAction(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public function currentWindowSizeIs($width, $height) |
|
198
|
|
|
{ |
|
199
|
|
|
// what are we doing? |
|
200
|
|
|
$log = usingLog()->startAction("current browser window dimensions must be '{$width}' x '{$height}' (w x h)"); |
|
201
|
|
|
|
|
202
|
|
|
// get the dimensions |
|
203
|
|
|
$dimensions = fromBrowser()->getCurrentWindowSize(); |
|
204
|
|
|
|
|
205
|
|
|
// are they right? |
|
206
|
|
|
if ($dimensions['width'] != $width || $dimensions['height'] != $height) { |
|
207
|
|
|
throw new E5xx_ExpectFailed(__METHOD__, "$width x $height", "{$dimensions['width']} x {$dimensions['height']}"); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// all done |
|
211
|
|
|
$log->endAction(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function __call($methodName, $methodParams) |
|
215
|
|
|
{ |
|
216
|
|
|
return new SingleElementExpect($this->getTopElement(), $methodName, $methodParams); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|