获取使用量角器所有元素属性 [英] Get all element attributes using protractor

查看:202
本文介绍了获取使用量角器所有元素属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据该文件,以获得通过名称的单一属性,您可以使用的 .getAttribute() WebElement

According to the documentation, to get a single attribute by name you can use .getAttribute() on a WebElement:

var myElement = element(by.id('myId'));
expect(myElement.getAttribute('myAttr')).toEqual('myValue');

但我怎么能得到所有的元素具有的属性?

But how can I get all of the attributes that an element has?

有没有关于在量角器API 这个用例/功能信息。

There is no information about this use case/functionality in the Protractor API.

推荐答案

您可以扩展JavaScript的元素键入并添加的getAttributes()功能:

You can expand javascript's Element type and add getAttributes() function:

Element.prototype.getAttributes = function() {
    return (function (node) {
        var attrs = {};
        for (var i=0;i<node.length;i++) {
            attrs[node.item(i).name] = node.item(i).value;
        }
        return attrs;
    })(this.attributes);
};

演示

然后就可以使用您使用一个属性同样的方法测试属性的完整性:

then you can test integrity of attributes using the same method you use for one attribute:

var myElement = element(by.id('myId'));
expect(myElement.getAttributes()).toEqual({'attr1': 'value1', 'attr1': 'value1', ... });

这篇关于获取使用量角器所有元素属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆