Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver getLocation in protractor

I'm trying to get the x and y values for an element on the page while running my Protractor test.

it('should keep the left nav links floating along with the page', function() {
        var navDiv = element(by.id('pd_page_nav'));
        var initTop = navDiv.getLocation().y;
        var initLeft = navDiv.getLocation().x;

        browser.get("en-us/learn#dpacreditresource");
        var currTop = navDiv.getLocation().y;
        var currLeft = navDiv.getLocation().x;

        expect(initLeft).toBe('');
        expect(initTop).toBe('');
        expect(currLeft).toBe(initLeft);
        expect(currTop).toBeGreaterThan(initTop);
});

I'm getting errors like 'Expected undefined to be ''.' What am I missing?

like image 775
Arun Avatar asked Dec 25 '22 10:12

Arun


1 Answers

Apparently, getLocation() returns a promise, so the proper way to write the call is as below.

it('should keep the left nav links floating along with the page', function () {
    var initTop = 0;
    var initLeft = 0;

    element(by.id('pd_page_nav')).getLocation().then(function (navDivLocation) {
        initTop = navDivLocation.y;
        initLeft = navDivLocation.x;

        browser.get("en-us/learn#dpacreditresource");

        element(by.id('pd_page_nav')).getLocation().then(function (navDivLocation2) {
            var currTop = navDivLocation2.y;
            var currLeft = navDivLocation2.x;

            expect(currLeft).toBe(initLeft);
            expect(currTop).toBeGreaterThan(initTop);
        });
    });
});
like image 115
Arun Avatar answered Dec 28 '22 05:12

Arun