我有一个 Google Auth 页面对象(如下),它为我完成了整个工作。
这里的关键是“isAngularSite(false);” 如果我们进入“角度”或“非角度”网站,功能女巫会指示 webdriver。(下面的实现)。
/* 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();
--- 把它扔进 protractor.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();
});
});
});