shanebonham
shanebonham

Reputation: 2279

Using PHPUnit with Selenium, how can I test that an element contains exactly something?

I'm using PHPUnit and Selenium, and currently using something like $this->assertElementContainsText('id=foo', 'bar') which passes when it finds this: <p id="foo">bar</p>

However, I am also trying to test for a case where p#foo might contain other HTML, and I want to test that the contents matches exactly. In my mind, it would look something like $this->assertElementTextEquals('id=foo', '<a href="http://www.example.com/">bar</a>').

Is there an existing way to achieve this with PHPUnit and Selenium?

Upvotes: 1

Views: 4504

Answers (1)

David Harkness
David Harkness

Reputation: 36522

You can pass in any Xpath to target the desired element.

$this->assertElementContainsText(
    "//p[@id='foo']/a[@href='http://www.example.com/']",
    'bar'
);

You can add the following to your base class to allow testing for exact matches on the contents.

/**
 * Asserts that an element contains exactly a given string.
 *
 * @param  string $locator
 * @param  string $text
 * @param  string $message
 */
public function assertElementTextEquals($locator, $text, $message = '')
{
    $this->assertEquals($text, $this->getText($locator), $message);
}

Upvotes: 5

Related Questions