量角器:获取元素's同上
Protractor: get Element's Id
我在量角器和javascript中的所有地方都有这种异步调用。过去需要2行代码,现在需要10行代码。这里有一个例子:
我正在编写一个量角器实用程序方法,只需检查一组相关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,真是太可惜了。
这对于异步代码来说并不那么冗长。。。特别是如果考虑到可以直接将函数innerCheck
传递给promise:
// 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 {
field.getAttribute('id').then(innerCheck);
}
应该只是简单的
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 在动态创建的元素上获取对特定选择器的引用
- 从城市名称获取惊喜
- Angular只从数组中获取所需的数据
- 无法将数据从firebase获取到我的html页面
- 从ajax请求中获取javascript对象
- 如何从画布上的某个移动事件中获取X和Y
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在PHP中使用$_POST获取Select元素值
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 量角器:获取元素's同上