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

Functional testing: possibility to add form fields #1828

Closed
lowerends opened this issue Apr 1, 2015 · 10 comments
Closed

Functional testing: possibility to add form fields #1828

lowerends opened this issue Apr 1, 2015 · 10 comments
Labels

Comments

@lowerends
Copy link

@lowerends lowerends commented Apr 1, 2015

Would it make sense to add the possibility to add form fields during functional testing, in order to simulate Javascript behaviour?

At this moment, one can fill form fields one-by-one using the fillField() function or fill them all in a formSubmit() function. In a formSubmit() one can also add extra fields in the parameter array that will be submitted, but it's not possible to combine formSubmit() with fillField() to add extra fields to submitted form, as formSubmit() overwrites all form fields that were filled using fillField().

Wouldn't it therefore be useful to have a function like e.g. addField() where you can define a field name and a value and which would also be submitted when clicking the submit button? This could then be used to add extra fields to the form which would be generated by Javascript in a real browser.

@zbateson
Copy link
Member

@zbateson zbateson commented Apr 1, 2015

If I'm not mistaken you should be able to add any random submission field names/values you like with submitForm. Have you tried:

$I->submitForm('#my-form', ['some-non-existent-field' => 'some value']);
@zbateson
Copy link
Member

@zbateson zbateson commented Apr 1, 2015

I assume you're using a framework module. For selenium (WebDriver) tests that would not work.

@lowerends
Copy link
Author

@lowerends lowerends commented Apr 1, 2015

I'm using the Laravel5 framework module indeed.

I did try with submitForm(), but it's overwriting the values that were filled before in the testcase using fillField(). That means that all the form fields need to be included in the parameter array of submitForm(). This is not ideal since I want to make sure that these form fields actually exist on the page, hence fillField() will make sure that they do exist.

@zbateson
Copy link
Member

@zbateson zbateson commented Apr 1, 2015

Yeah, at the moment the two functions (fillField and submitForm) work differently and aren't entirely compatible. I agree it would be better if they were and thought of that myself.

At some point I may get around to it. I rarely use fillField myself and instead use $I->seeElement if I need to check for existence, and always submit with submitForm. Just my preference though.

Feel free to submit a pull request if you have the time to play with it :) in this case the changes would be to src/Lib/InnerBrowser.php

@zbateson zbateson added the ENHANCEMENT label Apr 1, 2015
@lowerends
Copy link
Author

@lowerends lowerends commented Apr 1, 2015

Using seeElement() in combination with submitForm() would indeed assert that the form fields are present, so that may be a good alternative for now.

I had looked at the InnerBrowser.php code already. I thought of modifying the submitForm() function to take fillField() values into account, but how can I retrieve these values within the submitForm() function? Is there some method for that?

@zbateson
Copy link
Member

@zbateson zbateson commented Apr 1, 2015

You could get the values by using the returned form field from a call to matchFormField. submitForm is already pretty large though, so if you're to do it I would recommend creating a separate method that assigns the values to a params array and then merge the arrays.

It may be possible to scrap the loop for default values in submitForm and use the form object returned by getFormFor - if that's possible and it passes all tests, that may be the best option actually.

@lowerends
Copy link
Author

@lowerends lowerends commented Apr 1, 2015

It actually just seems to require one extra line in the submitForm() function:

    $filledValues = $this->getFormFor($form)->getValues();
    $requestParams = array_merge($defaults, $filledValues, $params);

This way, the default parameters that are set in the defaults loop are overwritten by the manually set fillField() values which in turn are again overwritten by the $params. Would you agree?

@zbateson
Copy link
Member

@zbateson zbateson commented Apr 1, 2015

Wow, much easier than expected - can it replace the foreach ($fields as $field) loop entirely I wonder?

@lowerends
Copy link
Author

@lowerends lowerends commented Apr 2, 2015

I did some testing with leaving out the foreach ($fields as $field) loop (both for create and edit forms, with and without pre-filled values) and it does seem to work nice.

@zbateson
Copy link
Member

@zbateson zbateson commented Apr 8, 2015

Hi @lowerends - should be fixed in current now :)

@zbateson zbateson closed this Apr 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.