How to read OTP from gmail to protractor?

Conf.js

How to read OTP from gmail. I tried, but I can not and do not get any errors.

+6
source share
1 answer

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) {
// Open email from *******@gmail.com
// Its a non-angular site, so need to turn off synchronization
browser.ignoreSynchronization = true;
browser.driver.sleep(3000);

// Opens a new tab in which you retrieve OTP
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);

// Switch to new tab
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) {
            //console.log(token);
            tokenKey = token.substring(token.indexOf('-')+1, token.length);
            //console.log(tokenKey);
          });
        });
      });
  browser.driver.close();
  browser.switchTo().window(handles[0]);
});
}
0
source

All Articles