Skip to content

Commit

Permalink
Merge pull request #443 from underdogio/dev/multi.select.sqwished
Browse files Browse the repository at this point in the history
Added multi-select support to LxmlDriver
  • Loading branch information
andrewsmedina committed Oct 26, 2015
2 parents 6993e85 + 07a487b commit 56bc052
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
26 changes: 20 additions & 6 deletions splinter/driver/lxmldriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def _do_method(self, action, url, data=None):
def visit(self, url):
self._do_method('get', url)

def serialize(self, form):
data = {}
for k, v in form.fields.items():
if v is None:
continue
if isinstance(v, lxml.html.MultipleSelectOptions):
data[k] = [val for val in v]
else:
data[k] = v
for key in form.inputs.keys():
input = form.inputs[key]
if getattr(input, 'type', '') == 'file' and key in data:
data[key] = open(data[key], 'rb')
return data

def submit(self, form):
method = form.attrib.get('method', 'get').lower()
action = form.attrib.get('action', '')
Expand All @@ -45,11 +60,7 @@ def submit(self, form):
else:
url = self._url
self._url = url
data = dict(((k, v) for k, v in form.fields.items() if v is not None))
for key in form.inputs.keys():
input = form.inputs[key]
if getattr(input, 'type', '') == 'file' and key in data:
data[key] = open(data[key], 'rb')
data = self.serialize(form)
self._do_method(method, url, data=data)
return self._response

Expand Down Expand Up @@ -190,7 +201,10 @@ def fill_form(self, field_values):
elif control_type == 'radio':
control.value = value # [option for option in control.options if option == value]
elif control_type == 'select':
control.value = [value]
if isinstance(value, list):
control.value = value
else:
control.value = [value]
else:
# text, textarea, password, tel
control.value = value
Expand Down
5 changes: 5 additions & 0 deletions tests/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ <h1 id="firstheader">Example Last Header</h1>
<option value="celery">Celery</option>
</optgroup>
</select>
<select name="pets" multiple="multiple">
<option value="cat">Cat</option>
<option value="dog">Dog</option>
<option value="fish">Fish</option>
</select>
<label for="description">Description</label>
<textarea rows="3" cols="50" name="description"></textarea>
</form>
Expand Down
7 changes: 7 additions & 0 deletions tests/test_flaskclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def test_attach_file(self):
assert 'text/plain' in html
assert open(file_path, 'rb').read().decode('utf-8') in html

def test_serialize_select_mutiple(self):
"should serialize a select with multiple values into a list"
self.browser.select('pets', ['cat', 'dog'])
form = self.browser.find_by_name('send')._get_parent_form()
data = self.browser.serialize(form)
assert data['pets'] == ['cat', 'dog']

def test_forward_to_none_page(self):
"should not fail when trying to forward to none"
browser = Browser('flask', app=app)
Expand Down

0 comments on commit 56bc052

Please sign in to comment.