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.
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.
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.
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? 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.
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', ... });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With