【问题标题】:Authentification with HTTP cookie in Selenium using Behat使用 Behat 在 Selenium 中使用 HTTP cookie 进行身份验证
【发布时间】:2018-02-22 13:34:50
【问题描述】:

我想创建一个行为定义来使用 cookie 对用户进行身份验证。

the behat scenario 上没有@javascript 标记时,它适用于Behat BrowserKitDriver。 但它不适用于 Behat Selenium2Driver,当有 @javascript 标签 like here

我用the symfony-demo application for demonstrate my tests

我的定义有什么问题?

/**
 * @Given I am logged in as :username
 */
public function iAmLoggedInAs($username)
{
    $driver = $this->getSession()->getDriver();
    $session = $this->kernel->getContainer()->get('session');
    $user = $this->kernel->getContainer()->get('security.user.provider.concrete.database_users')->loadUserByUsername($username);
    $providerKey = 'secured_area';

    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());
    $session->set('_security_'.$providerKey, serialize($token));
    $session->save();

    if ($driver instanceof BrowserKitDriver) {
        $client = $driver->getClient();
        $cookie = new Cookie($session->getName(), $session->getId());
        $client->getCookieJar()->set($cookie);
    } else if ($driver instanceof Selenium2Driver) {
        $this->visitPath('/');
    } else {
        throw new \Exception('Unsupported Driver');
    }

    $this->getSession()->setCookie($session->getName(), $session->getId());
}

我只希望my last behat test 有效。
我不知道我是否清楚...问如果没有。

如果你能做一个带有修复的拉取请求,那将是完美的。

【问题讨论】:

  • @javascript 启用 javascript 并且 BrowserKitDriver 无法评估 javascript。您收到任何错误吗?
  • 当我设置@javascript时,驱动不是BrowserKitDriver而是Selenium2Driver。没有错误,javascript 已正确执行,但我没有登录到应用程序。
  • 在尝试设置 cookie 之前和之后,您是否尝试过对 cookie 进行回显。此外,在尝试设置 cookie 之前,请确保页面已加载。您是否尝试过@insulated 和@javascript?您的 cookie 是 HttpOnly cookie 吗?如果 cookie 有 HttpOnly 标志,您可能会被禁止通过 JavaScript 设置 cookie 以防止 XSS 攻击。
  • 您是否尝试在访问页面之前设置 cookie?
  • @lauda 我尝试在 FeatureContext.php 中显示 cookie:var_dump($driver->getWebDriverSession()->getAllCookies());,但也在 twig:the result 中。奇怪的是有 2 个 cookie:PHPSESSID 和 MOCKSESSID。 cookie 不是 httpOnly(见结果页面)。

标签: selenium symfony behat mink


【解决方案1】:

你有几种方法

选项 1. 只使用 Mink

  • 如果您使用 Mink,您的上下文将扩展 RawWebContextWebContext,因此您可以使用 getSession() 访问 Mink 会话。

  • 然后,使用setCookie

如您所见,我们很幸运,这就是与 Mink 合作的重点,Cookie Manipulationsupported by many drivers

// ...
$this->visitPath('/');
$this->getSession()->setCookie(
    $session->getName(),
    $session->getId()
);

注 1: 请注意,以下不是一回事:

注意 2:想要查看您的 cookie?

var_dump($driver->getClient()->getCookieJar());

选项 2. 访问 Selenium2 会话

不要犹豫,深入研究代码,看看 Selenium2 WebDriver Session 是如何工作的。

您一定会找到absolute joy and peace of mind

else if ($driver instanceof Selenium2Driver) {
      $this->visitPath('/');
      $cookie =  array(
           "domain" => "", <----- You can even add your domain here 
           "name" => $session->getName(), 
           "value" => $session->getId(),
           "path" => "/",
           "secure" => False
      );
      $driver->getWebDriverSession()->setCookie($cookie);
} 

注意 3: 想要查看您的 cookie?

var_dump($driver->getWebDriverSession()->getAllCookies());

【讨论】:

  • 花哨的答案,但它不起作用......我不敢相信使用 Selenium 驱动程序和 Symfony 的 Behat 测试没有单一的解决方案,这不是一个罕见的设置,它实际上是一个非常常见的设置...
【解决方案2】:

这是我用于自动验证为 USER 的代码。这让我节省了很多时间(无论如何都应该在另一个功能文件中进行登录测试):

将示例场景放入 YOUR_FILE.feature 示例文件:

  Scenario: List Backend Products
    Given I auto authenticate as "YOUR_USER_NAME"
    Then  I go to "http://YOUR_SITE/backend/products"
    Then  I should see "Backend Products Management"

 

 /**
 * @Given /^I auto authenticate as "([^"]*)"$/
 */
public function iAutoAuthenticateAs($userName)
{
    $user = $this->getContainer()->get('fos_user.user_manager')->findUserByUsername($userName);
    $providerKey = $this->getContainer()->getParameter('fos_user.firewall_name');
    $token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());

    $context = $this->getContainer()->get('security.token_storage');
    $session = $this->getContainer()->get('session');

    $context->setToken($token);
    $session->set('_security_'.$providerKey, serialize($token));
    $session->save();

    $this->getSession()->setCookie($session->getName(), $session->getId());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 2019-02-08
    • 2016-05-11
    • 1970-01-01
    • 2021-03-27
    • 2014-07-31
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多