我试图在我的自动化测试脚本中说明“选择的选择”。我正在使用webdriver.io,并参考了以下接口信息:http://webdriver.io/api.html
我需要点击“a.selected-single”,在“select”的世界中,它等同于用户点击了select。这将用户的注意力集中在文本输入上(它允许用户过滤选择选项,因此chosen很酷),然后我需要模拟用户输入文本。
问题是,我编写的脚本导致所有选择-选择被单击,然后键被输入。这意味着他们的文本只被输入到最终的选择-选择输入中。
在单击元素之后,我插入了一个pause()。我希望暂停发生在每次单击之后,但实际上,暂停只发生在最后一个元素被单击时,并且所有元素的键都在最后一起键入,以便最后一个元素的值为'FIL12‘
this.click(container + ' a.chosen-single').then(function(){
console.log('clicked');
console.log('value', fields[selectName]);
this.pause(1000)
this.keys(fields[selectName])
//press enter to finalize selection
//.keys('\uE007')
console.log('keys pressed');
});下面是我在终端中得到的读数:
clicked
value F
keys pressed
clicked
value IL
keys pressed
clicked
value 1
keys pressed
clicked
value 2
keys pressed我不知道如何确保在按键输入之前,下一个任务不会排队。请帮帮忙。
发布于 2015-09-24 04:19:55
pause自身返回一个promise,所以您应该在pause上调用then,以便在pause回调返回之后执行该块。
this.pause(1000).then(function() {
this.keys(fields[selectName])
//press enter to finalize selection
//.keys('\uE007')
console.log('keys pressed');
});发布于 2015-09-26 22:06:23
我终于回答了我自己的问题。问题是我使用了一个for(){}循环来遍历我想要填充的不同字段。for(){}循环正在执行每个命令,而不是等待给定的select设置为正确的值(单击select,键入value,然后按enter)。我通过收集选择器和值,将它们存储在一个函数中,并将每个完整的函数添加到一个队列中,解决了这个问题。然后,我一次一个地执行每个函数,在前面的函数中调用'keys‘命令的" then“回调中的下一个函数(因此在随后选择的select上按下Enter键后调用下一个函数)。我使用迭代器来获取队列中的每个next函数。任何感兴趣的人都可以检查我的代码和评论任何问题的建议。谢谢!
webdriverio = require('webdriverio');
var tester = {};
var options = {
desiredCapabilities: {
browserName: 'chrome'
}
};
var params = {
//editing this out because info is private
};
//changing the fields because info is private
var fields = {
testField: 'John Smith',
testSelect: 'USA'
};
var execQueue = [];
var wrapFunction = function(fn, context, params) {
return function() {
fn.apply(context, params);
};
};
function fillFields(driver, fields){
driver.iter = 0;
driver.elements('select + .chosen-container').then(function(result){
console.log('how many selects', result.value.length);
tester.totalSelects = result.value.length;
});
//loop through all selects and inputs
for(property in fields){
var p = property;
//closure to preserve value of property
(function(p){
//if chosen input then choose from list
driver.isExisting('div.' + p + ' .chosen-results').then(function(result){
if(result === true){
driver.elements('div.' + p + ' select').then(function(result){
//loop through each select (expiration date has two selections in one container)
for(var i=0;i<result.value.length;i++){
var s = result.value[i].ELEMENT;
//closure
(function(s){
//find the name of each select
driver.elementIdAttribute(s,'name').then(function(result){
//find the chosen container after select
var container = 'select[name=' + result.value + '] + .chosen-container';
var selectName = result.value;
//find corresponding a.chosen-single
//this.debug()
//click on a.chosen-single to activate chosen drop
var qfunction = function(link, value){
console.log('#################in qu function ###########', value);
driver.click(link).then(function(){
this.keys([value, '\uE007']).then(function(){
driver.iter++;
execQueue[driver.iter]();
});//end keys.then
});//end click.then
}//end qfunction
execQueue.push(wrapFunction(qfunction, this, [container + ' a.chosen-single', fields[selectName]]));//end push
if(execQueue.length == tester.totalSelects - 1){
console.log('**********equal');
execQueue[driver.iter]();
}//end if equal
console.log('queue so far', execQueue.length);
});//end elementIdAttribute
})(s);//end closure
}//end for selects in container
});//end driver.elements
}else{
driver.addValue('input[name=' + p + ']', fields[p]);
}
})//end driver.isExisting
})(p);
}//end for each field
};//end fillFields
webdriverio
.remote(options)
.init()
.url('https://' + params.domain + '/' + params.clientId + '/?scope=' + params.scope + '&cart=' + params.cart + '&cfg=' + params.cfg + '&progress=' + params.progress + '&language=' + params.language + '¤cyId=' + params.currencyId + '&debug=nocache')
.then(function(result) {
fillFields(this, fields);
});https://stackoverflow.com/questions/32748611
复制相似问题