0
我正在使用Javascript,webdriverio(v2.1.2)从内部网站执行一些数据提取。这样的想法是Javascript循环播放元素并单击使用WebdriverIO的链接
- 身份验证
- 打开所需的网址,验证
- 在新的页面时,搜索具有特定关键字
- 一旦找到一个锚标记,单击锚标记
下面是我尝试过的,它的工作原理(最后两点)。我不得不使用Q和异步来实现它。我希望只用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
对我来说,你正在使用正确和推荐的方法。异步和Q用于不同的事情 - Q用于承诺接口,异步用于流量控制。有许多方法可以在没有异步的情况下编写它,但为什么要这么做呢? – 2014-12-13 15:07:53