我有一个特定的用例,在测试代码中我想获取表头(异步),然后在javascript对象上动态创建getter。我想在测试中使用的语法是antenneTable.row(0).getAzimut();
。我遇到的问题是antenneTable.row(0);
返回一个promise,因此尚未在该对象上定义getAzimut。
如果我使用antennetable.row(0).then()但我想保留语法,我知道如何修复它。
提前致谢
这是我的代码:
Row.js
'use strict';
var Row = function (row, classes) {
var outer = this;
function camelCase(input) {
return input.toLowerCase().replace(/-(.)/g, function(match, group1) {
return group1.toUpperCase();
})}
for(var i =0 ; i< classes.length; i ++){
var methodName = camelCase("get-"+classes[i]);
(function(index) {
outer[methodName] = function () {
return row.all(by.tagName('td')).get(index).getText();
}
})(i);
}
};
module.exports = Row;
Table.js
'use strict';
var Row = require('./row.js');
var Table = function (tableElement) {
var headElement = tableElement.element(by.css('thead'));
var bodyElement = tableElement.element(by.css('tbody'));
this.row = function (index) {
return headElement.all(by.css('th')).map(function (th) {
return th.getAttribute('class');
}).then(function (clazzes) {
var rows = bodyElement.all(by.css('tr'));
return new Row(rows.get(index), clazzes);
})
};
};
module.exports = Table;
MyTestClass.js
'use strict';
describe('dossier map', function(){
var mainPage = require('../../pages/mainpage.js');
var DossierDetailPage = require('../../pages/dossier/dossier-detail.js');
var Table = require('../../pages/table.js');
var dossierDetailPage = new DossierDetailPage();
afterEach(function() {
resetData();
mainPage.logout();
});
it('antenne details popup verschijnt', function() {
// TODO Activate test in Firefox and IE
if (!browser.isFirefox && !browser.isIE) {
mainPage.goTo('admin', 'admin');
mainPage.dossiers().byIndex(7).openDetail();
dossierDetailPage.zoomOut(5);
var popoverElement = element(by.css('#popover .popover'));
browser.driver.wait(function() {
return popoverElement.isPresent().then(function(isDisplayed){
if (isDisplayed) {
return true;
}
else {
dossierDetailPage.klikOpAntenneCluster(11);
return false;
}
});
});
var antenneTable = new Table(popoverElement.element(by.css('table')));
expect(antenneTable.row(0).getAzimut()).toBe('115');
}
}, 120000);
});
答案 0 :(得分:1)
这个名为async/await的图书馆(受C#
第5版启发)
这会使您的有效代码外观和感觉同步
<强> e.g。强>
var Q = require("q");
var async = require('asyncawait/async');
var await = require('asyncawait/await');
function delay(millis) {
var deferred = Q.defer();
setTimeout(deferred.resolve, millis);
return deferred.promise;
}
async(function () {
console.log('message 1');
await(delay(100));
console.log('message 2');
})();
答案 1 :(得分:0)
解决方案可能是不返回对象,而是返回此对象的代理,并仅在您的promise已解决时解析您的方法。