【问题标题】:Is there a way to set a cookie during a test using PHPUnit_Extensions_SeleniumTestCase?有没有办法在使用 PHPUnit_Extensions_SeleniumTestCase 的测试期间设置 cookie?
【发布时间】:2012-02-20 12:57:33
【问题描述】:

我有一个应用程序通过设置到我的应用程序域的 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); 

}

【问题讨论】:

    标签: unit-testing selenium phpunit


    【解决方案1】:

    最终使用 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);
    

    【讨论】:

      【解决方案2】:

      Selenium documentationcreateCookie(),它在PHPUnit_Extensions_SeleniumTestCase 中被列为方法。从文档中,您可以使用以下内容将 login cookie 设置为 frank.wilson 5 分钟。

      $this->createCookie('login=frank.wilson', 'max_age=600');
      

      【讨论】:

      • 资料不错,但我的问题还是没有解决。大卫,你有任何使用 createCookie 的示例代码吗?
      • 我还没用过。我们基于 Selenium 的测试非常有限。尝试在createCookie() 之后使用getCookie()getCookieByName() 以查看是否已设置。另一种选择是查看 Selenium 2 是否有效。
      猜你喜欢
      • 1970-01-01
      • 2020-06-12
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多