将PHPUnit与Selenium结合使用,如何测试元素完全包含某些元素? [英] Using PHPUnit with Selenium, how can I test that an element contains exactly something?
问题描述
我正在使用PHPUnit和Selenium,当前使用的是类似$this->assertElementContainsText('id=foo', 'bar')
的东西,当发现以下内容时就会通过:<p id="foo">bar</p>
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>
但是,我还尝试测试p#foo
可能包含其他HTML的情况,并且我想测试内容是否完全匹配 .在我看来,它看起来像$this->assertElementTextEquals('id=foo', '<a href="http://www.example.com/">bar</a>')
.
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>')
.
是否存在使用PHPUnit和Selenium实现此目的的方法?
Is there an existing way to achieve this with PHPUnit and Selenium?
推荐答案
您可以传入任何Xpath来定位所需的元素.
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);
}
这篇关于将PHPUnit与Selenium结合使用,如何测试元素完全包含某些元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!