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.pharinstall

安装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
classGitHubTest extendsPHPUnit_Framework_TestCase {
 
    protected$webDriver;
 
    publicfunction setUp()
    {
        $capabilities= array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
        $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub',$capabilities);
    }
 
    protected$url = 'https://www.baidu.com';
 
    publicfunction 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= newFirefoxProfile();
$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

发布了430 篇原创文章 · 获赞 415 · 访问量 925万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览