WebDriver getLocation in protractor

I am trying to get the x and y values ​​for an element on a page while my Protractor test is running.

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 get errors like "Expected undefined as". What am I missing?

+4
source share
1 answer

Apparently, getLocation () returns a promise, so the correct way to record a call is as follows.

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);
        });
    });
});
+8
source

Source: https://habr.com/ru/post/1536599/


All Articles