Skip to content

Commit 10482f7

Browse files
authored
Merge pull request #60 from Codeception/fix-checkbox-handling
fix: checkbox handling
2 parents 721fbb8 + 72b98ef commit 10482f7

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/Codeception/Lib/InnerBrowser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,13 @@ public function grabFromCurrentUrl(string $uri = null): mixed
655655
public function seeCheckboxIsChecked($checkbox): void
656656
{
657657
$checkboxes = $this->getFieldsByLabelOrCss($checkbox);
658-
$this->assertDomContains($checkboxes->filter('input[checked=checked]'), 'checkbox');
658+
$this->assertGreaterThan(0, $checkboxes->filter('input[checked]')->count());
659659
}
660660

661661
public function dontSeeCheckboxIsChecked($checkbox): void
662662
{
663663
$checkboxes = $this->getFieldsByLabelOrCss($checkbox);
664-
$this->assertSame(0, $checkboxes->filter('input[checked=checked]')->count());
664+
$this->assertSame(0, $checkboxes->filter('input[checked]')->count());
665665
}
666666

667667
public function seeInField($field, $value): void
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<input type="checkbox" id="checkedbox1" checked="random"/>
4+
<input type="checkbox" id="checkedbox2" checked/>
5+
</body>
6+
</html>

tests/unit/Codeception/Module/TestsForWeb.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Codeception\Lib\Framework;
88
use Codeception\Test\Unit;
99
use PHPUnit\Framework\AssertionFailedError;
10+
use PHPUnit\Framework\ExpectationFailedException;
1011

1112
/**
1213
* Author: davert
@@ -480,22 +481,36 @@ public function testFileFieldByLabel()
480481
$this->assertNotEmpty(data::get('files'));
481482
}
482483

483-
public function testSeeCheckboxIsNotChecked()
484-
{
485-
$this->module->amOnPage('/form/checkbox');
486-
$this->module->dontSeeCheckboxIsChecked('#checkin');
487-
$this->module->dontSeeCheckboxIsChecked(['css' => '#checkin']);
488-
$this->module->dontSeeCheckboxIsChecked('I Agree');
484+
public function checkBoxes(): iterable {
485+
yield ['/form/checkbox_checked', ['css' => "#checkedbox1"], true];
486+
yield ['/form/checkbox_checked', ['css' => "#checkedbox2"], true];
487+
yield ['/form/checkbox', '#checkin', false];
488+
yield ['/form/checkbox', ['css' => '#checkin'], false];
489+
yield ['/form/checkbox', 'I Agree', false];
490+
yield ['/info', 'input[type=checkbox]', true];
491+
yield ['/info', ['css' => 'input[type=checkbox]'], true];
492+
yield ['/info', 'Checked', true];
489493
}
490494

491-
public function testSeeCheckboxChecked()
495+
#[\Codeception\Attribute\DataProvider('checkBoxes')]
496+
public function testSeeCheckboxIsNotChecked(string $page, string|array $selector, bool $checked): void
492497
{
493-
$this->module->amOnPage('/info');
494-
$this->module->seeCheckboxIsChecked('input[type=checkbox]');
495-
$this->module->seeCheckboxIsChecked(['css' => 'input[type=checkbox]']);
496-
$this->module->seeCheckboxIsChecked('Checked');
498+
$this->module->amOnPage($page);
499+
if ($checked) {
500+
$this->expectException(ExpectationFailedException::class);
501+
}
502+
$this->module->dontSeeCheckboxIsChecked($selector);
497503
}
498504

505+
#[\Codeception\Attribute\DataProvider('checkBoxes')]
506+
public function testSeeCheckboxIsChecked(string $page, string|array $selector, bool $checked): void
507+
{
508+
$this->module->amOnPage($page);
509+
if (!$checked) {
510+
$this->expectException(ExpectationFailedException::class);
511+
}
512+
$this->module->seeCheckboxIsChecked($selector);
513+
}
499514
public function testSeeWithNonLatin()
500515
{
501516
$this->module->amOnPage('/info');

0 commit comments

Comments
 (0)