myk*_*iwi 6 selenium symfony behat mink
我想创建一个behat定义来使用cookie验证用户.
它的工作原理与贝哈特BrowserKitDriver,当没有@javascript在标签的贝哈特方案.但它并没有与贝哈特工作Selenium2Driver,当存在@javascript标签喜欢这里.
我的定义有什么问题?
/**
* @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());
}
Run Code Online (Sandbox Code Playgroud)
我只想要我的最后一次测试工作.
我不知道我是否清楚......如果不是,请问.
如果您可以使用修复程序执行Pull Request,那将是完美的.
Sam*_*ent -1
这是我用来自动验证用户身份的代码。这使我节省了大量时间(无论如何都应该在另一个功能文件中进行登录测试):
将示例场景放入 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"
Run Code Online (Sandbox Code Playgroud)
/**
* @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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |