Skip to content

Commit 0280297

Browse files
authored
Merge pull request #7946 from michalsn/feat/domparser-seeXPath
feat: domparser - ability to write more advanced expressions
2 parents 5ef7fa2 + 60dc155 commit 0280297

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

system/Test/DOMParser.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ public function seeCheckboxIsChecked(string $element): bool
175175
return (bool) $result->length;
176176
}
177177

178+
/**
179+
* Checks to see if the XPath can be found.
180+
*/
181+
public function seeXPath(string $path): bool
182+
{
183+
$xpath = new DOMXPath($this->dom);
184+
185+
return (bool) $xpath->query($path)->length;
186+
}
187+
188+
/**
189+
* Checks to see if the XPath can't be found.
190+
*/
191+
public function dontSeeXPath(string $path): bool
192+
{
193+
return ! $this->seeXPath($path);
194+
}
195+
178196
/**
179197
* Search the DOM using an XPath expression.
180198
*

tests/system/Test/DOMParserTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,48 @@ public function testSeeAttribute(): void
416416
$this->assertTrue($dom->see(null, '*[ name = user ]'));
417417
$this->assertFalse($dom->see(null, '*[ name = notthere ]'));
418418
}
419+
420+
public function testSeeXPathSuccess(): void
421+
{
422+
$dom = new DOMParser();
423+
424+
$html = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
425+
$dom->withString($html);
426+
427+
$this->assertTrue($dom->seeXPath('//h1[contains(@class, "heading")]'));
428+
$this->assertTrue($dom->seeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World")]'));
429+
}
430+
431+
public function testSeeXPathFail(): void
432+
{
433+
$dom = new DOMParser();
434+
435+
$html = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
436+
$dom->withString($html);
437+
438+
$this->assertFalse($dom->seeXPath('//h1[contains(@class, "heading123")]'));
439+
$this->assertFalse($dom->seeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World 123")]'));
440+
}
441+
442+
public function testDontSeeXPathSuccess(): void
443+
{
444+
$dom = new DOMParser();
445+
446+
$html = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
447+
$dom->withString($html);
448+
449+
$this->assertTrue($dom->dontSeeXPath('//h1[contains(@class, "heading123")]'));
450+
$this->assertTrue($dom->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World 123")]'));
451+
}
452+
453+
public function testDontSeeXPathFail(): void
454+
{
455+
$dom = new DOMParser();
456+
457+
$html = '<html><body><h1 class="heading gap-2">Hello World Wide Web</h1></body></html>';
458+
$dom->withString($html);
459+
460+
$this->assertFalse($dom->dontSeeXPath('//h1[contains(@class, "heading")]'));
461+
$this->assertFalse($dom->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello World")]'));
462+
}
419463
}

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ Commands
5252
Testing
5353
=======
5454

55+
- **DomParser:** The new methods were added ``seeXPath()`` and ``dontSeeXPath()``
56+
which allows users to work directly with DOMXPath object, using complex expressions.
57+
5558
Database
5659
========
5760

user_guide_src/source/testing/response.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ Finally, you can check if a checkbox exists and is checked with the ``seeCheckbo
223223
.. literalinclude:: response/023.php
224224
:lines: 2-
225225

226+
seeXPath()
227+
----------
228+
229+
.. versionadded:: 4.5.0
230+
231+
You can use ``seeXPath()`` to take advantage of the full power that xpath gives you.
232+
This method is aimed at more advanced users who want to write a more complex expressions
233+
using the DOMXPath object directly:
234+
235+
.. literalinclude:: response/033.php
236+
:lines: 2-
237+
238+
The ``dontSeeXPath()`` method is the exact opposite:
239+
240+
.. literalinclude:: response/034.php
241+
:lines: 2-
242+
226243
DOM Assertions
227244
==============
228245

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
// Check that h1 element which contains class "heading" is on the page
4+
if ($results->seeXPath('//h1[contains(@class, "heading")]')) {
5+
// ...
6+
}
7+
8+
// Check that h1 element which contains class "heading" have a text "Hello World"
9+
if ($results->seeXPath('//h1[contains(@class, "heading")][contains(.,"Hello world")]')) {
10+
// ...
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
// Check that h1 element which contains class "heading" does NOT exist on the page
4+
if ($results->dontSeeXPath('//h1[contains(@class, "heading")]')) {
5+
// ...
6+
}
7+
8+
// Check that h1 element which contains class "heading" and text "Hello World" does NOT exist on the page
9+
if ($results->dontSeeXPath('//h1[contains(@class, "heading")][contains(.,"Hello world")]')) {
10+
// ...
11+
}

0 commit comments

Comments
 (0)