Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crawler is detached from the form fill helpers #138

Open
norkunas opened this issue Dec 14, 2023 · 7 comments
Open

Crawler is detached from the form fill helpers #138

norkunas opened this issue Dec 14, 2023 · 7 comments

Comments

@norkunas
Copy link
Contributor

norkunas commented Dec 14, 2023

I'm having problems with selectFieldOption when it is dynamic. It's impossible to select a value that is added via JS.

So I'm trying to add it dynamically and then select it, but the thing is that in ->use(function (Crawler $crawler) callback what I modify via crawler is not trully sent as form data.

In symfony/ux#1334 it was suggested to use ChoiceFormField::addChoice($node) but that doesn't work with non-multiple select element. So I though I will do this:

            ->use(static function (Crawler $crawler) {
                $form = $crawler->selectButton('Submit')->form();
                $stage = $form->get('create[items][0][stage]');
                self::assertInstanceOf(ChoiceFormField::class, $stage);
                $stage->disableValidation();
                $stage->select('cf97d7fe-3222-472a-a1c3-ed9cdd058080');
            })

But probably the crawler does not modify Mink's document? Could we have a helper method to add dynamic values for this case?

@kbond
Copy link
Member

kbond commented Dec 14, 2023

It's impossible to select a value that is added via JS.

Are you using the PantherBrowser then? Panther's crawler should allow this 🤔

@norkunas
Copy link
Contributor Author

In this case not and would like not to

@kbond
Copy link
Member

kbond commented Dec 15, 2023

Ok, yeah, this is currently a problem with KernelBrowser. The mink browser kit driver keeps track of the form fields in memory.

I'm fairly certain #77 #125 will allow this (or could be added easily). I'm hesitant to pull the trigger on that PR, while it should be BC, I'm worried about edge cases I haven't yet discovered. I'm thinking I might include this PR in a new major version of this package.

@norkunas
Copy link
Contributor Author

norkunas commented Dec 9, 2024

So is it possible to do this after #125 ? As I'm writing tests again and again for autocomplete to remember that it won't work :D

@kbond
Copy link
Member

kbond commented Dec 13, 2024

@norkunas, could you try upgrading your app to 2.x-dev and see?

At SFCon, several people told me there are on 2.x already which makes me more confident tagging a release.

@norkunas
Copy link
Contributor Author

norkunas commented Dec 17, 2024

With 2.x I started to get:

Zenstruck\Dom\Exception\RuntimeException: Node with selector "auto:==:user_work_information_update[spokenLanguages][0][language]" is not a fillable form field.

/var/www/html/vendor/zenstruck/browser/src/Browser.php:261

Oh ffs 🤦‍♂️ i've accidentally used fillField instead of selectField and for some reason previously it worked 😂

@norkunas
Copy link
Contributor Author

norkunas commented Dec 17, 2024

Test case

->use(function (KernelBrowser $browser) {
    $skills = $browser->crawler()->selectButton('Submit')->form()->get('user_work_information_update[skills]');
    self::assertInstanceOf(ChoiceFormField::class, $skills);
    $skills->disableValidation();

    $el1 = new \DOMElement('option');
    $el1->setAttribute('value', '01935237-ec29-7271-b522-f56cf050dcdb');
    $el2 = new \DOMElement('option');
    $el2->setAttribute('value', '01935237-ff0e-7bdf-b2fa-bbcabe79c48d');
    $skills->addChoice($el1);
    $skills->addChoice($el2);
})
->selectFieldOptions('user_work_information_update[skills][]', [
    '01935237-ec29-7271-b522-f56cf050dcdb',
    '01935237-ff0e-7bdf-b2fa-bbcabe79c48d',
])

Result:

1) Zenstruck\Dom\Exception\RuntimeException: Could not find option with value/text "01935237-ec29-7271-b522-f56cf050dcdb".

/var/www/html/vendor/zenstruck/dom/src/Dom/Node/Form/Field/Select/Multiselect.php:66
/var/www/html/vendor/zenstruck/browser/src/Browser.php:338
/var/www/html/vendor/zenstruck/browser/src/Browser.php:361

Note: I've also tried to do:

$skills->disableValidation()->setValue([
    '01935237-ec29-7271-b522-f56cf050dcdb',
    '01935237-ff0e-7bdf-b2fa-bbcabe79c48d',
]);

then there is no exception, but for some reason backend does not get any submitted values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants