|
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 Exception; |
|
47
|
|
|
use Storyplayer\SPv2\Modules\Browser; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* do things to forms in the web 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 UsingForm extends UsingBrowser |
|
60
|
|
|
{ |
|
61
|
|
|
protected $formId = null; |
|
62
|
|
|
protected $formElement = null; |
|
63
|
|
|
|
|
64
|
|
|
protected function initActions() |
|
65
|
|
|
{ |
|
66
|
|
|
// call our parent initActions() first |
|
67
|
|
|
parent::initActions(); |
|
68
|
|
|
|
|
69
|
|
|
// shorthand |
|
70
|
|
|
$formId = $this->args[0]; |
|
71
|
|
|
|
|
72
|
|
|
// find the form |
|
73
|
|
|
$formElement = Browser::fromBrowser()->get()->elementById($formId); |
|
74
|
|
|
|
|
75
|
|
|
// is it really a form? |
|
76
|
|
|
if (strtolower($formElement->name()) !== 'form') { |
|
77
|
|
|
throw new E5xx_ActionFailed(__METHOD__, "expected form element, got element '" . $formElement->name() . "'"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// yes, it really is a form |
|
81
|
|
|
$this->formId = $formId; |
|
82
|
|
|
$this->setTopElement($formElement); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// ================================================================== |
|
86
|
|
|
// |
|
87
|
|
|
// Mass form-filling goes here |
|
88
|
|
|
// |
|
89
|
|
|
// ------------------------------------------------------------------ |
|
90
|
|
|
|
|
91
|
|
|
public function clearFields($fields) |
|
92
|
|
|
{ |
|
93
|
|
|
// what are we doing? |
|
94
|
|
|
$log = usingLog()->startAction("clear " . count($fields) . " field(s) in the form '{$this->formId}'"); |
|
95
|
|
|
|
|
96
|
|
|
foreach ($fields as $labelText => $fieldValue) { |
|
97
|
|
|
Browser::usingForm($this->formId)->clear()->theFieldLabelled($labelText); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$log->endAction(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function fillInFields($fields) |
|
104
|
|
|
{ |
|
105
|
|
|
// shorthand |
|
106
|
|
|
$formId = $this->formId; |
|
107
|
|
|
|
|
108
|
|
|
// what are we doing? |
|
109
|
|
|
$log = usingLog()->startAction("fill in " . count($fields) . " field(s) in the form '{$this->formId}'"); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($fields as $labelText => $fieldValue) { |
|
112
|
|
|
// find the element |
|
113
|
|
|
$element = Browser::fromForm($formId)->get()->elementByLabelIdOrName($labelText); |
|
114
|
|
|
$tag = $element->name(); |
|
115
|
|
|
|
|
116
|
|
|
switch ($tag) { |
|
117
|
|
|
case 'input': |
|
118
|
|
|
case 'textarea': |
|
119
|
|
|
$this->type($fieldValue)->intoElement($element); |
|
120
|
|
|
break; |
|
121
|
|
|
|
|
122
|
|
|
case 'select': |
|
123
|
|
|
$this->select($fieldValue)->fromElement($element); |
|
124
|
|
|
break; |
|
125
|
|
|
|
|
126
|
|
|
case null: |
|
127
|
|
|
$log->endAction("cannot find field labelled '{$labelText}'"); |
|
128
|
|
|
throw new E5xx_ActionFailed(__METHOD__); |
|
129
|
|
|
|
|
130
|
|
|
default: |
|
131
|
|
|
$log->endAction("* field labelled '{$labelText}' has unsupported tag '{$tag}' *"); |
|
132
|
|
|
throw new E5xx_ActionFailed(__METHOD__); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// all done |
|
137
|
|
|
$log->endAction(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function fillInFieldsIfPresent($fields) |
|
141
|
|
|
{ |
|
142
|
|
|
// shorthand |
|
143
|
|
|
$formId = $this->formId; |
|
144
|
|
|
|
|
145
|
|
|
// what are we doing? |
|
146
|
|
|
$log = usingLog()->startAction("fill in " . count($fields) . " field(s) in form '{$formId}' if present"); |
|
147
|
|
|
|
|
148
|
|
|
foreach ($fields as $labelText => $fieldValue) { |
|
149
|
|
|
// find the element |
|
150
|
|
|
$element = $log->addStep("finding field with label, id or name '{$labelText}'", function($log) use($formId, $labelText) { |
|
151
|
|
|
try { |
|
152
|
|
|
return Browser::fromForm($formId)->get()->elementByLabelIdOrName($labelText); |
|
153
|
|
|
} |
|
154
|
|
|
catch (Exception $e) { |
|
155
|
|
|
$log->endAction("field '{$labelText}' not present; ignoring!"); |
|
156
|
|
|
return null; |
|
157
|
|
|
} |
|
158
|
|
|
}); |
|
159
|
|
|
|
|
160
|
|
|
// did we get one? |
|
161
|
|
|
if ($element == null) { |
|
162
|
|
|
// missing field |
|
163
|
|
|
continue; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$tag = strtolower($element->name()); |
|
167
|
|
|
switch ($tag) { |
|
168
|
|
|
case 'input': |
|
169
|
|
|
case 'textarea': |
|
170
|
|
|
$this->type($fieldValue)->intoElement($element); |
|
171
|
|
|
break; |
|
172
|
|
|
|
|
173
|
|
|
case 'select': |
|
174
|
|
|
$this->select($fieldValue)->fromElement($element); |
|
175
|
|
|
break; |
|
176
|
|
|
|
|
177
|
|
|
default: |
|
178
|
|
|
$log->endAction("* field '{$labelText}' has unexpected tag '{$tag}' *"); |
|
179
|
|
|
throw new E5xx_ActionFailed(__METHOD__); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// all done |
|
184
|
|
|
$log->endAction(); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|