Skip to content

Commit

Permalink
Merge pull request #60 from Codeception/fix-checkbox-handling
Browse files Browse the repository at this point in the history
fix: checkbox handling
  • Loading branch information
Naktibalda authored Oct 3, 2022
2 parents 721fbb8 + 72b98ef commit 10482f7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Codeception/Lib/InnerBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,13 @@ public function grabFromCurrentUrl(string $uri = null): mixed
public function seeCheckboxIsChecked($checkbox): void
{
$checkboxes = $this->getFieldsByLabelOrCss($checkbox);
$this->assertDomContains($checkboxes->filter('input[checked=checked]'), 'checkbox');
$this->assertGreaterThan(0, $checkboxes->filter('input[checked]')->count());
}

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

public function seeInField($field, $value): void
Expand Down
6 changes: 6 additions & 0 deletions tests/data/app/view/form/checkbox_checked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<input type="checkbox" id="checkedbox1" checked="random"/>
<input type="checkbox" id="checkedbox2" checked/>
</body>
</html>
37 changes: 26 additions & 11 deletions tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Codeception\Lib\Framework;
use Codeception\Test\Unit;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ExpectationFailedException;

/**
* Author: davert
Expand Down Expand Up @@ -480,22 +481,36 @@ public function testFileFieldByLabel()
$this->assertNotEmpty(data::get('files'));
}

public function testSeeCheckboxIsNotChecked()
{
$this->module->amOnPage('/form/checkbox');
$this->module->dontSeeCheckboxIsChecked('#checkin');
$this->module->dontSeeCheckboxIsChecked(['css' => '#checkin']);
$this->module->dontSeeCheckboxIsChecked('I Agree');
public function checkBoxes(): iterable {
yield ['/form/checkbox_checked', ['css' => "#checkedbox1"], true];
yield ['/form/checkbox_checked', ['css' => "#checkedbox2"], true];
yield ['/form/checkbox', '#checkin', false];
yield ['/form/checkbox', ['css' => '#checkin'], false];
yield ['/form/checkbox', 'I Agree', false];
yield ['/info', 'input[type=checkbox]', true];
yield ['/info', ['css' => 'input[type=checkbox]'], true];
yield ['/info', 'Checked', true];
}

public function testSeeCheckboxChecked()
#[\Codeception\Attribute\DataProvider('checkBoxes')]
public function testSeeCheckboxIsNotChecked(string $page, string|array $selector, bool $checked): void
{
$this->module->amOnPage('/info');
$this->module->seeCheckboxIsChecked('input[type=checkbox]');
$this->module->seeCheckboxIsChecked(['css' => 'input[type=checkbox]']);
$this->module->seeCheckboxIsChecked('Checked');
$this->module->amOnPage($page);
if ($checked) {
$this->expectException(ExpectationFailedException::class);
}
$this->module->dontSeeCheckboxIsChecked($selector);
}

#[\Codeception\Attribute\DataProvider('checkBoxes')]
public function testSeeCheckboxIsChecked(string $page, string|array $selector, bool $checked): void
{
$this->module->amOnPage($page);
if (!$checked) {
$this->expectException(ExpectationFailedException::class);
}
$this->module->seeCheckboxIsChecked($selector);
}
public function testSeeWithNonLatin()
{
$this->module->amOnPage('/info');
Expand Down

0 comments on commit 10482f7

Please sign in to comment.