Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: domparser - ability to write more advanced expressions #7946

Merged
merged 7 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -50,6 +50,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()
----------

michalsn marked this conversation as resolved.
Show resolved Hide resolved
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-

.. note:: This method was introduced in v4.5.0.

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")]')) {
// ...
}
Loading