Skip to content

Commit

Permalink
Allow Browser::checkField() to work with radios (#48)
Browse files Browse the repository at this point in the history
* [bug] allow Browser::checkField() to work with radios

* [feature] add Browser::selectField() as an alias for:

- checkField
- selectFieldOption
- selectFieldOptions
  • Loading branch information
kbond authored Aug 4, 2021
1 parent 2b63095 commit 50798f7
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ $browser
->fillField('Name', 'Kevin')
->checkField('Accept Terms')
->uncheckField('Accept Terms')
->selectFieldOption('Type', 'Employee') // single option select
->selectFieldOptions('Notification', ['Email', 'SMS']) // multi-option select
->selectField('Canada') // "radio" select
->selectField('Type', 'Employee') // "select" single option
->selectField('Notification', ['Email', 'SMS']) // "select" multiple options
->attachFile('Photo', '/path/to/photo.jpg')
->click('Submit')

Expand Down
30 changes: 30 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ final public function fillField(string $selector, string $value): self
*/
final public function checkField(string $selector): self
{
$field = $this->documentElement()->findField($selector);

if ($field && 'radio' === \mb_strtolower($field->getAttribute('type'))) {
$this->documentElement()->selectFieldOption($selector, $field->getAttribute('value'));

return $this;
}

$this->documentElement()->checkField($selector);

return $this;
Expand All @@ -189,6 +197,28 @@ final public function uncheckField(string $selector): self
return $this;
}

/**
* Select Radio, check checkbox, select single/multiple values.
*
* @param string|array|null $value null: check radio/checkbox
* string: single value
* array: multiple values
*
* @return static
*/
final public function selectField(string $selector, $value = null): self
{
if (\is_array($value)) {
return $this->selectFieldOptions($selector, $value);
}

if (\is_string($value)) {
return $this->selectFieldOption($selector, $value);
}

return $this->checkField($selector);
}

/**
* @return static
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Browser/Mink/PantherDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public function attachFile($xpath, $path): void

public function isChecked($xpath): bool
{
$element = $this->crawlerElement($this->filteredCrawler($xpath));

if ('radio' === \mb_strtolower($element->getAttribute('type'))) {
return null !== $element->getAttribute('checked');
}

return $this->choiceFormField($xpath)->hasValue();
}

Expand Down
34 changes: 34 additions & 0 deletions tests/BrowserTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ public function form_assertions(): void
->assertNotSelected('Input 7', 'option 2')
->assertNotSelected('input7', 'option 2')
->assertNotSelected('input_7[]', 'option 2')
->assertNotSelected('input_8', 'option 1')
->assertSelected('input_8', 'option 2')
->assertNotChecked('Radio 1')
->assertNotChecked('radio1')
->assertNotChecked('Radio 3')
->assertNotChecked('radio3')
->assertChecked('Radio 2')
->assertChecked('radio2')
;
}

Expand Down Expand Up @@ -327,6 +335,7 @@ public function form_actions_by_field_label(): void
->selectFieldOption('Input 4', 'option 2')
->attachFile('Input 5', __FILE__)
->selectFieldOptions('Input 6', ['option 1', 'option 3'])
->checkField('Radio 3')
->click('Submit')
->assertOn('/submit-form')
->assertContains('"input_1":"Kevin"')
Expand All @@ -335,6 +344,7 @@ public function form_actions_by_field_label(): void
->assertContains('"input_4":"option 2"')
->assertContains(\sprintf('"input_5":"%s"', \pathinfo(__FILE__, \PATHINFO_BASENAME)))
->assertContains('"input_6":["option 1","option 3"]')
->assertContains('"input_8":"option 3"')
;
}

Expand All @@ -351,6 +361,7 @@ public function form_actions_by_field_id(): void
->selectFieldOption('input4', 'option 2')
->attachFile('input5', __FILE__)
->selectFieldOptions('input6', ['option 1', 'option 3'])
->checkField('radio3')
->click('Submit')
->assertOn('/submit-form')
->assertContains('"input_1":"Kevin"')
Expand All @@ -359,6 +370,7 @@ public function form_actions_by_field_id(): void
->assertContains('"input_4":"option 2"')
->assertContains(\sprintf('"input_5":"%s"', \pathinfo(__FILE__, \PATHINFO_BASENAME)))
->assertContains('"input_6":["option 1","option 3"]')
->assertContains('"input_8":"option 3"')
;
}

Expand All @@ -375,6 +387,7 @@ public function form_actions_by_field_name(): void
->selectFieldOption('input_4', 'option 2')
->attachFile('input_5', __FILE__)
->selectFieldOptions('input_6[]', ['option 1', 'option 3'])
->selectFieldOption('input_8', 'option 3')
->click('Submit')
->assertOn('/submit-form')
->assertContains('"input_1":"Kevin"')
Expand All @@ -383,6 +396,27 @@ public function form_actions_by_field_name(): void
->assertContains('"input_4":"option 2"')
->assertContains(\sprintf('"input_5":"%s"', \pathinfo(__FILE__, \PATHINFO_BASENAME)))
->assertContains('"input_6":["option 1","option 3"]')
->assertContains('"input_8":"option 3"')
;
}

/**
* @test
*/
public function select_field(): void
{
$this->browser()
->visit('/page1')
->selectField('Input 2')
->selectField('Input 4', 'option 2')
->selectField('Input 6', ['option 1', 'option 3'])
->selectField('Radio 3')
->click('Submit')
->assertOn('/submit-form')
->assertContains('"input_2":"on"')
->assertContains('"input_4":"option 2"')
->assertContains('"input_6":["option 1","option 3"]')
->assertContains('"input_8":"option 3"')
;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function submitForm(Request $request): JsonResponse
{
return new JsonResponse(\array_merge(
$request->request->all(),
\array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $request->files->all())
\array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), \array_filter($request->files->all()))
));
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Fixture/files/page1.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ <h1>h1 title</h1>
<option selected>option 3</option>
</select>

<label for="radio1">Radio 1</label>
<input type="radio" id="radio1" name="input_8" value="option 1">
<label for="radio2">Radio 2</label>
<input type="radio" id="radio2" name="input_8" value="option 2" checked>
<label for="radio3">Radio 3</label>
<input type="radio" id="radio3" name="input_8" value="option 3">

<button>Submit</button>
</form>
</body>
Expand Down

0 comments on commit 50798f7

Please sign in to comment.