diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index a987d9acff32..ab8616756d34 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -237,19 +237,19 @@ protected function doXPath(?string $search, string $element, array $paths = []) { $path = empty($selector['tag']) ? "id(\"{$selector['id']}\")" - : "//body//{$selector['tag']}[@id=\"{$selector['id']}\"]"; + : "//{$selector['tag']}[@id=\"{$selector['id']}\"]"; } // By Class elseif (! empty($selector['class'])) { $path = empty($selector['tag']) ? "//*[@class=\"{$selector['class']}\"]" - : "//body//{$selector['tag']}[@class=\"{$selector['class']}\"]"; + : "//{$selector['tag']}[@class=\"{$selector['class']}\"]"; } // By tag only elseif (! empty($selector['tag'])) { - $path = "//body//{$selector['tag']}"; + $path = "//{$selector['tag']}"; } if (! empty($selector['attr'])) diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index 1efe81e8c2b9..49e88c625f59 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -97,6 +97,19 @@ public function testSeeHTML() $this->assertTrue($dom->see('

')); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/3984 + */ + public function testSeeHTMLOutsideBodyTag() + { + $dom = new DOMParser(); + + $html = 'My Title

Hello World

'; + $dom->withString($html); + + $this->assertTrue($dom->see('My Title', 'title')); + } + public function testSeeFail() { $dom = new DOMParser();