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

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

问题描述

根据文档,要按名称获取单个属性,您可以使用 .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?

Protractor API 中没有关于此用例/功能的信息.

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

推荐答案

可以扩展javascript的Element类型并添加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天全站免登陆