I am trying to make sure the page scrolls to an element when a certain span is clicked. So I need to check the element's y
position. Can someone explain how I can get an element's position?
element.all(by.css('[scroll-to="section-executive-summary-anchor"]'))
.then(function (elem) {
elem[0].click().then(function () {
element(by.id('section-executive-summary-anchor'))
.then(function (el) {
// I need "el.position" or something along those lines
});
});
});
Locators in Protractor can be found by using the 'element' function which will take a locator as an argument (parameter) and return the 'ElementFinder' object. The 'ElementFinder' can be used to perform operations on the element like 'click', 'sendKeys', 'getAttribute' etc.
In order to get the location of an element relative to the document, jQuery offset() method is used. The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element. We can also use the jQuery position() method.
Definition and Usage. The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.
You can use getLocation()
function:
element(by.id('section-executive-summary-anchor')).getLocation().then(function (location) {
expect(location.y).toEqual(100);
});
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