Whilst I agree that ideally, you'd want to avoid testing the OAuth2 functionality itself, sometimes it is easier/required to go through those steps.
I use something like this, which makes use of another answer.
Credentials should not be in the code, and I don't even like them in files: I prefer them supplied on the command line, either as arguments or, as in this case, through environment variables.
loginWithGoogle(
process.env.PROJ_TEST1_GMAIL_USER,
process.env.PROJ_TEST1_GMAIL_PASS
)
/**
* Uses the dreaded `sleep` method because finding the password
* by any css selector tried fails.
* @param {string} username - A Google username.
* @param {string} passphrase - A Google passpharse.
* @return {Promise.<void>} Promise resolved when logged in.
*/
var loginWithGoogle = function (username, passphrase) {
return selectWindow(1).then( () => {
return browser.driver.findElement(by.css('[type="email"]'))
.then( (el) => {
el.sendKeys( username + protractor.Key.ENTER);
}).then( () => {
browser.driver.sleep(1000);
}).then( () => {
browser.actions().sendKeys( passphrase + protractor.Key.ENTER ).perform();
});
})
}
/**
* Focus the browser to the specified window.
* [Implementation by and thanks to]{@link http://stackoverflow.com/questions/21700162/protractor-e2e-testing-error-object-object-object-has-no-method-getwindowha}
* @param {Number} index The 0-based index of the window (eg 0=main, 1=popup)
* @return {webdriver.promise.Promise.<void>} Promise resolved when the index window is focused.
*/
var selectWindow = (index) => {
browser.driver.wait(function() {
return browser.driver.getAllWindowHandles().then( (handles) => {
if (handles.length > index) {
return true;
}
});
});
return browser.driver.getAllWindowHandles().then( (handles) => {
return browser.driver.switchTo().window(handles[index]);
});
};
One day I should find out why the commented-out code does not run: until then, this does work.