xp-framework/webtest

XP 框架的 Web 测试

v7.1.0 2022-03-04 09:04 UTC

This package is auto-updated.

Last update: 2024-09-04 14:09:55 UTC


README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.0+ Supports PHP 8.0+ Latest Stable Version

XP 框架的 Web 测试

示例

以下 Web 测试案例包括打开 GitHub 的主页并断言标题等于公司名称

use unittest\web\{WebTestCase, Webtest};
use unittest\Test;

#[Webtest(url: 'https://github.com/')]
class GitHubTestCase extends WebTestCase {

  #[Test]
  public function home() {
    $this->beginAt('/');
    $this->assertStatus(200);
    $this->assertTitleEquals('GitHub: Where the world builds software · GitHub');
  }
}

运行它与正常测试案例一样

$ xp test GitHubTestCase
[.]

✓: 1/1 run (0 skipped), 1 succeeded, 0 failed
Memory used: 1861.12 kB (2474.66 kB peak)
Time taken: 1.225 seconds

要覆盖注解中指定的默认 URL,请将其作为命令行参数提供,例如 unittest GitHubTestCase -a https://github.staging.lan/

断言方法

除了 unittest 库提供的断言方法外,以下是与响应相关的断言方法

public void assertStatus(int $status)
public void assertUrlEquals(peer.URL $url)
public void assertContentType(string $ctype)
public void assertHeader(string $header, string $value)
public void assertElementPresent(string $id)
public void assertTextPresent(string $text)
public void assertImagePresent(string $src)
public void assertLinkPresent(string $url)
public void assertLinkPresentWithText(string $text)
public void assertFormPresent(string $name= null)
public void assertTitleEquals($title)

导航

要跟踪页面内的链接,Web 测试可以使用点击方法

protected void clickLink(string $id);
protected void clickLinkWithText(string $text);

表单

要处理表单,可以使用 getForm() 方法

use unittest\web\{WebTestCase, Webtest};
use unittest\Test;

#[Webtest(url: 'https://github.com/')]
class GitHubTestCase extends WebTestCase {

  #[Test]
  public function search_for() {
    $this->beginAt('/');
    $form= $this->getForm();
    $form->getField('q')->setValue('XP Framework');
    $form->submit();
    $this->assertStatus(200);
    $this->assertTitleEquals('Search · XP Framework · GitHub');
  }
}

另请参阅

xp-framework/rfc#169