我试图使用 PHPUnit 测试 HTTP 403 错误。我检查了 PHPUnit Selenium 扩展代码,发现驱动程序在收到错误响应后立即结束会话,并且似乎没有绕过此功能的方法。
我最终使用了 try-catch 解决方案并重新启动了 Selenium 会话:
try {
$this->open('restricted_url');
$this->assertTitle('The page cannot be displayed');
} catch (PHPUnit_Framework_Exception $e) {
$this->start();
}
当 Selenium 会话重新启动时,所有会话信息都会丢失,正如预期的那样,因此您需要再次构建会话以运行更多测试:
$this->loginAsGuest();
try {
$this->open('admin_url');
$this->assertTitle('The page cannot be displayed');
} catch (PHPUnit_Framework_Exception $e) {
$this->start();
$this->loginAsGuest();
}