Javascript循环元素并使用WebdriverIO单击链接

问题描述 投票:0回答:1

我正在使用Javascript,webdriverio(v2.1.2)从内部站点执行一些数据提取。所以这个想法是

  • 认证
  • 经过身份验证后,打开所需的URL
  • 在新页面中,搜索具有特定关键字的锚标记
  • 找到后,单击锚标记

以下是我尝试过的并且它有效(最后两点)。我不得不使用Q和async来实现它。我希望只使用Q来实现它。有人可以帮我,关于如何使用Q实现它?

    var EmployeeAllocationDetails = (function () {
'use stricy';
/*jslint nomen: true */
var Q = require('Q'),
    async = require('async'),   
    _ead_name = 'Employee Allocation Details',
    goToEadFromHome;

goToEadFromHome = function (browserClient) {
    browserClient.pause(500);
    var deferred = Q.defer();
    browserClient.elements('table.rmg td.workListTD div.tab2 div.contentDiv>a', function (err, results) {
        if (err) {
            deferred.reject(new Error('Unable to get EAD page. ' + JSON.stringify(err)));
        } else {

        async.each(results.value, function (oneResult, callback) {
                console.log('Processing: ' + JSON.stringify(oneResult));
                browserClient.elementIdText(oneResult.ELEMENT, function (err, result) {
                    if (err) {
                        if (err.message.indexOf('referenced element is no longer attached to the DOM') > -1 ){
                            callback();
                        } else {
                            callback('Error while processing :' + JSON.stringify(oneResult) + '. ' + err);
                        }
                    } else if(!result){
                        console.log('result undefined. Cannot process: ' + JSON.stringify(oneResult));
                        callback();
                    } else if(result.value.trim() === _ead_name){
                        deferred.resolve(oneResult);
                        callback();
                    }
                });
            }, function (err) {
                // if any of the processing produced an error, err would equal that error
            if( err ) {
        // One of the iterations produced an error.
        // All processing will now stop.
            console.log('A processing failed to process. ' + err);
            } else {
            console.log('All results have been processed successfully');
            }
            }); //end of async.each

        }
    });
    return deferred.promise;
};

return {
    launchEad : goToEadFromHome
  }
})();
module.exports = EmployeeAllocationDetails;

相关Github问题链接https://github.com/webdriverio/webdriverio/issues/123

javascript selenium-webdriver q webdriver-io
1个回答
0
投票

我认为你应该使用async。我认为你的代码很棒。它并行运行所有内容,它可以很好地处理错误。

如果

如果你想删除async,有几个选项:

  • 使用Q流量控制
  • 复制粘贴async的实现
  • 自己实现它

如果您尝试使用Q的流控制,它将看起来像这样(伪代码):

   var getTextActions = [];
   function createAction(element){ 
        return function(){
             return element.getText();
        }
   }

   each(elements, function(element){ getTextActions.push( createAction(element) ) }); 
   Q.all(getTextActions).then(function(results) {
      ... iterate all results and resolve promise with first matching element.. 
   } ); 

请注意,此实施的性能较差。它将首先从所有元素中获取文本,然后尝试解决您的承诺。你的实现更好,因为它们并行运行。

我不建议你自己实现它,但如果你仍然想要它,它将看起来像这样(伪代码):

var elementsCount = elements.length; 
elements.each(function(element){
   element.getText(function(err, result){
      elementsCount --;
      if ( !!err ) { logger.error(err); /** async handles this much better **/  } 
      if ( isThisTheElement(result) ) { deferred.resolve(result); }
      if ( elementsCount == 0 ){ // in case we ran through all elements and didn't find any
            deferred.resolve(null); // since deferred is resolved only once, calling this again if we found the item will have no effect
      }
   })
})

如果有什么不清楚,或者我没有到达现场,请告诉我,我会改进答案。

© www.soinside.com 2019 - 2024. All rights reserved.