You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a use case for asserting a node contains a string, including any node's children. I'm currently using a custom trait using querypath.
trait HTMLAssertions
{
/** * Assert that a given element, or any of its children contains the given text * @param string $text the text to check for * @param string $element HTML5 blob * @param string $selector selector to scope the search to * @param string $message optional message when the assertion fails * @return void */publicfunctionassertElementContains(string$text, string$element, string$selector, ?string$message = ''): void
{
$this->assertStringContainsString($text, $this->parseToSelector($element, $selector), $message);
}
/** * Assert that a given element, or any of its children not contains the given text * @param string $text the text to check for * @param string $element HTML5 blob * @param string $selector selector to scope the search to * @param string $message optional message when the assertion fails * @return void */publicfunctionassertElementNotContains(string$text, string$element, string$selector, ?string$message = ''): void
{
$this->assertStringNotContainsString($text, $this->parseToSelector($element, $selector), $message);
}
privatefunctionparseToSelector(string$element, string$selector): string
{
returnhtml5qp($element, $selector)->text();
}
}
A simple approach: parse the HTML-string, find the selector, get node text contents, use string matching.
I'm considering making it a dedicated package, but it could be a fit for including it here? Let me know if this is of interest, before I rewrite it to a pull request. :)
The text was updated successfully, but these errors were encountered:
If I'm not mistaken, you're trying to filter contents to just the text nodes before making string assertions, is that right? Kind of the equivalent of element.innerHTML vs. element.innerText in JavaScript?
It's kinda niche, but I can definitely see where this would be valuable. Maybe a pair of methods like assertElementContainsText() and assertElementNotContainsText()? Would you like to take a stab at a PR for it?
I have a use case for asserting a node contains a string, including any node's children. I'm currently using a custom trait using querypath.
A simple approach: parse the HTML-string, find the selector, get node text contents, use string matching.
I'm considering making it a dedicated package, but it could be a fit for including it here? Let me know if this is of interest, before I rewrite it to a pull request. :)
The text was updated successfully, but these errors were encountered: