I need to solve a little problem of simulation of user's actions on site: select certain <option>
tag in <select>
and then imitate pressing the button "OK". There are several rows with <select>
tags so I decided do it in a for loop with jQuery. But this code works only in the first iteration (but works pretty well - numbers changing and then alert('gooood-goood') shows).
Interestingly, the loop continues and vars 'option' and 'select' assigned just as they should, even alert()
of the second button shows, indicating that event onclick
worked, but <select>
tags in second row remained at zero, and there was no simulate of onclick
event actually. This code seemed to refuse to work in all loops of the cycle, but the first.
The ultimate goal - to simulate the user and solve exactly on JavaScript is not required. I have a dynamically changing parameters in the loop, I think that Selenium IDE may be the answer for me but I need it work with some logic and changed parameters in the loop.
Here two lists: script and html where script should be executed.
<input type="button" id="done" value="C'mon" />
<table>
<tr>
<td id="c10">
<select name="c0">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td id="c11">
<select name="c1">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td id="c12">
<input value="OK" type="button" onclick="alert('gooood-goood')" />
</td>
</tr>
<tr>
<td id="c20">
<select name="c0">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td id="c21">
<select name="c1">
<option value="0" selected>0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</optiaon>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td id="c22">
<input value="OK" type="button" onclick="alert('gooood-goood')" />
</td>
</tr>
</table>
And script then:
function foo() {
alert('inside');
var comb = rand(10, 99) + '', row = 1, col = 0;
for (; row <= 2; row++) {
for (; col <= 1; col++) {
var select = '#c' + row + col + ' select[name=c' + col + ']';
var option = "#c" + row + col + " select option[value=" + comb[col] + "]";
$(select).change(function () {
$(option).prop('selected', true);
});
$(select).change();
if (col == 1) {
var i = col + 1;
$('#c' + row + i + ' input').click();
}
}
col = 0;
comb = rand(10, 99);
}
}
$(function () {
document.getElementById('done').onclick = foo;
});
function rand(min, max) { // Generate a random integer
if (max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
} else {
return Math.floor(Math.random() * (min + 1));
}
}
UPDATE
I don't know what I've done, but it's working now. Same code :/