尝试为 ionic 编写一些 Protractor 测试,实现了选择器 element( by.css('.selector'));
不再滚动到元素。当它在浏览器中不可见时,测试会自动失败。
我花了一段时间才弄清楚。使用 ptor.sleep(2000)
在 browser.get('domain');
之后延迟页面然后如果我滚动浏览正在测试元素的部分,它们会通过(如预期的那样)。
我相信这与 ionic 接管滚动事件有关。
有没有人遇到过这个问题,或者对所有元素都有某种 scrollTo 实现?
sample 测试
"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" );
} );
});
最佳答案
最终使用了此处提供的答案的变体:How to set focus on a section of my web page then scroll down
更改了它,以便函数仅将元素作为可重用性的参数。似乎正在工作。
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" );
} );
});
关于 Protractor 不滚动到测试元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25436491/