量角器没有滚动到测试元素

Chr*_*all 4 protractor ionic-framework

试图为离子写一些量角器测试,意识到选择器element( by.css('.selector'));不再滚动到元素.当它在浏览器中不可见时,会自动失败测试.

我花了一段时间来搞清楚.使用a ptor.sleep(2000)来延迟页面browser.get('domain');然后如果我滚动查看元素的部分,它们会通过(如预期的那样).

我相信这与离子接管滚动事件有关.

有没有人遇到过这个或者为所有元素都有某种scrollTo实现?

样本测试

"use strict";

/* Tests */

var ptor = protractor.getInstance();

describe( "Register page", function ()
{
    browser.get( "#/register" );

    ptor.sleep(2000);

    it( "Check SMS Preference", function ()
    {

        var smsLabelConfirm = element( by.css( ".sms-confirm" ) ),
            smsLabelDeny = element( by.css( ".sms-deny" ) ),
            smsInputConfirm = element ( by.id( "sms-confirm" ) ),
            smsInputDeny = element ( by.id( "sms-deny" ) );

            smsLabelConfirm.click();
            expect( smsInputConfirm.getAttribute( "checked" ) ).toBe( "true" );
            expect( smsInputDeny.getAttribute( "checked" ) ).toBe( null );

            smsLabelDeny.click();
            expect( smsInputConfirm.getAttribute( "checked" ) ).toBe( null );
            expect( smsInputDeny.getAttribute( "checked" ) ).toBe( "true" );

    } );
});
Run Code Online (Sandbox Code Playgroud)

Chr*_*all 5

使用此处提供的答案的变体结束:如何将焦点设置在我的网页的一部分上,然后向下滚动

更改它,以便函数只将元素作为可重用性的参数.似乎工作.

var ptor = protractor.getInstance();

var scrollIntoView = function (element) {
  arguments[0].scrollIntoView();
};

describe( "Register page", function ()
{
    browser.get( "#/register" );

    ptor.sleep(2000);

    it( "Check SMS Preference", function ()
    {

        var smsLabelConfirm = element( by.css( ".sms-confirm" ) ),
            smsLabelDeny = element( by.css( ".sms-deny" ) ),
            smsInputConfirm = element ( by.id( "sms-confirm" ) ),
            smsInputDeny = element ( by.id( "sms-deny" ) );

            browser.executeScript(scrollIntoView, smsLabelConfirm);

            smsLabelConfirm.click();
            expect( smsInputConfirm.getAttribute( "checked" ) ).toBe( "true" );
            expect( smsInputDeny.getAttribute( "checked" ) ).toBe( null );

            smsLabelDeny.click();
            expect( smsInputConfirm.getAttribute( "checked" ) ).toBe( null );
            expect( smsInputDeny.getAttribute( "checked" ) ).toBe( "true" );

    } );
});
Run Code Online (Sandbox Code Playgroud)

  • @AshleyCoolman我也有这个问题,但我通过将`.getWebElement()`添加到我传入参数的元素上来解决它.这样就可以传入`WebElement`而不是它的包装器. (4认同)
  • 这个答案有一个错误 - 应该改为`element.scrollIntoView();`.这仍然不适合我,但当你添加@Aaron的使用`.getWebElement()`的建议时,一切正常! (3认同)
  • 奇怪的是,使用`scrollIntoView`给了我一个最大的调用堆栈错误. (2认同)