divineomega/phantomjs-laravel-testing

PhantomJS Laravel 测试

v1.0 2017-02-16 17:23 UTC

This package is auto-updated.

Last update: 2024-09-06 08:59:11 UTC


README

PhantomJS Laravel 测试包允许您轻松测试 Laravel 应用的 JavaScript 功能。它利用 PhantomJS 无头浏览器来模拟真实用户与您页面交互的方式。如果您已经习惯了常规的 Laravel 测试,那么您会高兴地知道,这个包尽可能地与它的语法相匹配。

💡 注意:如果您正在启动一个新项目,我建议使用 Laravel Dusk。因为 PhantomJS 开发已被暂停,并且可能不会收到任何未来的更新。

特性

  • 尽可能使用与标准 Laravel 测试代码相同的语法
  • 由 PhantomJS 提供动力的无头浏览器允许进行全面的功能测试,包括 JavaScript 和 AJAX
  • 使用数据库事务来防止测试对数据库产生永久性影响
  • 自动设置和安装所有依赖项,包括 phantomjs 二进制文件

要求

  • 目前仅支持 Laravel 5.1

安装

  1. 将 composer 脚本 "PhantomInstaller\\Installer::installPhantomJS" 添加到 composer.jsonpost-install-cmdpost-update-cmd 数组。
  2. 使用 composer require divineomega/phantomjs-laravel-testing 安装。
  3. 将服务提供者 DivineOmega\PhantomJSLaravelTesting\ServiceProvider::class 添加到 config/app.phpproviders 数组。
  4. 将全局中间件 \DivineOmega\PhantomJSLaravelTesting\Http\Middleware\GlobalMiddleware::class 添加到 app/Http/Kernel.phpmiddleware 数组。

用法

只需将您的测试类更改为扩展 PhantomJSTestCase 而不是 TestCase,然后像平常一样运行您的单元测试。当需要时,PhantomJS 将自动启动。

以下是一个示例测试用例。

<?php

use DivineOmega\PhantomJSLaravelTesting\Objects\PhantomJSTestCase;

class ExampleTestCase extends PhantomJSTestCase
{
    public function testGoogleShowsImFeelingLucky()
    {
    
        $this->visit('https://google.co.uk/');
        $this->see('I\'m Feeling Lucky');
    }

    public function testGoogleShowsImFeelingDucky()
    {
        $this->visit('https://google.co.uk/');
        $this->see('I\'m Feeling Ducky');
    }
}