PHP使用Selenium



通过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环境支持,可使用XVNCX 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值