Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element's offset position with protractor

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

    });

  });
like image 531
Subtubes Avatar asked Mar 06 '15 00:03

Subtubes


People also ask

How do you find the location of an element using a protractor?

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.

How do you find the position of an element relative to a window?

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.

What is offset top CSS?

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.


1 Answers

You can use getLocation() function:

element(by.id('section-executive-summary-anchor')).getLocation().then(function (location) {
    expect(location.y).toEqual(100);
});
like image 174
alecxe Avatar answered Oct 05 '22 05:10

alecxe