Here is the code I came up with. I made the assumption that OTP is inside the first email message in your inbox. It is also useful to disable the setting in gmail, which allows you to group similar messages, as this can cause problems. (I apologize for using browser.driver.sleep (), this can be replaced)
var tokenKey;
function getKey(a) {
browser.ignoreSynchronization = true;
browser.driver.sleep(3000);
browser.driver.executeScript(function() {
(function(a){
document.body.appendChild(a);
a.setAttribute('href', 'https://gmail.com');
a.dispatchEvent((function(e){
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
return e;
}(document.createEvent('MouseEvents'))))}(document.createElement('a')));
});
browser.driver.sleep(3000);
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[1]);
if(a){
var username = browser.driver.findElement(by.xpath('//*[@id="identifierId"]'));
username.sendKeys('*********@gmail.com');
browser.driver.findElement(by.id('identifierNext')).click();
}
var EC = protractor.ExpectedConditions;
var firstEmail = element(by.xpath('//*[@id=":3d"]'));
var passwordInput = element(by.xpath('//*[@id="password"]/div[1]/div/div[1]/input'));
if(a){
browser.wait(EC.visibilityOf(passwordInput), 5000);
browser.driver.sleep(1000);
passwordInput.sendKeys('*********');
browser.driver.findElement(by.id('passwordNext')).click();
}
browser.wait(EC.visibilityOf(firstEmail), 5000);
firstEmail.click().then(function () {
browser.driver.sleep(2000);
element.all(by.cssContainingText('div', 'Text Leading up to password:')).count().then(function (results) {
element.all(by.cssContainingText('div', 'Text Leading up to password::')).get(results-1).getText().then(function (token) {
tokenKey = token.substring(token.indexOf('-')+1, token.length);
});
});
});
browser.driver.close();
browser.switchTo().window(handles[0]);
});
}
source
share