我试图在测试前设置cookie,但由于某种原因它们没有设置。
下面是我的示例代码:class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
protected function setUp()
{
$this->setBrowser('firefox');
$this->setBrowserUrl('http://dev.local/');
}
public function testTitle()
{
$session = $this->prepareSession();
$session->cookie()->remove('language_version');
$session->cookie()->add('language_version', 'en')->set();
$this->url('/');
$this->assertEquals('Title in English', $this->title());
}
}
有人知道怎么做吗?非常感谢任何帮助。
我在Selenium文档中找到了我的问题的答案:
如果你想在开始与网站互动之前预置cookie,而你的主页很大/需要一段时间才能加载,另一种选择是在网站上找到一个较小的页面,通常404页面很小(http://example.com/some404page)
现在我的测试看起来像这样:
$this->url('/unit_tests.php');
$this->cookie()->remove('language_version');
$this->cookie()->add('language_version', 'en')->set();
$this->url('/');
$this->assertEquals('Title in English', $this->title());
/unit_tests.php
是一个空的PHP文件,允许我初始设置页面的cookie。
这个cookie不应该存在,所以删除会失败。Selenium在每次运行测试套件时使用一个新的空配置文件运行浏览器。.