jhoopes/laravel-selenium-driver

一个与 selenium 配合使用的包装器,用于在浏览器中测试 Laravel 应用程序,灵感来源于 laracasts 集成包

dev-master 2016-09-07 21:09 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:23:39 UTC


README

此包主要围绕 Jeffery Way 的集成包设计。它模拟了 Laravel 现有测试包(PHP Unit)的许多 API。

设置/安装

  1. 从这里下载 selenium 2 的最新版本: https://github.com/SeleniumHQ/selenium/releases
  2. 如果您打算使用 firefox 作为浏览器,只需运行 selenium 服务器即可。
  3. 下载完这两样东西后,您可以通过 composer 安装此包

配置和设置第一个测试

  • 此包使用 selenium .env 文件进行某些设置。您可以根据下面的示例创建此文件

示例 .env.seleniumConfig


# Application environment
APP_ENV=testing

# Which browser you'd like to use
BROWSER=chrome 

# The base url for the application on your local machine
BASE_URL=http://test.dev 

# Whether or not you'd like to boot up a laravel application during your testing
#   this is so you can use things like "seeInDatabase"
USE_LARAVEL=true

#If you booted laravel, set this to 1 if you'd like your database connection to be auto migrated
MIGRATE=0

#If you booted laravel, set this to 1 if you'd like your database connection to be auto seeded
SEED=0

#OPTIONAL:  You can optionally set the window's witdth and height with these values.  Be sure that they are integers
WINDOW_WIDTH=1250
WINDOW_HEIGHT=900
  • 此配置文件与 phpunit.xml 文件一起使用。
    • 例如,要连接到数据库并使用 Laravel 进行工作,您需要在其中设置数据库配置信息。
    • 或者确保您的正常 .env 文件中设置了数据库配置。
    • 例如,当使用 homestead 时,这是您在 phpunit.xml 文件中应该放的内容
   <?xml version="1.0" encoding="UTF-8"?>
   <phpunit backupGlobals="false"
            backupStaticAttributes="false"
            bootstrap="bootstrap/autoload.php"
            colors="true"
            convertErrorsToExceptions="true"
            convertNoticesToExceptions="true"
            convertWarningsToExceptions="true"
            processIsolation="false"
            stopOnFailure="false">
       <testsuites>
           <testsuite name="Application Test Suite">
               <directory>./tests/</directory>
           </testsuite>
       </testsuites>
       <filter>
           <whitelist>
               <directory suffix=".php">app/</directory>
           </whitelist>
       </filter>
       <php>
           <env name="APP_ENV" value="testing"/>
           <env name="CACHE_DRIVER" value="array"/>
           <env name="SESSION_DRIVER" value="array"/>
           <env name="QUEUE_DRIVER" value="sync"/>
           <env name="DB_CONNECTION" value="mysql"/>
           <env name="DB_HOST" value="127.0.0.1:33060"/>
           <env name="DB_DATABASE" value="yourDatabase" />
           <env name="DB_USERNAME" value="homestead" />
           <env name="DB_PASSWORD" value="secret" />
       </php>
   </phpunit>
  • 如果您使用 Laravel Valet,则您的数据库应该位于主机本地,因此 phpunit 应该能够通过正常的 .env 文件连接到它
  • 一旦配置设置完成,请参阅示例以进一步了解如何设置测试。

编写测试

  • 附加文档即将推出,但我想指出,重要的是要记住 selenium 将在您的浏览器中运行一个实时会话。
    • 因此,当运行测试时,如果您使用 Laravel,则您的 phpunit 会话将完全不同于浏览器中运行的会话。
    • 当在测试代码中执行“会话”项时,请记住这一点,并且不要在网页浏览器中运行它

运行测试

  • 在运行测试之前,您需要启动您的 selenium 服务器
    • 为此,只需运行 java -jar /path/to/selenium-server/selenium-server.jar
  • 要运行测试,您可以运行包含验收测试的整个目录或单个类。
  • 例如:
phpunit tests/acceptance
phpunit tests/acceptance/ExampleTest.php
  • 应该发生的情况是您的网页浏览器打开,并运行验收测试

文档

  • 文档目前正在制作中。
  • 要查看当前 API 中的内容,请通过 traits 目录查看函数名称。

未来功能

  • 更完善的 API
  • 为类似 selectize、dropzone 等的 JS 小部件提供更多辅助工具
  • 本包的测试
  • 更好的文档