HTML form submit fields can have a formactionattribute which alters the endpoint a form is submitted to
Currently in webtest, a submit field's formaction attribute is ignored - the form is always submitted to its action
This PR
Would add support for the formaction attribute, so that when a form is submitted (via its .submit() method) we:
Check if the specified submit field has a formaction attribute
If found, submit the test request to this endpoint
Otherwise just default to just using self.action
Implementation:
Adding formaction support means we need to isolate the submit field and from that retrieve the formaction
Currently the submit field is only held in memory during a for loop that involves all form fields, in the submit_fields() method
An option here would be to mutate the form's action attribute during the for loop, but this felt a bit implicit and potentially unexpected - the current method has no other side-effects, and a form's action isn't the same as one of its submit button's formactions
The other option I see, which I have suggested in this PR, is to split the logic in submit_fields into 2 parts: a _get_submit_field() method and a shortened submit_fields() method
_get_submit_field() is then used:
In the refactored submit_fields() method, to add the submit field's value to the list of field values to submit
A new _get_action() method, which returns the submit field's formaction or just the form's action
The location to submit the form to is then set from _get_action()
Ideally _get_submit_field() would only be called once (within submit() probably) as it involves looping through all the form fields. However this would require passing the submit field to submit_fields() which is infeasible as it would require a signature change to a public method
Context
formaction
attribute which alters the endpoint a form is submitted towebtest
, a submit field'sformaction
attribute is ignored - the form is always submitted to itsaction
This PR
formaction
attribute, so that when a form is submitted (via its.submit()
method) we:self.action
Implementation:
formaction
support means we need to isolate the submit field and from that retrieve theformaction
submit_fields()
methodaction
attribute during the for loop, but this felt a bit implicit and potentially unexpected - the current method has no other side-effects, and a form'saction
isn't the same as one of its submit button'sformaction
ssubmit_fields
into 2 parts: a_get_submit_field()
method and a shortenedsubmit_fields()
method_get_submit_field()
is then used:submit_fields()
method, to add the submit field'svalue
to the list of field values to submit_get_action()
method, which returns the submit field's formaction or just the form's action_get_action()
_get_submit_field()
would only be called once (withinsubmit()
probably) as it involves looping through all the form fields. However this would require passing the submit field tosubmit_fields()
which is infeasible as it would require a signature change to a public method