webdriver.io在页面重新加载后继续脚本

时间:2015-09-26 17:05:41

标签: javascript selenium selenium-webdriver webdriver-io

我正在用webdriver.io编写一个自动脚本来填写网页上的输入/选择并截取屏幕截图。如果用户操作(例如更改选择的值)导致页面在自动化脚本的执行过程中加载,该怎么办?有没有办法在脚本中提取你离开的地方(让我们说在那个时候已经填写了一些输入,并且他们通过会话数据保留了它们的价值,但你想继续填写剩下的部分) 。

据我所知,一旦该页面重新加载,脚本就会停止执行。

如果没有办法从上次停止的位置开始,那么如何通过检测页面加载然后重新执行脚本来从头开始触发脚本?

回应评论:我不认为我需要粘贴代码(虽然我会),因为问题实际上就是当页面加载发生在中间时如何通过webdriver.io检测页面加载脚本执行并不是由脚本本身启动的。

UPDATE !!:我设法使用waitForExist()解决这个问题,并在我与之交互之前等待每个输入/选择存在。它并不理想,但它确实有效。如果有人有更好的解决方案,请告诉我们!

以下是代码:

webdriverio = require('webdriverio');

var tester = {};

tester.iter = 0;

var options = {
    desiredCapabilities: {
        browserName: 'chrome'
    }
};

var params = {
  [[I've removed these because it's private info]]
};

var fields = {
  deliveryCompany: 'ABC Inc', 
  deliveryFirstname: 'John', 
  deliveryLastname: 'Smith',
  deliveryStreet1: '123 Main St.',
  deliveryCity: 'Boston',

};

var execQueue = [];


var wrapFunction = function(fn, context, params) {
    return function() {
        fn.apply(context, params);
    };
};

var takeScreenshot = function(){
  console.log('take screenshot', this);
   this.saveScreenshot('./checkout.png')
  .click('input.CBSubmit').then(function(value) {
     this.waitForExist('#mjPaymentReview').then(function(){
        this.saveScreenshot('./review.png').then(function(){
          this.click('input.CBSubmit').then(function(){
            this.waitForExist('#mjCartFinal').then(function(){
              this.saveScreenshot('./confirmation.png').then(function(){
                this.end();
              });
            });//end wait for exists

          });//end click.then

        });// Save the screenshot to disk
      });//end pause.then

   });//end click.then

};//end takeScreenshot



function fillFields(driver, fields){

  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(){
                       tester.iter++;
                       console.log('tester.iter', tester.iter);
                       console.log('tester.totalSelects', tester.totalSelects);
                       if(tester.iter == tester.totalSelects-1){
                          takeScreenshot.call(this, null);
                        }
                        execQueue[tester.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[tester.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) // navigate to the web page
  .then(function(result) {

    fillFields(this, fields);

  });

0 个答案:

没有答案