Pylons / webtest

Wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server.
https://docs.pylonsproject.org/projects/webtest/en/latest/
Other
336 stars 110 forks source link

Can't test HTML5 forms with buttons #175

Closed oz123 closed 7 years ago

oz123 commented 7 years ago

This have been discussed before, and I really wish this would be possible.

I have a form which looks like this:

      <form class="form-horizontal" action="/create_bundle/">
        <div class="table-responsive">
          <table class="table table-bordered table-striped table-highlight">
            <thead>
             <tr>
               <th>Serial</th><th>Site</th>
             </tr>
            </thead>
            <tbody>

                <tr>
                    <td>abcdef123</td>
                    <td>
                        <select class="form-control" name="site">
                            <option>lünen</option>
                        </select>
                    </td>
              <td title="click to configure this device..." align="center">
                      <button type="submit" class="btn btn-success" name="serial" value="abcdef123">
                       <i class="fa fa-wrench" aria-hidden="true"></i>
                      </button>
                    </td>
                         <td title="click to remove this device from the pending list..." align="center">
                             <button type="submit" formaction="/delete/" class="btn btn-danger" id="del1" name="serial" value="abcdef123">
                        <i class="fa fa-trash" aria-hidden="true"></i>
                      </button>
                    </td>
                </tr>

            </tbody>
          </table>
        </div>
      <input type="hidden" name="csrf_token" value="...asd.a...asdasd...">
</form>

I can't click this button ... but it is found with BeatifulSoup:

-> deleted = res.clickbutton(buttonid="del1")
(Pdb++) res.clickbutton(buttonid="del1")
*** IndexError: No matching elements found (from 0 possible)
(Pdb++) res.html.find(id="del1")
<button class="btn btn-danger" formaction="/delete/" id="del1" name="serial" type="submit" value="abcdef123">
<i aria-hidden="true" class="fa fa-trash"></i>
</button>

Is there any possible work around this?

digitalresistor commented 7 years ago

Just reading through your bug report: In your HTML it is del_1 yet in your Pdb++ example it is del1... is that an expected inconsistency?

digitalresistor commented 7 years ago

Just a note that webtest's clickbutton API states the following:

Like click(), except looks for link-like buttons. This kind of button should look like <button onclick="...location.href='url'...">.

http://docs.pylonsproject.org/projects/webtest/en/latest/api.html#webtest.response.TestResponse.clickbutton

It doesn't look like clickbutton is for submitting forms or other actions.

oz123 commented 7 years ago

@bertjwregeer as for the inconsistency, that is not the problem. I fixed the code. In the beginning, I tried with _ and also without it. That is why the inconsistency (I copied examples from two different terminal).

As for onclick="...location.href='url'... The documentation here is not clear to me. I have hardly seen examples in HTML that use that syntax. Can you maybe post a concrete example?

In the mean while, I am doing the following to click that button:

    res = res.goto("/delete/", params={"serial": "abcdef123",
                                       res.html.form.input['name']:
                                       res.html.form.input['value']})
digitalresistor commented 7 years ago

@oz123 clickbutton works on code that looks like this:

<button onclick="window.location.href='http://google.com/';">Go to Google</button>

It's similar to click which follows this:

<a href="http://google.com/">Go to Google</a>

Except happens to use a button tag and thus uses onclick.

gawel commented 7 years ago

You should use .submit('name_of_the_button')

But webtest do not support html5 forms (it is (was?) really hard https://github.com/Pylons/webtest/issues/8) so your formaction will be ignored

oz123 commented 7 years ago

@gawel if formaction is ignored it will submit the whole form I am afraid, and I would not want that. I think, app.get is my only solution here. As much as I appreciate WebTest, I think it is time for me to look for a more comprehensive solution (I am hitting the limits of what I can do with webtest too many times ...).

digitalresistor commented 7 years ago

@oz123 You would be more looking at doing testing using Selenium with a testing framework against a real browser...

There are a bunch of libraries out there to accomplish what you want which will allow much more thorough testing of site behaviour, especially once you also start adding JavaScript.

oz123 commented 7 years ago

@bertjwregeer, yes. I really hated selenium back than when I used it, so I settled for WebTest for it's simplicity and elegance. Lately, I bumped into https://github.com/cobrateam/splinter, which seems to make selenium nicer to use. In my next application I will look into it.