Skip to content

Commit 2cee168

Browse files
robrkerrDavertMik
authored andcommitted
Possible fix for a 'selectOption' issue (#408)
* 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.
1 parent 4b5c8ed commit 2cee168

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lib/helper/Nightmare.js

+5
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,12 @@ class Nightmare extends Helper {
651651
el = codeceptjs.fetchElement(el);
652652
let found = document.evaluate(locator, el, null, 5);
653653
var current = null;
654+
var items = [];
654655
while (current = found.iterateNext()) {
656+
items.push(current);
657+
}
658+
for (var i = 0; i < items.length; items++) {
659+
current = items[i];
655660
current.selected = true;
656661
var event = document.createEvent('HTMLEvents');
657662
if (!el.multiple) el.value = current.value;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<html>
2+
<body>
3+
<form action="/form/complex" method="POST">
4+
<label>
5+
<div>Select a value:</div>
6+
<div>
7+
<select name="select" id="select">
8+
<option value=""></option>
9+
<option value="option1">Option 1</option>
10+
<option value="option2">Option 2</option>
11+
<option value="option3">Option 3</option>
12+
</select>
13+
</div>
14+
</label>
15+
<input id="submit" disabled="disabled" type="submit" value="Submit" />
16+
</form>
17+
<script>
18+
document.getElementById('select').addEventListener('change', function() {
19+
var submit = document.getElementById('submit');
20+
if (this.value === "") {
21+
submit.setAttributeNode("disabled", "disabled");
22+
} else {
23+
var disabled = submit.getAttributeNode("disabled");
24+
submit.removeAttributeNode(disabled);
25+
}
26+
});
27+
</script>
28+
</body>
29+
</html>

test/helper/webapi.js

+7
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ module.exports.tests = function() {
219219
return assert.equal(formContents('age'), 'adult');
220220
});
221221

222+
it('should select option by label and option text - with an onchange callback', function*() {
223+
yield I.amOnPage('/form/select_onchange');
224+
yield I.selectOption('Select a value', 'Option 2');
225+
yield I.click('Submit');
226+
return assert.equal(formContents('select'), 'option2');
227+
});
228+
222229
it('should select multiple options', function*() {
223230
yield I.amOnPage('/form/select_multiple');
224231
yield I.selectOption('What do you like the most?', ['Play Video Games', 'Have Sex']);

0 commit comments

Comments
 (0)