zopefoundation / zope.testbrowser

Programmable browser for functional black-box tests
Other
21 stars 16 forks source link

Browser._findAllControls() has wrong behaviour with no-name inputs and checkboxes #92

Open vernans opened 4 years ago

vernans commented 4 years ago

Checkboxes become multiple controls that are handled together when they are collected by Browser._findAllControls(). Usually they all have the same name. When there are other fields with the same name – or in a more normal and case multiple inputs with no name – unexpected errors are possible. Has anybody any idea how to fix this? Those tests assert the expected behaviour but fail with the current implementation.

class TestsSameNameAndCheckboxes(unittest.TestCase):
    """Testing weird behaviour regarding checkboxes with the same name."""

    def test_checkbox_controls_with_same_name_1(self):
        """A checkbox without name doesn't interfere with
        throwing a LookupError. In this case, multiple input
        fields are handled together but the checkbox is not
        treated as that because the first input is a button.
        """
        app = QuietTestApp()
        browser = Browser(wsgi_app=app)
        app.set_next_response(b'''\
<html><body>
<form action="." method="post" enctype="multipart/form-data">

    <button type="button">Do Stuff</button>
    <button type="button">Do Other Stuff</button>
    <label for="checkbox">Label</label>
    <input type="checkbox" id="checkbox">
</form></body></html>
        ''')
        browser.open('http://localhost/')
        with self.assertRaises(LookupError):
            browser.getControl('Not There')

    def test_checkbox_controls_with_same_name_2(self):
        """A checkbox is regognized as an ItemControl."""
        app = QuietTestApp()
        browser = Browser(wsgi_app=app)
        app.set_next_response(b'''\
<html><body>
<form action="." method="post" enctype="multipart/form-data">

    <button name="any" type="button">Do Stuff</button>
    <button name="any" type="button">Do Other Stuff</button>
    <label for="checkbox">Label</label>
    <input name="any" type="checkbox" id="checkbox">
</form></body></html>
        ''')
        browser.open('http://localhost/')
        control = browser.getControl('Label')
        assert isinstance(control, ItemControl)

    def test_checkbox_controls_with_same_name_3(self):
        """A checkbox without name doesn't interfere with
        throwing a LookupError. This time it seems to interpret the buttons
        as options of the checkbox."""
        app = QuietTestApp()
        browser = Browser(wsgi_app=app)
        app.set_next_response(b'''\
<html><body>
<form action="." method="post" enctype="multipart/form-data">
    <label for="checkbox">Label</label>
    <input type="checkbox" id="checkbox">
    <button type="button">Do Stuff</button>
    <button type="button">Do Other Stuff</button>
</form></body></html>
        ''')
        browser.open('http://localhost/')
        with self.assertRaises(LookupError):
            browser.getControl('Not There')
mgedmin commented 4 years ago

Failure tracebacks for reference:

Error in test test_checkbox_controls_with_same_name_1 (zope.testbrowser.tests.test_browser.TestsSameNameAndCheckboxes)
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/tests/test_browser.py", line 232, in test_checkbox_controls_with_same_name_1
    browser.getControl('Not There')
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 397, in getControl
    available)
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 1354, in disambiguate
    for choice in available])
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 643, in controlFormTupleRepr
    return wtcontrol.mechRepr()
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 775, in mechRepr
    raise NotImplementedError(str((self, ctrl)))
NotImplementedError: (<Control name='None' type='checkbox'>, <Checkbox name="None" id="checkbox">)
Failure in test test_checkbox_controls_with_same_name_2 (zope.testbrowser.tests.test_browser.TestsSameNameAndCheckboxes)
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/tests/test_browser.py", line 250, in test_checkbox_controls_with_same_name_2
    assert isinstance(control, ItemControl)
AssertionError
Error in test test_checkbox_controls_with_same_name_3 (zope.testbrowser.tests.test_browser.TestsSameNameAndCheckboxes)
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/tests/test_browser.py", line 269, in test_checkbox_controls_with_same_name_3
    browser.getControl('Not There')
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 397, in getControl
    available)
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 1354, in disambiguate
    for choice in available])
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 643, in controlFormTupleRepr
    return wtcontrol.mechRepr()
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 1247, in mechRepr
    if self.selected:
  File "/home/mg/src/zopefoundation/zope.testbrowser/.tox/py27/local/lib/python2.7/site-packages/zope/testbrowser/browser.py", line 1215, in selected
    return self._control.checked
AttributeError: 'Submit' object has no attribute 'checked'