需要 OAuth 身份验证的 E2E 量角器测试



我有一个 Angular 应用程序,需要向 Google 进行身份验证、授予一些范围等,我正在尝试为它设置自动 e2e 测试。 总的来说,我的量角器对我来说效果很好,但是当我们进入谷歌身份验证页面、登录并被重定向时,量角器无法通过测试,因为"文档在等待结果时卸载"。

在每次测试之前,我可以使用某种工具或技术对开发Google帐户进行身份验证吗?

如果我能让框架在普通旧的网络驱动程序驱动登录时保持一秒钟,并且只有在我到达目标页面后才能真正激活角度的东西,那就完美了!

关键是使用 browser.driver.get 而不是 browser.get ,并在使用角度特定命令之前使用browser.driver.sleep(someMilliseconds)让角载荷到达最终目的地。

这是我的工作量角器规范,它首先授权给 Google,然后计算中继器中的项目:

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

我有一个谷歌身份验证页面对象(下图)可以为我完成整个工作。

这里的关键是"isAngularSite(false)";函数女巫指示webdriver我们是否进入"angular"或"non angular"网站。(实现如下)。

/* global element, browser, by */
'use strict';
var GOOGLE_USERNAME = 'my.account@google.com';
var GOOGLE_PASSWORD = 'password';
var ec = protractor.ExpectedConditions;
var Google = function () {
  this.emailInput = element(by.id('Email'));
  this.passwordInput = element(by.id('Passwd'));
  this.nextButton = element(by.id('next'));
  this.signInButton = element(by.id('signIn'));
  this.approveAccess = element(by.id('submit_approve_access'));
  this.loginToGoogle = function () {
    var self = this;
    /* Entering non angular site, it instructs webdriver to switch 
       to synchronous mode. At this point I assume we are on google
       login page */ 
    isAngularSite(false); 
    this.emailInput.sendKeys(GOOGLE_USERNAME);
    this.nextButton.click();
    this.passwordInput.isPresent().then(function () {
      browser.wait(ec.visibilityOf(self.passwordInput), BROWSER_WAIT).then(function () {
        self.passwordInput.sendKeys(GOOGLE_PASSWORD);
        self.signInButton.click();
        browser.wait(ec.elementToBeClickable(self.approveAccess), BROWSER_WAIT).then(function () {
          self.approveAccess.click();
          /* Now we are being redirected to our app, switch back to
             async mode (page with angular) */
          isAngularSite(true);
        });
      });
    });
  }
}
module.exports = new Google();

---把它扔到量角器中.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功能会一直阻止,直到加载 Angular 页面。

最新更新