Skip to content

Commit

Permalink
Possible fix for a 'selectOption' issue (#408)
Browse files Browse the repository at this point in the history
* No longer cycles through different options - just selects the first one.

* Fixed to pass existing tests and added a new test requiring changes.

* Tidied up the new test.

* More tidying of new test.
  • Loading branch information
robrkerr authored and DavertMik committed Feb 25, 2017
1 parent 4b5c8ed commit 2cee168
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/helper/Nightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,12 @@ class Nightmare extends Helper {
el = codeceptjs.fetchElement(el);
let found = document.evaluate(locator, el, null, 5);
var current = null;
var items = [];
while (current = found.iterateNext()) {
items.push(current);
}
for (var i = 0; i < items.length; items++) {
current = items[i];
current.selected = true;
var event = document.createEvent('HTMLEvents');
if (!el.multiple) el.value = current.value;
Expand Down
29 changes: 29 additions & 0 deletions test/data/app/view/form/select_onchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<body>
<form action="/form/complex" method="POST">
<label>
<div>Select a value:</div>
<div>
<select name="select" id="select">
<option value=""></option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
</div>
</label>
<input id="submit" disabled="disabled" type="submit" value="Submit" />
</form>
<script>
document.getElementById('select').addEventListener('change', function() {
var submit = document.getElementById('submit');
if (this.value === "") {
submit.setAttributeNode("disabled", "disabled");
} else {
var disabled = submit.getAttributeNode("disabled");
submit.removeAttributeNode(disabled);
}
});
</script>
</body>
</html>
7 changes: 7 additions & 0 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ module.exports.tests = function() {
return assert.equal(formContents('age'), 'adult');
});

it('should select option by label and option text - with an onchange callback', function*() {
yield I.amOnPage('/form/select_onchange');
yield I.selectOption('Select a value', 'Option 2');
yield I.click('Submit');
return assert.equal(formContents('select'), 'option2');
});

it('should select multiple options', function*() {
yield I.amOnPage('/form/select_multiple');
yield I.selectOption('What do you like the most?', ['Play Video Games', 'Have Sex']);
Expand Down

0 comments on commit 2cee168

Please sign in to comment.