Pylons / Pylons/webtest

Invalid value for unchecked checkboxes

Open
#165 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
346
Forks
113
PR merge metrics
No merged PRs in 30d

Description

Using webtest.forms to check the forms generated by my views, the value of a checkbox is None when the checkbox is not checked.

Here is a very simple reproducer.

First, create a checkboxes folder, with acheckboxes/__init__.py file in it, containing the following:

from pyramid.config import Configurator


def home_view(request):
    return {'active': 'id1'}


def main(global_config, **settings):
    config = Configurator(settings=settings)
    config.include('pyramid_jinja2')

    config.add_route('home', '/')
    config.add_view(
        '.home_view', route_name='home',
        renderer='home.jinja2')

    return config.make_wsgi_app()


if __name__ == '__main__':
    from webtest import TestApp

    app = TestApp(main({}, **{}))
    response = app.get('/')

    form = response.forms['home-form']
    checkboxes = form.fields['checkboxes']

    assert checkboxes[0].id == 'id1'
    assert checkboxes[0].checked == True
    assert checkboxes[0].value == 'value1'

    assert checkboxes[1].id == 'id2'
    assert checkboxes[1].checked == False
    assert checkboxes[1].value == 'value2'

In addition, create the checkboxes/home.jinja2 template file, containing the following:

<html>
  <body>
    <form id="home-form">
      <ul>
        <li><input type="checkbox" id="id1" name="checkboxes" value="value1" {% if active == 'id1' %}checked="checked"{% endif %}></li>
        <li><input type="checkbox" id="id2" name="checkboxes" value="value2" {% if active == 'id2' %}checked="checked"{% endif %}></li>
      </ul>
  </body>
</html>

Install pyramid, pyramid_jinja2 and webtest, then run python checkboxes/__init__.py:

$ python checkboxes/__init__.py 
Traceback (most recent call last):
  File "checkboxes/__init__.py", line 35, in <module>
    assert checkboxes[1].value == 'value2'
AssertionError

Why is the checkbox value not what is set in the DOM? This makes testing harder.

We found that checkboxes[1]._value == 'value2' which is the correct value. But using private attributes doesn't feel right.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with checkboxes/init.py and checkboxes/home.jinja2, then trace how webtest.forms exposes checkbox values. Compare the public value with the private _value shown in the report. Done means an unchecked checkbox exposes its DOM value through the public API and the reproducer assertions pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.