Skip to content

Commit

Permalink
feat: add seeXPath to DOMParser class
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Sep 15, 2023
1 parent 642cf3b commit 3dfaa67
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
18 changes: 18 additions & 0 deletions system/Test/DOMParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/system/Test/DOMParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
$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 = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
$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 = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
$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 = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
$dom->withString($html);

$this->assertFalse($dom->dontSeeXPath('//h1[contains(@class, "heading")]'));
$this->assertFalse($dom->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World")]'));
}
}
15 changes: 15 additions & 0 deletions user_guide_src/source/testing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
==============

Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/testing/response/033.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// Check that h1 element which contains class "heading" is on the page
if ($results->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")]')) {
// ...
}
11 changes: 11 additions & 0 deletions user_guide_src/source/testing/response/034.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

// Check that h1 element which contains class "heading" does NOT exist on the page
if ($results->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")]')) {
// ...
}

0 comments on commit 3dfaa67

Please sign in to comment.