问题描述
我目前正在为我的 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','/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','Créer une nouvelle question');
/*$client->executeScript("document.querySelector('.answer-add').click()");
$client->waitFor('#create_question_answers_0_label');
$client->submitForm('Créer','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() 方法之后的那一行。
解决方法
经过我所有的研究,我自己得到了一个答案。
我只需要在每次测试开始时调用以下行以避免出现问题。
self::stopWebServer();
由于 Panther 客户端是异步进程,我认为它会很快导致非常奇怪的问题,而且这样做更简单。