一、安装phantomjs
首先,需要在机器上安装phantomjs,可以从官网下载并进行安装。以下是linux下的安装步骤:
下载phantomjs二进制文件
$ sudo apt-get install wget unzip
$ wget https://github.com/medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-linux-x86_64.tar.bz2解压二进制文件
$ tar xvf phantomjs-2.1.1-linux-x86_64.tar.bz2将phantomjs可执行文件添加到系统路径中
$ sudo mv phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/现在,phantomjs已经安装完成,并且可以通过在命令行下运行“phantomjs”命令来验证。
二、使用php中的phantomjs进行自动化测试
在安装了phantomjs之后,下一步是将其整合到php中进行自动化测试。以下是一个简单的php脚本,它会使用phantomjs打开google.com并搜索phantomjs。
<?php $url = 'https://www.google.com/'; $searchtext = 'phantomjs'; // 启动phantomjs服务 $phantomjs = new jonnywphantomjsclient(); $phantomjs->getengine()->setpath('/usr/local/bin/phantomjs'); $request = $phantomjs->getmessagefactory()->createrequest($url, 'get'); $response = $phantomjs->getmessagefactory()->createresponse(); // 执行搜索 $request->setdelay(5); // 等待5秒钟 $request->setviewportsize(1024, 768); // 设置视口大小 $request->setrequestdata(array('q' => $searchtext), jonnywphantomjshttprequestinterface::method_post); $phantomjs->send($request, $response); // 输出响应 echo $response->getcontent();
以上代码使用了一个php库“jonnyw/phantomjs”,它提供了一个简单的接口来控制phantomjs并获取响应。首先,需要启动phantomjs服务并设置其执行路径。然后,创建一个请求对象并指定要访问的页面url和搜索文本。接下来,可以设置页面的大小和延迟时间,以及请求类型(post或get)。最后,使用send()方法发送请求并存储响应。响应可以在$response对象中获取到。
三、高级用法
除了以上的基本用法,phantomjs还有很多高级用法可以探索。以下是一些例子:
抓取页面的屏幕截图
使用phantomjs可以方便地捕获屏幕截图,示例代码如下:<?php $url = 'https://www.google.com/'; $searchtext = 'phantomjs'; // 启动phantomjs服务 $phantomjs = new jonnywphantomjsclient(); $phantomjs->getengine()->setpath('/usr/local/bin/phantomjs'); $request = $phantomjs->getmessagefactory()->createrequest($url, 'get'); // 设置截图配置 $settings = array( 'quality' => 90, 'format' => 'png', 'viewportsize' => array('width' => 1024, 'height' => 768), 'cliprect' => array('top' => 0, 'left' => 0, 'width' => 1024, 'height' => 768), 'papersize' => array('format' => 'a4', 'orientation' => 'portrait', 'margin' => '1cm'), ); // 创建一个屏幕截图 $screenshot = $phantomjs->capturescreenshot($request, $settings); // 保存截图到文件 file_put_contents('screenshot.png', $screenshot->getbinary());
模拟用户操作
phantomjs可以模拟用户的点击、滚动和输入等操作,示例代码如下:<?php $url = 'https://www.google.com/'; $searchtext = 'phantomjs'; // 启动phantomjs服务 $phantomjs = new jonnywphantomjsclient(); $phantomjs->getengine()->setpath('/usr/local/bin/phantomjs'); $request = $phantomjs->getmessagefactory()->createrequest($url, 'get'); $response = $phantomjs->getmessagefactory()->createresponse(); // 执行搜索 $request->setdelay(5); // 等待5秒钟 $request->setviewportsize(1024, 768); // 设置视口大小 $request->setrequestdata(array('q' => $searchtext), jonnywphantomjshttprequestinterface::method_post); // 模拟点击 $request->setscript('document.getelementsbyname("btng")[0].click();'); // 模拟滚动 $request->setscript('window.scrollto(0, document.body.scrollheight);'); // 模拟输入 $request->setscript('document.getelementsbyname("q")[0].setattribute("value", "phantomjs");'); $phantomjs->send($request, $response); // 输出响应 echo $response->getcontent();
四、结论
phantomjs是一个强大的无界面测试工具,可以帮助开发人员在web开发过程中自动化测试。通过将phantomjs与php结合使用,可以轻松编写无界面测试脚本并模拟用户操作。无论是对于开发人员还是测试人员来说,都可以大大提高测试效率。
以上就是php中如何使用phantomjs进行无界面测试的详细内容。