Skip to content

Commit

Permalink
Merge pull request #91 from olexp/preg-match-typeerror
Browse files Browse the repository at this point in the history
Avoid TypeError in preg_match()
  • Loading branch information
Naktibalda authored Jan 23, 2022
2 parents 2474cc4 + 809f741 commit 57e2d83
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,10 @@ public function grabTextFrom($cssOrXPathOrRegex)
return $els[0]->getText();
}

if (@preg_match($cssOrXPathOrRegex, $this->webDriver->getPageSource(), $matches)) {
if (
is_string($cssOrXPathOrRegex)
&& @preg_match($cssOrXPathOrRegex, $this->webDriver->getPageSource(), $matches)
) {
return $matches[1];
}

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Codeception/Module/TestsForWeb.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,19 @@ public function testGrabTextFrom()
$this->assertSame('test', $result);
}

public function testGrabTextFromWithArraySelectorForExistingElement()
{
$this->module->amOnPage('/');
$this->assertSame('More info', $this->module->grabTextFrom(['id' => 'link']));
}

public function testGrabTextFromWithArraySelectorForNonExistentElement()
{
$this->module->amOnPage('/');
$this->expectException('Codeception\Exception\ElementNotFound');
$this->module->grabTextFrom(['id' => 'unknown_link']);
}

public function testGrabValueFrom()
{
$this->module->amOnPage('/form/hidden');
Expand Down

0 comments on commit 57e2d83

Please sign in to comment.