Skip to content

Commit

Permalink
Merge pull request #7946 from michalsn/feat/domparser-seeXPath
Browse files Browse the repository at this point in the history
feat: domparser - ability to write more advanced expressions
  • Loading branch information
kenjis authored Sep 20, 2023
2 parents 5ef7fa2 + 60dc155 commit 0280297
Show file tree
Hide file tree
Showing 6 changed files with 104 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")]'));
}
}
3 changes: 3 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Commands
Testing
=======

- **DomParser:** The new methods were added ``seeXPath()`` and ``dontSeeXPath()``
which allows users to work directly with DOMXPath object, using complex expressions.

Database
========

Expand Down
17 changes: 17 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,23 @@ Finally, you can check if a checkbox exists and is checked with the ``seeCheckbo
.. literalinclude:: response/023.php
:lines: 2-

seeXPath()
----------

.. versionadded:: 4.5.0

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 expressions
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 0280297

Please sign in to comment.