这是按钮的html:
<button class="btn btn-hover-success"><i class="icon-plus"></i> Add</button>
这是根据chrome扩展名的XPath路径:
/body[@class='wide']/div[@class='container']/div[@id='main']/div[@class='container']/div[@class='content']/div[@class='users form']/form[@id='UserAddForm']/div[@class='button-control-row']/button[@class='btn btn-hover-success']
我尝试使用byXPath
和byCssSelector
(以及byLinkText
来定位按钮,但实际上并不是链接)但没有成功。这是我最接近的:
// Attempt to add a bogus user
$netidInput = $this->byId('UserNetid');
$netidInput->value('bogus01');
$submitLink = $this->byCssSelector('button');
$this->assertEquals('Add', $submitLink->text());
$this->click();
$this->assertContains('Users', $this->title());
assertEquals
成功匹配“添加”,即按钮文字但无法点击它。我收到以下错误:
[root@#### View]# phpunit UsersViewTest.php PHPUnit 3.7.18 by Sebastian Bergmann. E Time: 5 seconds, Memory: 5.75Mb There was 1 error: 1) UsersViewTest::testMyTest PHPUnit_Extensions_Selenium2TestCase_WebDriverException: '[JavaScript Error: "can't access dead object" {file: "file:///tmp/anonymous8204548738502860021webdriver-profile/extensions/fxdriver@googlecode.com/components/synthetic-mouse.js" line: 7462}]' when calling method: [wdIMouse::click] Build info: version: '2.43.0', revision: '597b76b', time: '2014-09-09 20:52:14' System info: host: '####', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-358.el6.x86_64', java.version: '1.6.0_24' Driver info: driver.version: unknown /var/www/html/formsdev/app/Plugin/BaseApp/Test/Case/View/UsersViewTest.php:25 /var/www/html/formsdev/app/Plugin/BaseApp/Test/Case/View/UsersViewTest.php:25 FAILURES! Tests: 1, Assertions: 2, Errors: 1.
这是针对RHEL 6.4上的Xvfb无头浏览器运行的。如果我遗漏了任何相关信息,请告诉我。感谢您提供的任何建议!
编辑:添加另一个尝试,其中包含可能触发某人某事的其他错误。
$submitLink = $this->byXPath("//button[contains(text(),'Add')]");
$this->assertEquals('Add', $submitLink->text());
$this->click("//button[contains(text(),'Add')]");
这会导致错误:
There was 1 error:
1) UsersViewTest::testMyTest
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: java.lang.String cannot be cast to java.lang.Long
答案 0 :(得分:1)
事实证明,我的电话点击不正确。而不是$ this-&gt; click(),我需要使用$ submitLink-&gt; click(),它适用于XPath或CssSelector定位它的方法。
// Attempt to add a bogus user
$netidInput = $this->byId('UserNetid');
$netidInput->value('bogus01');
$submitLink = $this->byCssSelector('button');
//$submitLink = $this->byXPath("//button[contains(text(),'Add')]");
$this->assertEquals('Add', $submitLink->text());
$submitLink->click();
$this->assertContains('Users', $this->title());
干杯!