selenium - Symfony,PHPUnit:客户端 Webdriver 身份验证
问题描述
我需要对我的 WebDriver 客户端进行身份验证以进行功能测试。
例如,在我的集成测试中,我正在做类似的事情:
namespace Tests\Controller;
use App\Entity\Donor;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use SebastianBergmann\Type\RuntimeException;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class DonorTest extends WebTestCase
{
private static $client;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var SchemaTool
*/
private $schemaTool;
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
static::ensureKernelShutdown();
if (!self::$client) {
self::$client = static::createClient([], [
'PHP_AUTH_USER' => 'Same Old User',
'PHP_AUTH_PW' => 'Same Old Password',
]);
}
$this->entityManager = self::bootKernel()
->getContainer()
->get('doctrine')
->getManager();
$this->schemaTool = new SchemaTool($this->entityManager);
/** Safeguard */
$connection = $this->entityManager->getConnection()->getParams();
if ($connection['driver'] != 'pdo_sqlite' || $connection['path'] != '/tmp/test_db.sqlite') {
throw new RuntimeException('Wrong database, darling ! Please set-up your testing database correctly. See /config/packages/test/doctrine.yaml and /tests/README.md');
}
}
我只是在参数中传递凭据,它可以工作。
但是,在我的功能测试中,我使用的是 WebDriver。它不接受参数中的凭据:
<?php
namespace App\Tests\Functional\Entities\Donor;
use App\Entity\Donor;
use App\Tests\Functional\Helpers\Carrier\CarrierHelper;
use App\Tests\Functional\Helpers\Donor\DonorHelper;
use Doctrine\ORM\EntityManager;
use Facebook\WebDriver\WebDriverBy;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\Client;
class DonorTest extends PantherTestCase
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var CarrierHelper
*/
private $helper;
/**
* @var Client
*/
private $client;
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->entityManager = self::bootKernel()
->getContainer()
->get('doctrine')
->getManager();
$this->helper = new DonorHelper();
}
public static function setUpBeforeClass(): void
{
// Do something
}
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->client = Client::createChromeClient();
$this->client->manage()->window()->maximize();
}
我无法在createChromeClient()
方法中传递任何登录参数。我想我必须在 cookieJar 或令牌中使用 cookie,但我不知道怎么做。
随意问我我的 ahtentication 方法,但我遵循了文档: https ://symfony.com/doc/current/security/form_login_setup.html
编辑
我刚刚尝试了别的东西。使用我的浏览器登录,生成一个 cookie,并尝试用相同的 PHPSESSID 手工制作另一个
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->client = Client::createChromeClient();
$this->client->manage()->window()->maximize();
$cookie = new Cookie('PHPSESSID', 'pafvg5nommcooa60q14nqhool0');
$cookie->setDomain('127.0.0.1');
$cookie->setHttpOnly(true);
$cookie->setSecure(false);
$cookie->setPath('/');
$this->client->manage()->addCookie($cookie);
}
但是得到这个错误:
Facebook\WebDriver\Exception\InvalidCookieDomainException:无效的 cookie 域
域名很好,和我的网络浏览器一样。随着调查的进展,我会更新。
编辑 2
好的,我知道了。
根据这个线程:无法在 Selenium Webdriver 中设置 cookie
要设置 cookie['domain'],您必须首先在域上请求,然后设置 cookie...
所以,这几乎可以工作:
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->client = Client::createChromeClient();
$this->client->manage()->window()->maximize();
$this->client->request('GET', 'http://127.0.0.1/randompage');
$handcookie = Cookie::createFromArray([
'name' => 'PHPSESSID',
'value' => 'pcvbf3sjlla16rfb1b1274qk01',
'domain' => '127.0.0.1',
'path' => '/'
]);
$this->client->manage()->addCookie($handcookie);
}
下一步:找到一种生成永久 cookie 的方法,没有生命周期。我想没有人会读这个,但我会更新它以防其他人被卡住。
解决方案
推荐阅读
- python - 无法从云存储发生变化时触发的 Cloud 函数触发 composer/airflow dag
- javascript - 如何使用 Google App Script 过滤 gmail 中的西里尔字母
- javascript - 如何从目录加载随机文件
- javascript - 从 AJAX 插入记录
- python - Display Django Templates after sucessfuly posting data
- java - 为什么在 Java 中使用扫描器类和分隔符获取双精度时会得到错误的数学结果?
- python - 如何在以另一个列表的一个元素开头的行列表中查找特定行?
- c - 创建后序二叉树数组的函数
- discord - 会员计数频道 discord.py
- pyspark - 我可以在 pyspark forEach 中使用协程函数吗?