美文网首页我爱编程开源工具技巧
在Codeception使用phantomjs为webdrive

在Codeception使用phantomjs为webdrive

作者: yggira | 来源:发表于2017-02-13 16:16 被阅读0次

前言

codeception是一套基于php的网站服务测试工具,其简洁的语法与易用性非常推荐大家使用。

官方网址:codeception.com ,他的家族还有另外一套 codecept.io 是基于NodeJS的,有兴趣的小夥伴可以自行研究一下。

在进行网站测试的同时,有时我们会需要等待页面渲染完成,或是需要预览用户实际上看到的视图,单纯使用curl无法满足我们需求时,这时就是模拟器上场的时候。

一般的情况下,codecept所使用的是Symfony模组用来处理各种页面的呼叫,而使用到REST模组时,预设会使用PhpBrowser,而当我们有需要预览视图或是撷取画面的时候,这时候就是使用WebDriver模组的时候了。

这边我们以phantomjs这套为例:

因为我要测试的网站有桌机版跟行动版,其判读方式透过浏览器的useragent,所以在呼叫模组的时候必须进行设置。

以下为范例

1.首先我们需要创建一个测试环境
在命令列执行下面的指令,当然mobile可以替换成实际专案的名称

codecept generate:suite mobile

会得到类似下面的内容

Helper \Helper\Mobile was created in /home/user/codecept/project/tests/_support/Helper/Mobile.php
Actor MobileTester was created in /home/user/codecept/project/tests/_support/MobileTester.php
Suite config mobile.suite.yml was created.

Next steps:
1. Edit mobile.suite.yml to enable modules for this suite
2. Create first test with generate:cest testName ( or test|cept) command
3. Run tests of this suite with codecept run mobile command

2.设置 mobile.suite.yml 内容

class_name: MobileTester
modules:
enabled:
    - \Helper\Server
    - WebDriver:
        url: http://yourTestUrl
        host: '127.0.0.1'
        port: 4444
        window_size: '360x640''
        capabilities:
                phantomjs.page.settings.userAgent: "Mozilla/5.0 (Linux; Android 6.0; Redmi Note 4 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.91 Mobile Safari/537.36"

WebDriver下的设定值说明:
url : 要测试的网站网址
host : phantomjs的网址
port : phantomjs预设的port
window_size : 模拟的装置解析度
capabilities: (设定套件的参数)
    phantomjs.page.settings.userAgent : 设置phantomjs的userAgent
官方文件在这边说的不是很清楚,不过笔者是使用上述的设置来模拟行动装置的,至于userAgent的值可以各种自行代换,这边用的是小米Note4的浏览器。

3.执行你的测试

codecept run mobile

官方的测试范例都还蛮易读的,就不多做赘述,只是推荐一款好用的工具给大家,至于要怎么启用phantomjs的话,可以参考 http://phantomjs.org/download.html 

下载解压执行:

./phantomjs --webdriver=4444

然后看到下面的信息:
[INFO  - 2017-02-13T08:13:08.551Z] GhostDriver - Main - running on port 4444

就表示你已经成功启动了。

祝大家玩的愉快~当然也欢迎小夥伴留言讨论~

相关文章

网友评论

    本文标题:在Codeception使用phantomjs为webdrive

    本文链接:https://www.haomeiwen.com/subject/wpxtwttx.html