From 3dfaa671183e23b81b731d3d69cc20c699a0752f Mon Sep 17 00:00:00 2001 From: michalsn Date: Fri, 15 Sep 2023 10:04:52 +0200 Subject: [PATCH] feat: add seeXPath to DOMParser class --- system/Test/DOMParser.php | 18 ++++++++ tests/system/Test/DOMParserTest.php | 44 +++++++++++++++++++ user_guide_src/source/testing/response.rst | 15 +++++++ .../source/testing/response/033.php | 11 +++++ .../source/testing/response/034.php | 11 +++++ 5 files changed, 99 insertions(+) create mode 100644 user_guide_src/source/testing/response/033.php create mode 100644 user_guide_src/source/testing/response/034.php diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 100b14b72dff..1a2cbb0d61aa 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -175,6 +175,24 @@ public function seeCheckboxIsChecked(string $element): bool return (bool) $result->length; } + /** + * Checks to see if the XPath can be found. + */ + public function seeXPath(string $path): bool + { + $xpath = new DOMXPath($this->dom); + + return (bool) $xpath->query($path)->length; + } + + /** + * Checks to see if the XPath can't be found. + */ + public function dontSeeXPath(string $path): bool + { + return ! $this->seeXPath($path); + } + /** * Search the DOM using an XPath expression. * diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index f6e33cc32250..63d04e9926e9 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -416,4 +416,48 @@ public function testSeeAttribute(): void $this->assertTrue($dom->see(null, '*[ name = user ]')); $this->assertFalse($dom->see(null, '*[ name = notthere ]')); } + + public function testSeeXPathSuccess(): void + { + $dom = new DOMParser(); + + $html = '

Hello World Wide Web

'; + $dom->withString($html); + + $this->assertTrue($dom->seeXPath('//h1[contains(@class, "heading")]')); + $this->assertTrue($dom->seeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World")]')); + } + + public function testSeeXPathFail(): void + { + $dom = new DOMParser(); + + $html = '

Hello World Wide Web

'; + $dom->withString($html); + + $this->assertFalse($dom->seeXPath('//h1[contains(@class, "heading123")]')); + $this->assertFalse($dom->seeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World 123")]')); + } + + public function testDontSeeXPathSuccess(): void + { + $dom = new DOMParser(); + + $html = '

Hello World Wide Web

'; + $dom->withString($html); + + $this->assertTrue($dom->dontSeeXPath('//h1[contains(@class, "heading123")]')); + $this->assertTrue($dom->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World 123")]')); + } + + public function testDontSeeXPathFail(): void + { + $dom = new DOMParser(); + + $html = '

Hello World Wide Web

'; + $dom->withString($html); + + $this->assertFalse($dom->dontSeeXPath('//h1[contains(@class, "heading")]')); + $this->assertFalse($dom->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World")]')); + } } diff --git a/user_guide_src/source/testing/response.rst b/user_guide_src/source/testing/response.rst index 2d5f4ddc9f13..435b797a2f3d 100644 --- a/user_guide_src/source/testing/response.rst +++ b/user_guide_src/source/testing/response.rst @@ -223,6 +223,21 @@ Finally, you can check if a checkbox exists and is checked with the ``seeCheckbo .. literalinclude:: response/023.php :lines: 2- +seeXPath() +--------- + +You can use ``seeXPath()`` to take advantage of the full power that xpath gives you. +This method is aimed at more advanced users who want to write a more complex query +using the DOMXPath object directly: + +.. literalinclude:: response/033.php + :lines: 2- + +The ``dontSeeXPath()`` method is the exact opposite: + +.. literalinclude:: response/034.php + :lines: 2- + DOM Assertions ============== diff --git a/user_guide_src/source/testing/response/033.php b/user_guide_src/source/testing/response/033.php new file mode 100644 index 000000000000..cf8cf3a7a3df --- /dev/null +++ b/user_guide_src/source/testing/response/033.php @@ -0,0 +1,11 @@ +seeXPath('//h1[contains(@class, "heading")]')) { + // ... +} + +// Check that h1 element which contains class "heading" have a text "Hello World" +if ($results->seeXPath('//h1[contains(@class, "heading")][contains(.,"Hello world")]')) { + // ... +} diff --git a/user_guide_src/source/testing/response/034.php b/user_guide_src/source/testing/response/034.php new file mode 100644 index 000000000000..e900dd1fff99 --- /dev/null +++ b/user_guide_src/source/testing/response/034.php @@ -0,0 +1,11 @@ +dontSeeXPath('//h1[contains(@class, "heading")]')) { + // ... +} + +// Check that h1 element which contains class "heading" and text "Hello World" does NOT exist on the page +if ($results->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello world")]')) { + // ... +}