symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.91k stars 213 forks source link

cannot submit form #541

Closed arderyp closed 2 years ago

arderyp commented 2 years ago

I am having a very basic problem where I am unable to submit a form. I must be doing something wrong, but I am new to this, so perhaps I'm missing something that will be obvious to you all.

I have a page with links on it. When you click one specific link, a modal pops up. In this modal is a simple form with a single text field ("comment") and a submit button ("Submit Comment").

I have tried every itteration I can find in the documentation and StackOverflow for how to submit this form. I do believe it is a JavaScript submit action, but for some reason unknown to me, the commonly suggested fix:

$this->client->executeScript("document.querySelector('#SubmitComment').click()");

only seems to actually work when the form is empty. If I execute the above in non-headless mode, without filling out the form, I see that the button is selected and clicked and the front end form validation error pops up saying "You must fill out the Comment field". However, if I simply try to fill out that form field before clicking via JS, nothing happens at all. That is, I see in the non-headless mode that the form fields are being filled out properly, but the submit never actually executes the ajax and thus, nothing happens

// this seems to work, because it triggers the front end form validation error whe executed against and empty form
$this->client->executeScript("document.querySelector('#SubmitComment').click()");

// this does not work
$form = $crawler->selectButton("Submit Comment")->form();
$form->setValues(["comment" => "This is a PHPUNIT comment"]);
$this->client->executeScript("document.querySelector('#SubmitComment').click()");

// this does not work
$form = $crawler->selectButton("Submit Comment")->form();
$form->setValues(["comment" => "This is a PHPUNIT comment"]);
$this->client->executeScript("$('#SubmitComment').click()");  // This works in a browser console when I execute it manually

// this does not work
$crawler->filter("#comment-input-field")->sendKeys("This is a test");
$this->client->executeScript("document.querySelector('#SubmitComment').click()");

// this does not work
$form = $crawler->filter("#comment-form")->form();
$form->setValues(["comment" => "This is a PHPUNIT comment"]);
$this->client->submit($form);

// this does not work
$this->client->submitForm("Submit Comment", [
    "comment" => "This is a PHPUNIT comment",
]);

// combination of form submit and JS submit also don't work, example
$this->client->submitForm("Submit Comment", [
    "comment" => "This is a PHPUNIT comment",
]);
$this->client->executeScript("$('#SubmitComment').click()");

I've tried hitting the main landing page, clicking the link to open the modal, then submitting the form (everything appears correct in non-headless, except the form doesn't submit). I've also tried to hit the modal url directly, thus submitting the form on a regular HTML page, which works in a browser. Again, everything appears correct in non-headless, until the form submission, when nothing happens. I am getting no errors or exceptions.

I think I'm hitting a brick wall and not sure what else to test. Does anyone else have suggestions?

arderyp commented 2 years ago

I don't have a fix, but I'm closing this as I am using a new approach and don't have the bandwidth to keep testing this.