Skip to content

Respect base element with fillField() #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,7 @@ protected function findFields($selector): array
}

if (is_array($selector) || ($selector instanceof WebDriverBy)) {
$fields = $this->match($this->webDriver, $selector);
$fields = $this->match($this->getBaseElement(), $selector);

if (empty($fields)) {
throw new ElementNotFound($selector);
Expand All @@ -1272,20 +1272,20 @@ protected function findFields($selector): array
".//label[contains(normalize-space(string(.)), {$locator})]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]"
// @codingStandardsIgnoreEnd
);
$fields = $this->webDriver->findElements(WebDriverBy::xpath($xpath));
$fields = $this->getBaseElement()->findElements(WebDriverBy::xpath($xpath));
if (!empty($fields)) {
return $fields;
}

// by name
$xpath = ".//*[self::input | self::textarea | self::select][@name = {$locator}]";
$fields = $this->webDriver->findElements(WebDriverBy::xpath($xpath));
$fields = $this->getBaseElement()->findElements(WebDriverBy::xpath($xpath));
if (!empty($fields)) {
return $fields;
}

// try to match by CSS or XPath
$fields = $this->match($this->webDriver, $selector, false);
$fields = $this->match($this->getBaseElement(), $selector, false);
if (!empty($fields)) {
return $fields;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/data/app/view/form/bug79.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head><title></title></head>
<body>
<form>
<label for="title-1">Title</label>
<input id="title-1">

<div class="nested">
<label for="title-2">Title</label>
<input id="title-2">
</div>
</form>
</body>
</html>
21 changes: 21 additions & 0 deletions tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Codeception\Module\WebDriver;
use Codeception\Test\Unit;
use Codeception\Util\ActionSequence;

/**
* Author: davert
Expand Down Expand Up @@ -1745,4 +1746,24 @@ public function testPasswordArgument()
$data = data::get('form');
$this->assertEquals('thisissecret', $data['password']);
}

/**
* @see https://github.com/Codeception/module-webdriver/issues/79
*/
public function testFieldActionsWithPerformOn()
{
$this->module->amOnPage('/form/bug79');
$this->module->performOn(
['class' => 'nested'],
ActionSequence::build()->fillField('Title', 'Test')
);

$this->module->dontSeeInField('#title-1', 'Test');
$this->module->seeInField('#title-2', 'Test');

$title1Value = $this->module->grabValueFrom('#title-1');
$this->assertEmpty($title1Value);
$title2Value = $this->module->grabValueFrom('#title-2');
$this->assertEquals('Test', $title2Value);
}
}