zenstruck / browser

A fluent interface for your Symfony functional tests.
MIT License
186 stars 17 forks source link

assertFieldEquals does not work with hidden fields #91

Closed bjo3rnf closed 2 years ago

bjo3rnf commented 2 years ago

Hi @kbond,

thank you for this great library, it's a real timesaver.

I'm not sure if this is the expected behavior but I stumbled across the following:

$this
    ->assertFieldEquals('flow_mailMessageCompose_step', '1') // doesn't work, 'Form field not found'
    ->assertElementAttributeContains('input[name="flow_mailMessageCompose_step"]', 'value', '1') // works
;

Thanks again.

Cheers Björn

kbond commented 2 years ago

Hi @bjo3rnf, yes that is sort of expected behaviour. This library uses Mink under the hood and it doesn't let you use fillField()/assertField() with hidden fields.

Using assertElementAttributeContains as you show above is the best way to assert the value.

Do you need to manipulate the hidden field? Currently, the only way would be to create a custom browser class and use some internal stuff.

kbond commented 2 years ago

If you do need to change a hidden field, here's a little method I use in a project:

class AppBrowser extends KernelBrowser
{
    public function fillHiddenField(string $name, string $value): self
    {
        $selector = "input[name=\"{$name}\"]";

        $this->assertSeeElement($selector);
        $this->session()->page()->find('css', $selector)->setValue($value); // note: ->session() is @internal

        return $this;
    }
}
bjo3rnf commented 2 years ago

Hi @kbond,

I don't have to manipulate the fields, but use the assertion to determine the current state. I thought I could simplify/shorten it that way, but that's ok for me.

Thanks again.