[英]Is there a way to set a cookie during a test using PHPUnit_Extensions_SeleniumTestCase?
我有一个应用程序,该应用程序通过设置为我的应用程序域的cookie使用外部服务。 在开发过程中,我手动创建此cookie,但在生产中,此cookie将通过登录生成。 在为此外部服务运行测试之前,是否可以使用在开发过程中设置的cookie?
我猜想我可以使用curl来实现一点自动化,但是我想知道我是否在PHPUnit和/或Selenium中缺少了一些隐藏的功能或技术。
[扩展PHPUnit_Extensions_SeleniumTestCase的类 ]
/**
* Can get the current authenticated user.
*/
public function testCanGetTheCurrentAuthenticatedUser()
{
$this->open('http://my/local/virtual/host/api/getCurrentUser');
$json = json_decode($this->getBodyText());
$this->assertEquals('25', $json->response->id);
}
最终使用Curl进行了需要cookie身份验证的测试。
$curl = curl_init('http://my/local/virtual/host/api/getCurrentUser');
curl_setopt($curl, CURLOPT_COOKIE, 'mycookie=authentication');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
$json = json_decode($output);
$this->assertEquals('25', $json->response->id);
Selenium文档具有createCookie()
,并且在PHPUnit_Extensions_SeleniumTestCase
中将其列为方法。 在文档中,您可以使用以下命令将login
cookie设置为frank.wilson
五分钟。
$this->createCookie('login=frank.wilson', 'max_age=600');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.