我目前正在为我的 Symfony 项目编写测试,我没有遇到像 WebTestCase 这样的常规测试的任何问题。但是现在我对其中一些使用 Panther,当我尝试一次启动多个 panther 测试时遇到错误。第一个总是正常工作,但下一个会出现奇怪的错误,具体取决于我如何尝试安排它们,我无法找到出口。
这是我的代码,我已将我所有的 panther 测试重新组合在同一个类中。
namespace App\Tests;
use App\Entity\User;
use App\Repository\TrainingRepository;
use App\Repository\QuestionRepository;
use Symfony\Component\Panther\PantherTestCase;
class PantherTest extends PantherTestCase
{
public function testCreateTrainingOneUser(): void
{
$client = static::createPantherClient();
$crawler = $client->request('GET', '/');
$client->submitForm('Sign in', [
'email' => 'test@test.com',
'password' => 'toto']);
$crawler = $client->request('GET', '/dashboard/training/create_training');
$this->assertSelectorTextContains('.h3', 'Créer une formation');
$client->executeScript("document.querySelector('#add_user_button').click()");
$client->waitFor('#create_training_users_0_email');
$client->submitForm('Create', [
"create_training[title]" => 'test',
"create_training[description]" => 'un test',
"create_training[image_url]" => 'image_url',
"create_training[users][0][email]" => 1,
"create_training[checklist][]" => 'option1',
]);
$trainingRepository = static::$container->get(TrainingRepository::class);
$testTraining = $trainingRepository->findOneBy(['title' => 'test']);
$this->assertNotEmpty($testTraining);
}
public function testCreateQuestion(): void
{
$client = static::createPantherClient();
$crawler = $client->request('GET', '/');
$client->submitForm('Sign in', [
'email' => 'test@test.com',
'password' => 'toto']);
$crawler = $client->request('GET', '/dashboard/training/1/qcm/1/create_question');
$this->assertSelectorTextContains('.h3', 'Créer une nouvelle question');
$client->executeScript("document.querySelector('.answer-add').click()");
$client->waitFor('#create_question_answers_0_label');
$client->submitForm('Créer', [
"create_question[label]" => 'test',
"create_question[hint]" => 'un test',
"create_question[type]" => 1,
"create_question[solution]" => 'le test',
"create_question[associated_chapters]" => 'test1',
"create_question[state]" => 'en cours',
"create_question[answers][0][label]" => 'réponse test',
"create_question[answers][0][is_right]" => true,
"create_question[answers][0][position]" => 1,
]);
$questionRepository = static::$container->get(QuestionRepository::class);
$testQuestion = $questionRepository->findOneBy(['label' => 'test', 'qcm' => 1]);
$this->assertNotEmpty($testQuestion);
}
}
这两个测试在评论另一个时启动时都非常有效,但是如果它们都“活动”,则只有第一个成功,并且我在当前代码设置中遇到此错误。
✘ Create question
┐
├ InvalidArgumentException: The current node list is empty.
│
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/symfony/panther/src/DomCrawler/Crawler.php:396
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/symfony/panther/src/DomCrawler/Crawler.php:308
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/symfony/browser-kit/AbstractBrowser.php:323
╵ /home/simon/Tek3/Stage/PhysioAc_Login/tests/PantherTest.php:45
第 45 行是这个吗
44- $client->submitForm('Sign in', [
45- 'email' => 'test@test.com',
46- 'password' => 'toto']);
由于我们的网站使用身份验证,我目前必须使用提交表单方法登录,因为 WebTestCase 可用的 loginUser 不适用于 Panther。
更新1: 正如第一条评论所建议的那样,“会话”可能已经登录。经过一个小测试,他确实是这种情况
public function testCreateQuestion(): void
{
$client = static::createPantherClient();
$crawler = $client->request('GET', '/dashboard/training/1/qcm/1/create_question');
$this->assertSelectorTextContains('.h3', 'Créer une nouvelle question');
/*$client->executeScript("document.querySelector('.answer-add').click()");
$client->waitFor('#create_question_answers_0_label');
$client->submitForm('Créer', [
"create_question[label]" => 'test',
"create_question[hint]" => 'un test',
"create_question[type]" => 1,
"create_question[solution]" => 'le test',
"create_question[associated_chapters]" => 'test1',
"create_question[state]" => 'en cours',
"create_question[answers][0][label]" => 'réponse test',
"create_question[answers][0][is_right]" => true,
"create_question[answers][0][position]" => 1,
]);
$questionRepository = static::$container->get(QuestionRepository::class);
$testQuestion = $questionRepository->findOneBy(['label' => 'test', 'qcm' => 1]);
$this->assertNotEmpty($testQuestion);*/
}
这是我新的第二次测试,这个断言有效,表明我仍然被认为是登录的并且可以自由导航。但是现在如果我取消对测试结束的注释,则需要很长时间才能完成(注释代码大约需要 1 分钟,没有注释代码大约需要 2 分钟)
如果没有评论,我会收到以下错误
✘ Create question
┐
├ Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session/fb755275a1734b52965592ca0765e4c4/element/7539b385-285d-4d86-ac88-502835620960/click
├
├ Operation timed out after 30001 milliseconds with 0 bytes received
│
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:332
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:591
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/php-webdriver/webdriver/lib/Remote/RemoteExecuteMethod.php:27
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebElement.php:76
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/symfony/panther/src/Client.php:244
╵ /home/simon/Tek3/Stage/PhysioAc_Login/vendor/symfony/browser-kit/AbstractBrowser.php:325
╵ /home/simon/Tek3/Stage/PhysioAc_Login/tests/PantherTest.php:50
第 50 行是 sumbitForm() 方法之后的行。