量角器中的WebDriver getLocation

时间:2014-04-15 13:01:44

标签: javascript webdriver protractor

我正在尝试在运行我的量角器测试时获取页面上元素的x和y值。

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

我遇到了'预期未定义为'的错误。'我错过了什么?

1 个答案:

答案 0 :(得分:9)

显然,getLocation()会返回一个promise,因此编写调用的正确方法如下所示。

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