Last active
October 23, 2019 13:39
-
-
Save leegee/400160e7113f984cb17d6746accac8ba to your computer and use it in GitHub Desktop.
Protractor Google OAuth2 Manual Sign-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment