openbuildings / phpunit-spiderling
在 phpunit 测试中使用 openbuildings/spiderling
Requires
- php: ^7.3 || ^7.4
- openbuildings/environment-backup: ^0.1
- openbuildings/spiderling: ^0.4
- phpunit/phpunit: ^9
Requires (Dev)
- kohana/core: ^3.3
This package is auto-updated.
Last update: 2024-08-27 15:50:24 UTC
README
深受 capybara capybara 启发。充分利用 spiderling 包。它为您提供了使用强大的 DSL 快速编写集成测试的能力,并可选择具有不同功能组合和性能的不同驱动程序 - 例如 phanomjs 或使用 curl 的原始 php。
示例测试
use Openbuildings\PHPUnitSpiderling\TestCase; class SpiderlingTest extends TestCase { /** * @driver phantomjs */ public function test_sample() { $this ->visit('http://example.com/index.html') ->assertHasCss('#navlink-1', array('text' => 'Subpage 1'), 'Should have a navigation link') ->click_button('Edit') ->assertHasCss('h1', array('text' => 'Edit Record'), 'Should be on the edit page of a record') ->fill_in('Name', 'New name') ->click_button('Save') ->assertHasCss('.notification', array('text' => 'Successfull edit'), 'Should have successfully performed the edit'); } }
Spiderling
Spiderling 具有流畅的 DSL,您最好熟悉它。测试用例包含 Openbuildings\Spiderling\Page 的所有方法,因此您可以直接使用它们,以及一些额外的断言。
测试用例
要使用 phpunit spiderling,您需要将您的测试编写为 extends \Openbuildings\PHPUnitSpiderlingTestCase
- 它将为您提供类的所有功能作为方法。
断言
自定义断言有
assertHasCss($selector, array $filters = array(), $message = NULL)
assertHasNoCss($selector, array $filters = array(), $message = NULL)
assertHasField($selector, array $filters = array(), $message = NULL)
assertHasNoField($selector, array $filters = array(), $message = NULL)
assertHasXPath($selector, array $filters = array(), $message = NULL)
assertHasNoXPath($selector, array $filters = array(), $message = NULL)
assertHasLink($selector, array $filters = array(), $message = NULL)
assertHasNoLink($selector, array $filters = array(), $message = NULL)
assertHasButton($selector, array $filters = array(), $message = NULL)
assertHasNoButton($selector, array $filters = array(), $message = NULL)
所有这些断言都是通过使用特定的定位类型和过滤器来确认元素是否在页面上(或不在),实际上它们是 Openbuildings\Spiderling\Page 的扩展,因此它们对所有嵌套节点都可用(并且仅在节点上下文中进行断言)。
例如
use Openbuildings\PHPUnitSpiderling\TestCase; class SpiderlingTest extends TestCase { public function test_sample() { $this ->visit('http://example.com/index.html') ->find('form#big') // This assertion checks only the form ->assertHasField('Name') ->end(); } }
环境
您可以通过使用 ->environment()
方法仅针对特定测试修改环境。它返回一个 环境备份 对象,并且您设置的任何内容都会在测试结束时恢复。
以下是一个示例用法
use Openbuildings\PHPUnitSpiderling\TestCase; class SpiderlingTest extends TestCase { public function test_sample() { $this ->environment() ->backup_and_set(array( 'SomeClass::$variable' => 'new value', 'REMOTE_HOST' => 'example.com', )); $this ->visit('http://example.com/index.html') ->assertHasField('Name'); } }
切换驱动程序
PHPUnit Spiderling 使用 PHP 注释来设置每个测试要使用的驱动程序。下面是如何做到这一点
use Openbuildings\PHPUnitSpiderling\TestCase; class SpiderlingTest extends TestCase { /** * @driver simple */ public function test_sample() { // ... } /** * @driver phantomjs */ public function test_sample_2() { // ... } }
您可以为每个测试有不同的驱动程序,可用的有: simple
、kohana
和 phantomjs
- 其中默认驱动程序是 simple
。每个驱动程序都带有默认配置,但您可以通过修改加载驱动程序的相应方法来更改它
- driver_simple() - 将返回 Driver_Simple 对象
- driver_kohana() - 将返回 Driver_Kohana 对象
- driver_phantomjs() - 将返回 Driver_Phantomjs 对象
在失败时保存
包含一个特殊的测试用例监听器类,在测试失败时保存测试状态,并将其保存为 HTML 页面,以便以后方便引用。这是 Saveonfailure
类。要使用它,您需要修改您的 phpunit.xml,如下所示
<!-- It is important to have composer autoloading. you can have a different bootstrap file, but this is the standard and easiest way to handle it. --> <phpunit bootstrap="vendor/autoload.php"> <listeners> <listener class="Openbuildings\PHPUnitSpiderling\Saveonfailure" file="vendor/openbuildings/phpunit-spiderling/src/Openbuildings/PHPUnitSpiderling/Saveonfailure.php"> <arguments> <!-- This is the folder where all the html snapshots will be placed --> <string>application/logs/functest</string> <!-- This is the base url which will be prefixed in fron of all relative assets, so that the page is loaded properly. Optional --> <string>http://test.clippings.dev</string> </arguments> </listener> </listeners> </phpunit>
许可证
版权所有 (c) 2012-2013,OpenBuildings Ltd。由 Ivan Kerin 在 clippings.com 作为一部分开发。
在 BSD-3-Clause 许可证下,请参阅 LICENSE 文件。