如何在量角器中从gmail读取OTP?

rts*_*rts 6 javascript email protractor

Conf.js

如何从gmail中读取OTP.我试过但我不能也没有得到任何错误.

bdf*_*7kt 0

这是我想出的代码。我假设 OTP 位于您收件箱中的第一封电子邮件内。关闭 Gmail 中允许类似邮件分组在一起的设置也很有帮助,因为这可能会导致问题。(请原谅使用 browser.driver.sleep(),这个可以替换)

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]);
});
}
Run Code Online (Sandbox Code Playgroud)