【发布时间】:2015-10-30 13:02:58
【问题描述】:
我将 Laravel 与 Vagrant 一起使用。我需要使用 Codeception 和 PhantomJS 运行验收测试。除了运行 JS 代码外,一切似乎都很好。
我有一个注册表单,使用一点JS代码来防止机器人注册:
<script type="text/javascript">
$(function() {
$('.form-horizontal').append('<input type="checkbox" name="human" value="1" checked="checked">');
});
</script>
这就是我所做的。
1) 我运行 phantomjs:
B# phantomjs --webdriver=5555
2) 开始验收测试:
vendor/bin/codecept run acceptance RegisterCept
当然测试失败是因为 PhantomJS 不执行 JS 代码,没有它就无法完成注册。我究竟做错了什么?配置文件:
class_name: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://localhost
browser: phantomjs
port: 4444
capabilities:
javascriptEnabled: true
webStorageEnabled: true
unexpectedAlertBehaviour: 'accept'
- Laravel5:
environment_file: .env.testing
- \Helper\Acceptance
我正在使用 Travis。测试也失败了。我的 .travis.yml:
language: php
php:
- 5.5
- 5.6
services: postgresql
addons:
postgresql: "9.3"
install:
- composer install
before_script:
- cp .env.testing .env
- php artisan migrate --seed --env="testing"
- php vendor/bin/codecept build
- phantomjs --webdriver=4444 2>&1 >/dev/null &
- sleep 5
script: php vendor/bin/codecept run
我的测试:
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('register a user');
$I->amOnPage('/Register');
$I->wait(1);
$I->fillField('name', 'Joe Doe');
$I->fillField('email', 'example@example.com');
$I->fillField('password', 'password');
$I->fillField('password_confirmation', 'password');
$I->click('button[type=submit]');
$I->amOnPage('/');
$I->see('Joe Doe');
【问题讨论】:
-
“PhantomJS 不执行 JS 代码” 当然可以。这就是为什么如果涉及 JavaScript,最好使用机械化之类的爬虫。问题可能是它无法在本地主机上打开页面。试试
127.0.0.1。 -
谢谢,但还是没有运气。我知道 PhantomJS 支持 JavaScript。它只是在我的情况下不起作用。我确定我错过了什么......
标签: laravel-5 phantomjs codeception