我使用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
我认为您应该使用async
。我认为你的代码很棒。它并行地运行所有的操作,并且能够很好地处理错误。
如果
如果要删除async
,有几个选项:
- 使用Q流量控制
- 复制粘贴异步的实现
- 自己实施
如果你尝试使用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
}
})
})
如果有什么不清楚的地方,或者我没有切中要害,请告诉我,我会改进答案。