我使用蜘蛛和PHPUnit对一个PHP项目进行功能测试,通常使用PhantomJS作为底层浏览器。这在使用遗留基础结构的Travis CI中非常好。
我在.travis.yml中切换到了.travis.yml,尝试迁移到基于Docker的基础设施,而所有基于PhantomJS的测试都失败了。实际上,我增加了两个新的琐碎测试,并且发现如果Spiderling切换为一个简单的基于cURL的提取,那么一切都很好:
/**
* Check to see if any tests are working on Travis containers
*
* (This succeeds)
*
* @driver simple
*/
public function testAnythingAtAllSimple()
{
$text = $this->visit(self::DOMAIN)->find('.container')->text();
$this->assertContains('Questions about this site', $text);
}
/**
* Check to see if any tests are working on Travis containers
*
* (This fails - identical except for the driver)
*
* @driver phantomjs
*/
public function testAnythingAtAllPhantom()
{
$text = $this->visit(self::DOMAIN)->find('.container')->text();
$this->assertContains('Questions about this site', $text);
}因此,我的Travis YAML配置是:
language: php
sudo: false
php:
- "5.6"
notifications:
email: false
before_script:
- phantomjs --help
- composer self-update
- COMPOSER=composer.travis.json composer install
- git submodule init
- git submodule update
- npm install bower
- bower install
- mysql -u root < test/build/create-user.sql
- export PATH=`pwd`/bin:$PATH
- cp config/env-config.php.example config/env-config.php
- touch /tmp/phantom-awooga.log
script:
- phpunit --coverage-clover=coverage.clover
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
- cat /tmp/phantom-awooga.log
- cat /tmp/awooga-screenshot-data.log我添加了phantomjs --help,以证明PhantomJS确实可以在这个新的基础结构中使用,这使通常的帮助输出与预期的一样。我不是自己安装这个-它是默认安装在Travis构建机器。
在另一个测试中,我在$this->visit()之后尝试了一个$this->visit(),这并没有什么区别。我曾想过,幻影是否需要更多的时间来适应新的环境。
我还尝试了制作屏幕截图的蜘蛛/幻影特性,我只获得了一个大的透明图像(它是一个有效的PNG,但上面没有任何内容)。
我想知道蜘蛛是不是意外地抑制了PhantomJS中一些导致困难的错误?PhantomJS确实正确启动,只是不会呈现。我以编程方式打开了连接日志记录,它正在记录OK,但是没有明显的错误消息:
Starting server on port 4516
Recieved command POST /url http://localhost:8090
Executing command POST /url http://localhost:8090
http://localhost:8090 fail
Recieved command POST /elements .//descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' container ')]
Executing command POST /elements .//descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' container ')]
Recieved command POST /elements .//descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' container ')]
Executing command POST /elements .//descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' container ')]
Recieved command POST /elements .//descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' container ')]
Executing command POST /elements .//descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' container ')]http://localhost:8090 fail看起来不太好,但它并没有解释问题的所在。
我对新的基础设施的理解有限,只是不允许使用sudo。但是,我在构建中的任何地方都不使用根权限。
这个问题并不太重要,因为测试在遗留基础结构上运行良好。然而,由于它是“遗产”,我认为它将在某个时候被撤回,所以如果可能的话,让它发挥作用是有意义的。
PhantomJS提供了一个--debug交换机,但不幸的是,Spiderling并没有提供一种简单的方式来添加它。因此,我使用了post黑客来覆盖Travis配置中所需的文件,这种方法确实会在日志中添加DEBUG信息。但是,我看不出有什么有用的。
因为这可能是特拉维斯的问题,我有惹起一只虫子。同时,对可能导致此问题的遗留环境和容器环境中的差异的任何观察都将是最受欢迎的。
发布于 2015-09-16 15:52:54
对于那些经历同样问题的人来说:
显然,在将主机名localhost解析为IP 127.0.0.1时,测试中的客户端与and服务器之间似乎存在漏洞。这个bug可能在PhantomJS或PHP中,因为直接调用curl很好。
简单的修复/解决方法是修改测试以调用http://127.0.0.1:port,并修改服务器配置以侦听127.0.0.1。由于不需要将主机名解析为环-IP,所以客户机从PHP服务器获取网页没有问题。
https://stackoverflow.com/questions/32537787
复制相似问题