Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all element attributes using protractor

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?

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

like image 996
alecxe Avatar asked Dec 29 '14 19:12

alecxe


People also ask

How to use elementfinder in protractor test script?

Returns ElementFinder which is used to uniquely locate the element in the webpage. In order to use the ElementFinder in the test script, we need to import element and ElementFinder. Purpose: The element () function in protractor is used to locate the element in a web page.

What is protractor and selenium in web design?

With the help of protractor and selenium, these elements are uniquely identified. Each web element is associated with the property such as class, tags, id or attributes which uniquely identifies that element. What is ElementFinder in Protractor? In protractor, the single web element belongs to type ElementFinder.

How to get the element of type webelement in protractor?

GetWebElement() function in Protractor. Purpose: The element() returns value type of ElementFinder. However, if required we can use the getWebElement() function to get the element of type WebElement. Syntax: element(locator).getWebElement(): WebElementPromise; Returns: This function returns the value of type WebElementPromise. Code Example:

What is elementarrayfinder in protractor?

What is ElementArrayFinder in Protractor? ElementArrayFinder is an array of WebElements which sets up a chain of conditions that identify an array of elements. Additionally, it allows the performance of actions on them. The action will apply to each element identified by the ElementArrayFinder.


1 Answers

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);
};

demo

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', ... });
like image 106
neo Avatar answered Sep 23 '22 11:09

neo