oscarmlage oscarmlage

Python, Zombie, forms, tables and tests

Written by oscarmlage on

Sometimes you feel that the world is against you, sometimes it's a computer what makes your life miserable. This time was a cocktail of elements. Trying to test a simple form functionally from python-zombie was like a nightmare.

I'm working testing a project using node v0.8.23, npm 1.4.4, zombie 1.4.1 and python-zombie 0.1.0a5. The template I'm trying to render while testing has a code like:

<form action="custom/url/delete" method="POST">
    <button>Delete</button>
</form>

The test I'm trying to run is:

from zombie import Browser
...
def test_remove_category(self):
    browser = self.browser
    ...
    # Go to the edit link
    browser.clickLink('a[href$="/edit"]')
    import pdb; pdb.set_trace()
    browser.pressButton('form[action$=delete] button[type=submit]')
    ...

The problem is the pressButton line (ignore the pdb for a moment), it's trying to press the button inside the form but the test is failing raising an error like:

NodeError: Error: No BUTTON 'form[action$=delete] button[type=submit]'

Trying to debug the problem - now is where pdb gets the focus - I got that the form was not nicely rendered:

(Pdb) print(browser.html())
....
<form method="POST" action="custom/url/delete"></form>
    <button type="submit" name="submit">Delete</button>

After an intense testing + browsing + reading task I've realized wtf was happening, taking a look to the template code we have the form inside atable, as child of a <tbody> element, something like:

&lt;table&gt;
 &lt;thead&gt;
  ...
 &lt;/thead&gt;
 &lt;tbody&gt;
  &lt;tal:r tal:repeat="q context.values()"&gt;
   &lt;tr&gt;...&lt;/tr&gt;
   &lt;div&gt;&lt;form&gt;...&lt;/form&gt;&lt;/div&gt;
  &lt;/tal:r&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

But a form is not allowed to be a child of table, neither tr or tbody elements so I think it was the reason why it was not nicely rendered by zombie.

After all it was no problem of zombie, the template was weird and, we've to be honest... it was funny and a great lucky to see this failing test.