Skip to content

Commit 3dfc247

Browse files
imjoehainesDavertMik
authored andcommitted
Add support for different selectors in submitForm (Codeception#3560)
Buttons can now be found with strict selectors or WebDriverBy instances
1 parent 28068d6 commit 3dfc247

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

src/Codeception/Module/WebDriver.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,24 @@ protected function getSubmissionFormFieldName($name)
17421742
* ]
17431743
* ]);
17441744
* ```
1745+
*
1746+
* The `$button` parameter can be either a string, an array or an instance
1747+
* of Facebook\WebDriver\WebDriverBy. When it is a string, the
1748+
* button will be found by its "name" attribute. If $button is an
1749+
* array then it will be treated as a strict selector and a WebDriverBy
1750+
* will be used verbatim.
1751+
*
1752+
* For example, given the following HTML:
1753+
*
1754+
* ``` html
1755+
* <input type="submit" name="submitButton" value="Submit" />
1756+
* ```
1757+
*
1758+
* `$button` could be any one of the following:
1759+
* - 'submitButton'
1760+
* - ['name' => 'submitButton']
1761+
* - WebDriverBy::name('submitButton')
1762+
*
17451763
* @param $selector
17461764
* @param $params
17471765
* @param $button
@@ -1806,7 +1824,16 @@ public function submitForm($selector, array $params, $button = null)
18061824

18071825
$submitted = false;
18081826
if (!empty($button)) {
1809-
$els = $form->findElements(WebDriverBy::name($button));
1827+
if (is_array($button)) {
1828+
$buttonSelector = $this->getStrictLocator($button);
1829+
} elseif ($button instanceof WebDriverBy) {
1830+
$buttonSelector = $button;
1831+
} else {
1832+
$buttonSelector = WebDriverBy::name($button);
1833+
}
1834+
1835+
$els = $form->findElements($buttonSelector);
1836+
18101837
if (!empty($els)) {
18111838
$el = reset($els);
18121839
$el->click();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<html>
2+
<body>
3+
<form action="/form/complex" method="POST">
4+
5+
<input type="hidden" name="action" value="kill_all" />
6+
<fieldset disabled="disabled">
7+
<input type="text" id="disabled_fieldset" name="disabled_fieldset" value="disabled_fieldset" />
8+
</fieldset>
9+
<input type="text" id="disabled_field" name="disabled_field" value="disabled_field" disabled="disabled" />
10+
<label for="description">Description</label>
11+
<textarea name="description" id="description" cols="30" rows="10"></textarea>
12+
13+
<label for="name">Name</label>
14+
<input type="text" id="name" name="name" value="" />
15+
16+
<label for="age">Select your age</label>
17+
<select name="age" id="age">
18+
<option value="child">below 13</option>
19+
<option value="teenage">13-21</option>
20+
<option value="adult">21-60</option>
21+
<option value="oldfag">60-100</option>
22+
<option value="dead">100-210</option>
23+
</select>
24+
25+
<select name="no_salutation" id="salutation" disabled="disabled" id="age">
26+
<option value="mr" selected="selected">Mr</option>
27+
<option value="ms">Mrs</option>
28+
</select>
29+
30+
31+
<input type="password" name="password" >
32+
<label for="checkin">I Agree</label>
33+
<input type="checkbox" id="checkin" name="terms" value="agree" checked="checked" />
34+
<input type="submit" value="Submit" id="submit_button" name="submit_button_name" class="button" />
35+
36+
<?php print_r($_SERVER); ?>
37+
</form>
38+
</body>
39+
</html>

tests/web/WebDriverTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,74 @@ public function testSubmitFormWithNumbers()
134134
$this->assertEquals('child', $form['age']);
135135
}
136136

137+
/**
138+
* @dataProvider strictSelectorProvider
139+
*/
140+
public function testSubmitFormWithButtonAsStrictSelector(array $selector)
141+
{
142+
$this->module->amOnPage('/form/strict_selectors');
143+
$this->module->submitForm('form', [
144+
'name' => 'Davert',
145+
'age' => 'child',
146+
'terms' => 'agree',
147+
'description' => 'My Bio'
148+
], $selector);
149+
150+
$form = data::get('form');
151+
152+
$this->assertEquals('Davert', $form['name']);
153+
$this->assertEquals('kill_all', $form['action']);
154+
$this->assertEquals('My Bio', $form['description']);
155+
$this->assertEquals('agree', $form['terms']);
156+
$this->assertEquals('child', $form['age']);
157+
}
158+
159+
public function strictSelectorProvider()
160+
{
161+
return [
162+
'by id' => [['id' => 'submit_button']],
163+
'by name' => [['name' => 'submit_button_name']],
164+
'by css' => [['css' => 'form #submit_button']],
165+
'by xpath' => [['xpath' => '//*[@id="submit_button"]']],
166+
'by link' => [['link' => 'Submit']],
167+
'by class' => [['class' => 'button']],
168+
];
169+
}
170+
171+
/**
172+
* @dataProvider webDriverByProvider
173+
*/
174+
public function testSubmitFormWithButtonAsWebDriverBy(WebDriverBy $selector)
175+
{
176+
$this->module->amOnPage('/form/strict_selectors');
177+
$this->module->submitForm('form', [
178+
'name' => 'Davert',
179+
'age' => 'child',
180+
'terms' => 'agree',
181+
'description' => 'My Bio'
182+
], $selector);
183+
184+
$form = data::get('form');
185+
186+
$this->assertEquals('Davert', $form['name']);
187+
$this->assertEquals('kill_all', $form['action']);
188+
$this->assertEquals('My Bio', $form['description']);
189+
$this->assertEquals('agree', $form['terms']);
190+
$this->assertEquals('child', $form['age']);
191+
}
192+
193+
public function webDriverByProvider()
194+
{
195+
return [
196+
'by id' => [WebDriverBy::id('submit_button')],
197+
'by name' => [WebDriverBy::name('submit_button_name')],
198+
'by css selector' => [WebDriverBy::cssSelector('form #submit_button')],
199+
'by xpath' => [WebDriverBy::xpath('//*[@id="submit_button"]')],
200+
'by link text' => [WebDriverBy::linkText('Submit')],
201+
'by class name' => [WebDriverBy::className('button')],
202+
];
203+
}
204+
137205
public function testRadioButtonByValue()
138206
{
139207
$this->module->amOnPage('/form/radio');

0 commit comments

Comments
 (0)