php,Laravel-Selenium,PHP Selenium使用教程

教程

Selenium是用于自动化测试工具,它是在Apache许可证2.0许可的开放源代码工具。Selenium是一套工具,它有助于自动化Web应用程序测试。

  • 框架底层使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动按照脚本代码做出点击,输入,打开,验证等操作,就像真实用户所做的一样,从终端用户的角度测试应用程序。

  • 使浏览器兼容性测试自动化成为可能,尽管在不同的浏览器上依然有细微的差别。

  • 使用简单,可使用Java,Python等多种语言编写用例脚本。

1.通过composer安装Selenium:

composer require facebook/webdriver

2.下载Selenium Server并启动:

//到http://www.seleniumhq.org/download/ 下找到Selenium Standalone Server并下载,或到https://chromedriver.storage.googleapis.com/index.html 上面下载。 //到命令提示符里启动以下命令(前提需要安装jdk,确保java命令能够运行) java -jar selenium-server-standalone-2.42.2.jar

3.运行测试代码

另启命令提示符,运行php test.php 命令
示例脚本test.php:

<?php
namespace Facebook\\WebDriver;use Facebook\\WebDriver\\Remote\\DesiredCapabilities;use Facebook\\WebDriver\\Remote\\RemoteWebDriver;require\_once('vendor/autoload.php');

header("Content-Type: text/html; charset=UTF-8");// start Firefox with 5 second timeout$waitSeconds = 15;  //需等待加载的时间,一般加载时间在0-15秒,如果超过15秒,报错。$host = 'http://localhost:4444/wd/hub'; // this is the default//这里使用的是chrome浏览器进行测试,需到http://www.seleniumhq.org/download/上下载对应的浏览器测试插件//我这里下载的是win32 Google Chrome Driver 2.25版:https://chromedriver.storage.googleapis.com/index.html?path=2.25/$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);// navigate to 'http://docs.seleniumhq.org/'$driver->get('https://www.baidu.com/');echo iconv("UTF-8","GB2312",'标题1').":" . $driver->getTitle() . "\\n";	//cmd.exe中文乱码,所以需转码$driver->findElement(WebDriverBy::id('kw'))->sendKeys('wwe')->submit();// 等待新的页面加载完成....$driver->wait($waitSeconds)->until(
    WebDriverExpectedCondition::visibilityOfElementLocated(
        WebDriverBy::partialLinkText('100shuai')
    )
);
$driver->findElement(WebDriverBy::partialLinkText('100shuai'))->sendKeys('xxx')->click();	//一般点击链接的时候,担心因为失去焦点而抛异常,则可以先调用一下sendKeys,再clickswitchToEndWindow($driver); //切换至最后一个window// 等待加载....$driver->wait($waitSeconds)->until(
    WebDriverExpectedCondition::visibilityOfElementLocated(
        WebDriverBy::partialLinkText('SmackDown收视率创历史新低')
    )
);echo iconv("UTF-8","GB2312",'标题2').":" . $driver->getTitle() . "\\n";	//cmd.exe中文乱码,所以需转码$driver->findElement(WebDriverBy::partialLinkText('SmackDown收视率创历史新低'))->click();

switchToEndWindow($driver); //切换至最后一个window// 等待加载....$driver->wait($waitSeconds)->until(
    WebDriverExpectedCondition::titleContains('SmackDown收视率创历史新低')
);echo iconv("UTF-8","GB2312",'标题3').":" . $driver->getTitle() . "\\n";	//cmd.exe中文乱码,所以需转码//关闭浏览器$driver->quit();//切换至最后一个window//因为有些网站的链接点击过去带有target="\_blank"属性,就新开了一个TAB,而selenium还是定位到老的TAB上,如果要实时定位到新的TAB,则需要调用此方法,切换到最后一个windowfunction switchToEndWindow($driver){
    
    $arr = $driver->getWindowHandles();    foreach ($arr as $k=>$v){        if($k == (count($arr)-1)){
            $driver->switchTo()->window($v);
        }
    }
}?>

下载资料:
1.http://selenium-release.storage.googleapis.com/index.html (selenium 下载地址)
2.https://chromedriver.storage.googleapis.com/index.html (chrome driver 下载地址)
3.http://phantomjs.org/download.html (PhantomJS Driver 下载地址)
4.http://www.cnbeta.com/articles/soft/563605.htm (chrome 下载地址,建议使用这个版本或者以下版本,其他最新版本,浏览器识别了是否为测试软件,对于个别用途的软件需要注意,如果仅仅是为了做测试,那就无所谓了。)
5.http://blog.csdn.net/huilan_same/article/details/51896672 (chromedriver.exe版本对应的chrome版本)

参考文档:
1.https://github.com/facebook/php-webdriver (里面有example.php以及 tests文件下的案例文档共参考)
2.https://github.com/facebook/php-webdriver/wiki 快速开始教程
3.http://facebook.github.io/php-webdriver/namespaces/default.html API文档
4.http://www.yiibai.com/selenium/ 易百教程
6.https://github.com/chibimagic/WebDriver-PHP/
7.https://code.google.com/archive/p/php-webdriver-bindings/
8.https://github.com/Element-34/php-webdriver
9.https://github.com/Nearsoft/php-selenium-client
10.http://pan.baidu.com/s/1eR31pM6 selenium_webdriver(python)第一版.pdf

转载自:https://www.kancloud.cn/wangking/selenium/234398

王加文博客
请先登录后发表评论
  • latest comments
  • 总共0条评论