4
与此异步调用遍布一般量角器和JavaScript的地方我有它。过去需要2行代码现在只需10这里有一个例子:量角器:获取元素的ID
我写一个量角器工具方法简单地检查一组相关的div和输入文本框的某些DOM属性。这是针对我正在开发的验证框架。我们的想法是将量角器元素传递给此方法,然后基于该元素的ID检查相关div和输入文本框上的某些DOM属性。这是我得到它的工作:
/**
* Checks for error in a NUAF field
* @param {String or Element} field .
* @param {string} errorText expected validation error text to appear in tooltip
*/
exports.checkForError = function (field, errorText) {
var innerCheck = function(fieldId) {
expect(fieldId).not.toBe(undefined);
var elmntd = element(by.id('divInput.'+fieldId));
expect(elmntd).not.toBe(null);
expect(elmntd.getAttribute('tooltip')).toContain(errorText);
expect(exports.hasClass(element(by.id('prnt.'+fieldId)), 'has-error')).toBe(true);
};
// this unbelievably complex block of code gets the id of the
// field argument. If string was passed, the fieldid is just that .
if (typeof field === 'string') {
innerCheck(field);
} else {
//what used to be field.id now needs 6 lines of code?
field.getAttribute('id').then(
function(idAttribute) {
console.log("*********: "+idAttribute);
innerCheck(idAttribute);
}
);
}
};
的问题是:有没有办法写的代码field.getAttribute('id').then
块一个更好,更简洁的方式。只是为了得到元素的Id而写所有这些只是一种耻辱。
你可以使类检查更具可读性 - 而不是hasClass帮助器,请[自定义toHaveClass匹配器](http://stackoverflow.com/a/32699366/771848)。 – alecxe