|
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/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 Exception; |
|
47
|
|
|
use Prose\Prose; |
|
48
|
|
|
use DataSift\Storyplayer\PlayerLib\StoryTeller; |
|
49
|
|
|
use DataSift\Storyplayer\PlayerLib\Action_LogItem; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get information from the browser |
|
53
|
|
|
* |
|
54
|
|
|
* @category Libraries |
|
55
|
|
|
* @package Storyplayer/Browser |
|
56
|
|
|
* @author Stuart Herbert <[email protected]> |
|
57
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
58
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
59
|
|
|
* @link http://datasift.github.io/storyplayer |
|
60
|
|
|
*/ |
|
61
|
|
|
class FromBrowser extends Prose |
|
62
|
|
|
{ |
|
63
|
|
|
protected function initActions() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->initDevice(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// ================================================================== |
|
69
|
|
|
// |
|
70
|
|
|
// Data extractors go here |
|
71
|
|
|
// |
|
72
|
|
|
// ------------------------------------------------------------------ |
|
73
|
|
|
|
|
74
|
|
|
public function getTableContents($xpath) |
|
75
|
|
|
{ |
|
76
|
|
|
// what are we doing? |
|
77
|
|
|
$log = usingLog()->startAction("get HTML table using xpath"); |
|
78
|
|
|
|
|
79
|
|
|
// can we find the table? |
|
80
|
|
|
try { |
|
81
|
|
|
$tableElement = fromBrowser()->get()->elementByXpath($xpath); |
|
82
|
|
|
} |
|
83
|
|
|
catch (Exception $e) { |
|
84
|
|
|
// no such table |
|
85
|
|
|
$log->endAction("no matching table"); |
|
86
|
|
|
|
|
87
|
|
|
throw new E5xx_ActionFailed(__METHOD__); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// at this point, it looks like we'll have something to return |
|
91
|
|
|
$return = []; |
|
92
|
|
|
|
|
93
|
|
|
// extract the headings |
|
94
|
|
|
$headings = []; |
|
95
|
|
|
$thElements = $tableElement->getElements('xpath', 'descendant::thead/tr/th'); |
|
96
|
|
|
foreach ($thElements as $thElement) { |
|
97
|
|
|
$headings[] = $thElement->text(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// extract the contents |
|
101
|
|
|
$row = 0; |
|
102
|
|
|
$column = 0; |
|
103
|
|
|
$trElements = $tableElement->getElements('xpath', 'descendant::tbody/tr'); |
|
104
|
|
|
foreach ($trElements as $trElement) { |
|
105
|
|
|
$column = 0; |
|
106
|
|
|
$tdElements = $trElement->getElements('xpath', "descendant::td"); |
|
107
|
|
|
|
|
108
|
|
|
foreach ($tdElements as $tdElement) { |
|
109
|
|
|
if (isset($headings[$column])) { |
|
110
|
|
|
$return[$row][$headings[$column]] = $tdElement->text(); |
|
111
|
|
|
} |
|
112
|
|
|
else { |
|
113
|
|
|
$return[$row][] = $tdElement->text(); |
|
114
|
|
|
} |
|
115
|
|
|
$column++; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$row++; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// all done |
|
122
|
|
|
$log->endAction("found table with $column columns and $row rows"); |
|
123
|
|
|
return $return; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// ================================================================== |
|
127
|
|
|
// |
|
128
|
|
|
// Tests for elements go here |
|
129
|
|
|
// |
|
130
|
|
|
// ------------------------------------------------------------------ |
|
131
|
|
|
|
|
132
|
|
|
public function has() |
|
133
|
|
|
{ |
|
134
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
135
|
|
|
|
|
136
|
|
|
$log = usingLog()->startAction("check the current page for $elementDesc '$elementName'"); |
|
137
|
|
|
if (is_object($element)) { |
|
138
|
|
|
$log->endAction('found it'); |
|
139
|
|
|
return true; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$log->endAction('could not find it'); |
|
143
|
|
|
return false; |
|
144
|
|
|
}; |
|
145
|
|
|
|
|
146
|
|
|
return new SingleElementAction( |
|
147
|
|
|
$action, |
|
148
|
|
|
"has", |
|
149
|
|
|
$this->getTopElement() |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function get() |
|
154
|
|
|
{ |
|
155
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
156
|
|
|
|
|
157
|
|
|
$log = usingLog()->startAction("retrieve the $elementDesc '$elementName'"); |
|
158
|
|
|
$log->endAction(); |
|
159
|
|
|
return $element; |
|
160
|
|
|
}; |
|
161
|
|
|
|
|
162
|
|
|
return new SingleElementAction( |
|
163
|
|
|
$action, |
|
164
|
|
|
"get", |
|
165
|
|
|
$this->getTopElement() |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function getName() |
|
170
|
|
|
{ |
|
171
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
172
|
|
|
|
|
173
|
|
|
$log = usingLog()->startAction("retrieve the name of the $elementDesc '$elementName'"); |
|
174
|
|
|
$log->endAction('name is: ' . $element->attribute('name')); |
|
175
|
|
|
return $element->attribute('name'); |
|
176
|
|
|
}; |
|
177
|
|
|
|
|
178
|
|
|
return new SingleElementAction( |
|
179
|
|
|
$action, |
|
180
|
|
|
"getName", |
|
181
|
|
|
$this->getTopElement() |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
View Code Duplication |
public function getNames() |
|
186
|
|
|
{ |
|
187
|
|
|
$action = function($elements, $elementName, $elementDesc) { |
|
188
|
|
|
|
|
189
|
|
|
$log = usingLog()->startAction("retrieve the names of the $elementDesc '$elementName'"); |
|
190
|
|
|
if (!is_array($elements)) { |
|
191
|
|
|
$log->endAction('1 element found'); |
|
192
|
|
|
return $elements->attribute('name'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$return = array(); |
|
196
|
|
|
foreach ($elements as $element) { |
|
197
|
|
|
$return[] = $element->attribute('name'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$log->endAction(count($return) . ' element(s) found'); |
|
201
|
|
|
return $return; |
|
202
|
|
|
}; |
|
203
|
|
|
|
|
204
|
|
|
return new SingleElementAction( |
|
205
|
|
|
$action, |
|
206
|
|
|
"getNames", |
|
207
|
|
|
$this->getTopElement() |
|
208
|
|
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
View Code Duplication |
public function getOptions() |
|
212
|
|
|
{ |
|
213
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
214
|
|
|
|
|
215
|
|
|
$log = usingLog()->startAction("retrieve the options of them $elementDesc '$elementName'"); |
|
216
|
|
|
// get the elements |
|
217
|
|
|
$optionElements = $element->getElements('xpath', "descendant::option"); |
|
218
|
|
|
|
|
219
|
|
|
// extract their values |
|
220
|
|
|
$return = array(); |
|
221
|
|
|
foreach ($optionElements as $optionElement) { |
|
222
|
|
|
$return[] = $optionElement->text(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// all done |
|
226
|
|
|
$log->endAction(count($return) . ' option(s) found'); |
|
227
|
|
|
return $return; |
|
228
|
|
|
}; |
|
229
|
|
|
|
|
230
|
|
|
return new SingleElementAction( |
|
231
|
|
|
$action, |
|
232
|
|
|
'getOptions', |
|
233
|
|
|
$this->getTopElement() |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
View Code Duplication |
public function getTag() |
|
238
|
|
|
{ |
|
239
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
240
|
|
|
$log = usingLog()->startAction("retrieve the tagname of the $elementDesc '$elementName'"); |
|
241
|
|
|
$log->endAction("tag is: " . $element->name()); |
|
242
|
|
|
return $element->name(); |
|
243
|
|
|
}; |
|
244
|
|
|
|
|
245
|
|
|
return new SingleElementAction( |
|
246
|
|
|
$action, |
|
247
|
|
|
"getTag", |
|
248
|
|
|
$this->getTopElement() |
|
249
|
|
|
); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
View Code Duplication |
public function getText() |
|
253
|
|
|
{ |
|
254
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
255
|
|
|
$log = usingLog()->startAction("retrieve the text of the $elementDesc '$elementName'"); |
|
256
|
|
|
$log->endAction("text is: " . $element->text()); |
|
257
|
|
|
return $element->text(); |
|
258
|
|
|
}; |
|
259
|
|
|
|
|
260
|
|
|
return new SingleElementAction( |
|
261
|
|
|
$action, |
|
262
|
|
|
"getText", |
|
263
|
|
|
$this->getTopElement() |
|
264
|
|
|
); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function getValue() |
|
268
|
|
|
{ |
|
269
|
|
|
$action = function($element, $elementName, $elementDesc) { |
|
270
|
|
|
$log = usingLog()->startAction("retrieve the value of the $elementDesc '$elementName'"); |
|
271
|
|
|
|
|
272
|
|
|
// is this a select box? |
|
273
|
|
|
switch($element->name()) { |
|
274
|
|
|
case 'select': |
|
|
|
|
|
|
275
|
|
|
// get the option that is selected |
|
276
|
|
|
try { |
|
277
|
|
|
$option = $element->getElement('xpath', 'option[@selected]'); |
|
278
|
|
|
$log->endAction("value is: " . $option->text()); |
|
279
|
|
|
return $option->text(); |
|
280
|
|
|
} |
|
281
|
|
|
catch (Exception $e) { |
|
282
|
|
|
// return the top option from the list |
|
283
|
|
|
$option = $element->getElement('xpath', 'option[1]'); |
|
284
|
|
|
$log->endAction("value is: " . $option->text()); |
|
285
|
|
|
return $option->text(); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
case 'input': |
|
289
|
|
|
$log->endAction("value is: " . $element->attribute("value")); |
|
290
|
|
|
return $element->attribute("value"); |
|
291
|
|
|
|
|
292
|
|
|
default: |
|
293
|
|
|
$log->endAction("value is: " . $element->text()); |
|
294
|
|
|
return $element->text(); |
|
295
|
|
|
} |
|
296
|
|
|
}; |
|
297
|
|
|
|
|
298
|
|
|
return new SingleElementAction( |
|
299
|
|
|
$action, |
|
300
|
|
|
"getValue", |
|
301
|
|
|
$this->getTopElement() |
|
302
|
|
|
); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
// ================================================================== |
|
306
|
|
|
// |
|
307
|
|
|
// Retrievers of navigation metadata |
|
308
|
|
|
// |
|
309
|
|
|
// ------------------------------------------------------------------ |
|
310
|
|
|
|
|
311
|
|
|
public function getUrl() |
|
|
|
|
|
|
312
|
|
|
{ |
|
313
|
|
|
// shorthand |
|
314
|
|
|
$browser = $this->device; |
|
315
|
|
|
|
|
316
|
|
|
// what are we doing? |
|
317
|
|
|
$log = usingLog()->startAction("retrieve the current URL from the browser"); |
|
318
|
|
|
|
|
319
|
|
|
// get the URL |
|
320
|
|
|
$url = $browser->url(); |
|
321
|
|
|
|
|
322
|
|
|
// all done |
|
323
|
|
|
$log->endAction($url); |
|
324
|
|
|
|
|
325
|
|
|
return $url; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
// ================================================================== |
|
329
|
|
|
// |
|
330
|
|
|
// Authentication actions go here |
|
331
|
|
|
// |
|
332
|
|
|
// ------------------------------------------------------------------ |
|
333
|
|
|
|
|
334
|
|
|
public function getHttpBasicAuthForHost($hostname) |
|
335
|
|
|
{ |
|
336
|
|
|
// this method deliberately has no logging, because it is called |
|
337
|
|
|
// every single time that we want the browser to go to a new URL |
|
338
|
|
|
// |
|
339
|
|
|
// nothing else should really use this |
|
340
|
|
|
|
|
341
|
|
|
try { |
|
342
|
|
|
// get the browser adapter |
|
343
|
|
|
$adapter = $this->st->getDeviceAdapter(); |
|
344
|
|
|
|
|
345
|
|
|
// get the details |
|
346
|
|
|
$credentials = $adapter->getHttpBasicAuthForHost($hostname); |
|
347
|
|
|
|
|
348
|
|
|
// all done |
|
349
|
|
|
return $credentials; |
|
350
|
|
|
} |
|
351
|
|
|
catch (Exception $e) |
|
352
|
|
|
{ |
|
353
|
|
|
return null; |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
public function hasHttpBasicAuthForHost($hostname) |
|
358
|
|
|
{ |
|
359
|
|
|
// this method deliberately has no logging, because it is called |
|
360
|
|
|
// every single time that we want the browser to go to a new URL |
|
361
|
|
|
// |
|
362
|
|
|
// nothing else should really use this |
|
363
|
|
|
|
|
364
|
|
|
try { |
|
365
|
|
|
// get the browser adapter |
|
366
|
|
|
$adapter = $this->st->getDeviceAdapter(); |
|
367
|
|
|
|
|
368
|
|
|
// get the details |
|
369
|
|
|
return $adapter->hasHttpBasicAuthForHost($hostname); |
|
370
|
|
|
} |
|
371
|
|
|
catch (Exception $e) |
|
372
|
|
|
{ |
|
373
|
|
|
return false; |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
// ================================================================== |
|
378
|
|
|
// |
|
379
|
|
|
// Retrievers of page metadata |
|
380
|
|
|
// |
|
381
|
|
|
// ------------------------------------------------------------------ |
|
382
|
|
|
|
|
383
|
|
View Code Duplication |
public function getTitle() |
|
|
|
|
|
|
384
|
|
|
{ |
|
385
|
|
|
// some shorthand to make things easier to read |
|
386
|
|
|
$browser = $this->device; |
|
387
|
|
|
|
|
388
|
|
|
$log = usingLog()->startAction("retrieve the current page title"); |
|
389
|
|
|
$log->endAction("title is: " . $browser->title()); |
|
390
|
|
|
|
|
391
|
|
|
return $browser->title(); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
// ================================================================== |
|
395
|
|
|
// |
|
396
|
|
|
// Retrievers of browser metadata |
|
397
|
|
|
// |
|
398
|
|
|
// ------------------------------------------------------------------ |
|
399
|
|
|
|
|
400
|
|
|
public function getCurrentWindowSize() |
|
401
|
|
|
{ |
|
402
|
|
|
// shorthand |
|
403
|
|
|
$browser = $this->device; |
|
404
|
|
|
|
|
405
|
|
|
// what are we doing? |
|
406
|
|
|
$log = usingLog()->startAction("retrieve the current browser window's dimensions"); |
|
407
|
|
|
|
|
408
|
|
|
// get the dimensions |
|
409
|
|
|
$dimensions = $browser->window()->getSize(); |
|
410
|
|
|
|
|
411
|
|
|
// all done |
|
412
|
|
|
$log->endAction("width: '{$dimensions['width']}'; height: '{$dimensions['height']}'"); |
|
413
|
|
|
return array('width' => $dimensions['width'], 'height' => $dimensions['height']); |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
|