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

Asserting text in nested elements #19

Open
jakobbuis opened this issue Apr 26, 2020 · 1 comment
Open

Asserting text in nested elements #19

jakobbuis opened this issue Apr 26, 2020 · 1 comment

Comments

@jakobbuis
Copy link
Contributor

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
     */
    public function assertElementContains(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
     */
    public function assertElementNotContains(string $text, string $element, string $selector, ?string $message = ''): void
    {
        $this->assertStringNotContainsString($text, $this->parseToSelector($element, $selector), $message);
    }

    private function parseToSelector(string $element, string $selector): string
    {
        return html5qp($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. :)

@stevegrunwell
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants