Skip to content

Commit 57e2d83

Browse files
authored
Merge pull request #91 from olexp/preg-match-typeerror
Avoid TypeError in preg_match()
2 parents 2474cc4 + 809f741 commit 57e2d83

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Codeception/Module/WebDriver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1971,7 +1971,10 @@ public function grabTextFrom($cssOrXPathOrRegex)
19711971
return $els[0]->getText();
19721972
}
19731973

1974-
if (@preg_match($cssOrXPathOrRegex, $this->webDriver->getPageSource(), $matches)) {
1974+
if (
1975+
is_string($cssOrXPathOrRegex)
1976+
&& @preg_match($cssOrXPathOrRegex, $this->webDriver->getPageSource(), $matches)
1977+
) {
19751978
return $matches[1];
19761979
}
19771980

tests/unit/Codeception/Module/TestsForWeb.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,19 @@ public function testGrabTextFrom()
706706
$this->assertSame('test', $result);
707707
}
708708

709+
public function testGrabTextFromWithArraySelectorForExistingElement()
710+
{
711+
$this->module->amOnPage('/');
712+
$this->assertSame('More info', $this->module->grabTextFrom(['id' => 'link']));
713+
}
714+
715+
public function testGrabTextFromWithArraySelectorForNonExistentElement()
716+
{
717+
$this->module->amOnPage('/');
718+
$this->expectException('Codeception\Exception\ElementNotFound');
719+
$this->module->grabTextFrom(['id' => 'unknown_link']);
720+
}
721+
709722
public function testGrabValueFrom()
710723
{
711724
$this->module->amOnPage('/form/hidden');

0 commit comments

Comments
 (0)