通过composer安装phpunit和Selenium,先安装composer:
1
|
curl -sS https:
//getcomposer
.org
/installer
| php --
install
-
dir
=
/usr/bin/
|
composer.json:
1
2
3
4
5
6
|
{
"require": {
"facebook/webdriver": "dev-master",
"phpunit/phpunit": "*"
}
}
|
安装:
1
|
composer.phar
install
|
安装JAVA,安装Selenium Server,启动:
1
2
|
wget http:
//selenium-release
.storage.googleapis.com
/2
.42
/selenium-server-standalone-2
.42.2.jar
java -jar selenium-server-standalone-2.42.2.jar
|
启动浏览器需X环境支持,可使用XVNC或X Window
可以使用Firefox扩展Selenium IDE: PHP Formatters录制脚本。
示例脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
class
GitHubTest
extends
PHPUnit_Framework_TestCase {
protected
$webDriver
;
public
function
setUp()
{
$capabilities
=
array
(\WebDriverCapabilityType::BROWSER_NAME =>
'firefox'
);
$this
->webDriver = RemoteWebDriver::create(
'http://localhost:4444/wd/hub'
,
$capabilities
);
}
protected
$url
=
'https://www.baidu.com'
;
public
function
testGitHubHome()
{
$this
->webDriver->get(
$this
->url);
var_dump(
$this
->webDriver->getTitle());
}
}
?>
|
不使用phpunit:
1
2
3
4
5
6
7
8
|
<?php
require
'vendor/autoload.php'
;
$capabilities
=
array
(\WebDriverCapabilityType::BROWSER_NAME =>
'firefox'
);
$webDriver
= RemoteWebDriver::create(
'http://localhost:4444/wd/hub'
,
$capabilities
);
$url
=
'https://github.com'
;
$webDriver
->get(
$url
);
var_dump(
$webDriver
->getTitle());
?>';
|
自定义:
1
2
3
4
5
6
7
8
9
|
require
'vendor/autoload.php'
;
$profile
=
new
FirefoxProfile();
$profile
->setPreference(
'browser.startup.homepage'
,
'https://github.com/facebook/php-webdriver/'
);
$profile
->setPreference(
"general.useragent.override"
,
"Mozilla/5.0"
);
$profile
->addExtension(
'./vimperator-3.8.2-fx.xpi'
);
$caps
= DesiredCapabilities::firefox();
$caps
->setCapability(FirefoxDriver::PROFILE,
$profile
);
$caps
->setCapability(WebDriverCapabilityType::PROXY,
array
(
'proxyType'
=>
'manual'
,
'httpProxy'
=>
'127.0.0.1:2043'
));
$webDriver
= RemoteWebDriver::create(
'http://localhost:4444/wd/hub'
,
$caps
);
|
文档:
https://github.com/facebook/php-webdriver/wiki
http://facebook.github.io/php-webdriver/namespaces/default.html
原文地址: http://www.haiyun.me/archives/987.html
本文地址: http://blog.csdn.net/aerchi/article/details/42146241