我有一个基于 Magento 的应用程序,我正在尝试将 PHPUnit 与 Selenium 一起使用来测试单击链接,但我一直收到错误消息。我也在使用BrowserStack,这可能会导致问题。此外,如果有人知道如何在终端中打印出所有的 html,那也太好了。
//Use composer to insall phpunit-selenium
require '/home/mark/PhpstormProjects/Project/vendor/autoload.php';
define('BROWSERSTACK_USER', 'USERNAME');
define('BROWSERSTACK_KEY', 'TOKEN');
class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
public static $browsers = [
/* array(
'browserName' => 'Safari',
'host' => 'hub.browserstack.com',
'port' => 80,
'desiredCapabilities' => array(
'version' => '6.1',
'browserstack.user' => BROWSERSTACK_USER,
'browserstack.key' => BROWSERSTACK_KEY,
'os' => 'OS X',
'os_version' => 'Mountain Lion'
)
),*/
[
'browserName' => 'chrome',
'host' => 'hub.browserstack.com',
'port' => 80,
'desiredCapabilities' => [
'version' => '30',
'browserstack.user' => BROWSERSTACK_USER,
'browserstack.key' => BROWSERSTACK_KEY,
'os' => 'Windows',
'os_version' => '8.1'
]
]
/* array(
'browserName' => 'IE',
'host' => 'hub.browserstack.com',
'port' => 80,
'desiredCapabilities' => array(
'version' => '11.0',
'browserstack.user' => BROWSERSTACK_USER,
'browserstack.key' => BROWSERSTACK_KEY,
'os' => 'Windows',
'os_version' => '7'
)
)*/
];
protected function setUp()
{
parent::setUp();
$this->setBrowserUrl('http://www.example.com/');
}
public function testTitle()
{
$this->url('http://www.example.com/');
$this->assertEquals('Example Domain', $this->title());
}
public function testGoogle()
{
echo $this->getBrowser();
$this->url('http://www.google.com/');
$element = $this->byName('q');
$element->click();
$this->keys('Browserstack');
$button = $this->byName('btnG');
$button->click();
$this->assertEquals('Browserstack - Google Search', $this->title());
}
public function testCheckout()
{
echo $this->getBrowser();
$this->url('http://sitename/checkout/cart/add/uenc/aHR0cDovL3Rlc3QuamV3ZWxzdHJlZXQuY29tL2VhcnJpbmdz/product/1308/');
$this->url('http://test.sitename.com/checkout/cart/');
$this->assertEquals('Shopping Cart | sitename.com', $this->title());
$element = $this->byXPath('/html/body/div[3]/section/div/div/div[1]/div[2]/div[5]/div[2]/button[2]');
//ive also tried
$element = $this->byId('mfi5');
//this id is generated by the version of magento we are using
$this->click($element);
$this->assertEquals('Checkout | sitename.com', $this->title());
}
}
我收到以下错误:
WebTest::testCheckout
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: no such element
(Session info: chrome=30.0.1599.101)
(Driver info: chromedriver=2.6.232923,platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 29 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
System info: host: '5-255-93-8', ip: '5.255.93.8', os.name: 'windows', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_05'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=C:\Windows\proxy\scoped_dir3904_2113}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=30.0.1599.101, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: db0a84a0384ebea5dc167bde8cba098f
org.openqa.selenium.NoSuchElementException
经过大量测试并经历了一些事情,我得到了这个:这不是使用浏览器堆栈,使用浏览器堆栈有多容易,我将如何删除烦人的睡眠();线条更干净
public function testAddProductToBag()
{
$this->url('http://test.project.dev/item3');
//Selects medium from the drop down list
$ringSizeOption = $this->byCssSelector('#select_4267 > option:nth-child(3)');
$ringSizeOption->click();
$addtoBag = $this->byXPath("//*[@title='Add to bag >>']");
$addtoBag->click();
sleep(7);
//$this->timeouts()->implicitWait(7000);
//Selects the total amount from the div in the top left corner
$amountDiv = $this->byCssSelector('a.blog:nth-child(2) > label:nth-child(1)');
$this->assertEquals("£48.60 (1)", $amountDiv->text(), "Error: Nothing in cart");
}