Skip to content

Commit ae6c8f5

Browse files
committed
feat: add seeXPath to DOMParser class
1 parent 2daf762 commit ae6c8f5

5 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎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/testing/response.rst‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ 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+
You can use ``seeXPath()`` to take advantage of the full power that xpath gives you.
230+
This method is aimed at more advanced users who want to write a more complex query
231+
using the DOMXPath object directly:
232+
233+
.. literalinclude:: response/033.php
234+
:lines: 2-
235+
236+
The ``dontSeeXPath()`` method is the exact opposite:
237+
238+
.. literalinclude:: response/034.php
239+
:lines: 2-
240+
226241
DOM Assertions
227242
==============
228243

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)