Angularjs 需要oauth认证的e2e量角器测试
我有一个Angular应用程序,需要与谷歌进行身份验证,授予一些作用域,等等,我正在尝试为它设置自动e2e测试。一般来说,我的量角器工作得很好,但当我们进入谷歌认证页面、登录并重定向时,量角器无法通过测试,因为“在等待结果时卸载了文档。” 在每次测试之前,我是否可以使用一种工具或技术对开发google帐户进行身份验证Angularjs 需要oauth认证的e2e量角器测试,angularjs,oauth,protractor,angularjs-e2e,Angularjs,Oauth,Protractor,Angularjs E2e,我有一个Angular应用程序,需要与谷歌进行身份验证,授予一些作用域,等等,我正在尝试为它设置自动e2e测试。一般来说,我的量角器工作得很好,但当我们进入谷歌认证页面、登录并重定向时,量角器无法通过测试,因为“在等待结果时卸载了文档。” 在每次测试之前,我是否可以使用一种工具或技术对开发google帐户进行身份验证 如果我能让框架在普通的webdriver驱动登录时保持一秒钟,并且在我到达目标页面后才真正激活angular,那就太完美了 关键是使用browser.driver.get而不是br
如果我能让框架在普通的webdriver驱动登录时保持一秒钟,并且在我到达目标页面后才真正激活angular,那就太完美了 关键是使用
browser.driver.get
而不是browser.get
,并使用browser.driver.sleep(几毫秒)
在使用角度特定命令之前让角度加载到最终目的地
这是我的工作量角器规范,首先授权给谷歌,然后计算中继器中的项目:
it('allows the user to add new slides', function () {
browser.driver.get('http://localhost:3000/editor/?state=%7B"action":"create"%7D');
// at this point my server redirects to google's auth page, so let's log in
var emailInput = browser.driver.findElement(by.id('Email'));
emailInput.sendKeys('user@googleappsdomain.com');
var passwordInput = browser.driver.findElement(by.id('Passwd'));
passwordInput.sendKeys('pa$sWo2d'); //you should not commit this to VCS
var signInButton = browser.driver.findElement(by.id('signIn'));
signInButton.click();
// we're about to authorize some permissions, but the button isn't enabled for a second
browser.driver.sleep(1500);
var submitApproveAccess = browser.driver.findElement(by.id('submit_approve_access'));
submitApproveAccess.click();
// this nap is necessary to let angular load.
browser.driver.sleep(10000);
// at this point the protractor functions have something to hook into and
// will work as normal!
element(by.id('new-slide-dropdown-trigger')).click();
element(by.id('new-text-slide-trigger')).click();
var slideList = element.all(by.repeater('slide in deck.getSlides()'));
slideList.then(function(slideElements) {
expect(slideElements.length).toEqual(1);
});
});
我有一个GoogleAuth页面对象(如下),它为我完成了全部工作 这里的关键是“isAngularSite(false);”函数,它指示webdriver我们是否进入“角度”或“非角度”网站。 (执行情况见下文) ---将其放入paragrator.conf.js中
onPrepare: function () {
global.isAngularSite = function (flag) {
console.log('Switching to ' + (flag ? 'Asynchronous' : 'Synchronous') + ' mode.')
browser.ignoreSynchronization = !flag;
},
global.BROWSER_WAIT = 5000;
}
---这就是您使用它的方式:
it('should login though google', function(done) {
mainPage.loginBtn.click().
then(function () {
loginPage.connectWithGoogleBtn.click();
googlePage.loginToGoogle();
browser.wait(mainPage.userName.isPresent()).
then(function () {
expect(mainPage.userName.getText()).
toEqual('my.account@google.com');
done();
});
});
});
应使用
waitForAngularEnabled
启用/禁用使用量角器等待角度任务。默认情况下,它将被启用。您可以使用以下选项:
// do things on your Angular application
// go to external oauth page
waitForAngularEnabled(false)
// login on oauth page and redirect to your Angular application
waitForAngularEnabled(true)
browser.get('/home') // this is a page from your Angular application
waitForAngularEnabled
返回承诺。browser.get
功能块,直到角度页面加载。我会使用量角器测试中的SEEP作为最后一个资源。与其在不确定的时间内休眠,为什么不等待DOM中的元素更改?抱歉,在注释之前等待了很长时间,但mainPage和loginPage是什么?它们没有在任何地方定义。mainPage和logingPage基本上是您在测试框架中可能拥有的页面对象。。。虽然你可以删除这些变量,代码也会有类似的感觉,那么googlePage变量呢?我如何使用第一个代码呢?我现在在另一个文件中有它,在规范中被调用。是这样吗?对不起,我有新的问题,我可能需要聊天。我不知道为什么这被否决了。可否澄清?demee和Riley Lark给出的答案是有效的,但是现在waitForAngularEnabled
可用了,我们应该使用它而不是睡眠计时器。将计时器设置得太高,您的测试将花费很长时间;将计时器设置得太低,您的测试可能会失败。当我们从oauth页面切换回来时,这个答案将立即继续测试您的Angular应用程序。
// do things on your Angular application
// go to external oauth page
waitForAngularEnabled(false)
// login on oauth page and redirect to your Angular application
waitForAngularEnabled(true)
browser.get('/home') // this is a page from your Angular application