webdriver.io异步问题 - 单击元素然后输入文本

时间:2015-09-23 20:10:09

标签: javascript selenium promise webdriver-io

我正在努力解释"选择的选择"在我的自动化测试脚本中。我正在使用webdriver.io并参考以下API信息: http://webdriver.io/api.html

我需要点击' a.chosen-single'在"选择" world等于用户点击select。这使用户专注于文本输入(允许用户过滤选择选项,因此选择的原因很酷)然后我需要模拟输入文本的用户。

问题在于我写的脚本导致所有选择的选择被点击,然后键被输入。这意味着它们的文本只会进入最终选择的选择输入。

我点击了元素后插入了一个pause()。我希望在每次单击后都会发生暂停,但是只有在单击最后一个元素并且所有元素的键都在末尾都被键入时才会发生暂停,这样最后一个元素的值为&# 39; 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

我无法弄清楚如何确保在输入击键之前下一个任务不会排队。请帮忙。

2 个答案:

答案 0 :(得分:1)

pause其自我返回一个承诺,因此您应该在then上调用pause以在 pause回调返回后执行

this.pause(1000).then(function() {
  this.keys(fields[selectName])
  //press enter to finalize selection
  //.keys('\uE007')

  console.log('keys pressed');
});

答案 1 :(得分:0)

我终于回答了我自己的问题。问题是我正在使用for(){}循环遍历我想填写的不同字段.for(){}循环执行每个命令而不等待给定的选择选择完成正确设置value(单击选择,键入值,按Enter键)。我通过收集选择器和值,将它们存储在函数中并将每个完整函数添加到队列中来解决这个问题。然后,我一次执行一个函数,在函数之前调用“keys”命令的“then”回调中的下一个函数(因此在继续选择的select上按下回车键后调用下一个函数)。我使用迭代器来获取队列中的每个下一个函数。任何有兴趣的人都可以查看我的代码并评论任何问题的建议。谢谢!

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 + '&currencyId=' + params.currencyId + '&debug=nocache') 
  .then(function(result) {

    fillFields(this, fields);

  });