From 809f741df7af01f581b2fb67d449f28d56093538 Mon Sep 17 00:00:00 2001 From: Olexandr Ponomarenko Date: Mon, 17 Jan 2022 09:26:41 -0500 Subject: [PATCH] Avoid TypeError in preg_match() --- src/Codeception/Module/WebDriver.php | 5 ++++- tests/unit/Codeception/Module/TestsForWeb.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 5684bb0..c11c5b3 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -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]; } diff --git a/tests/unit/Codeception/Module/TestsForWeb.php b/tests/unit/Codeception/Module/TestsForWeb.php index ed737f5..89510b7 100644 --- a/tests/unit/Codeception/Module/TestsForWeb.php +++ b/tests/unit/Codeception/Module/TestsForWeb.php @@ -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');