2017-06-09 19 views

Trả lời

0

Đây là mã tôi đã đưa ra. Tôi đã giả định rằng OTP nằm trong email đầu tiên trong hộp thư đến của bạn. Cũng rất hữu ích khi tắt cài đặt trong gmail cho phép các thư tương tự nhóm lại với nhau vì điều này có thể gây ra sự cố. (Vui lòng tha thứ cho việc sử dụng browser.driver.sleep(), điều này có thể được thay thế)

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

Tôi đã thử trên mã và không thể đạt được nó vì vậy tôi có thể cung cấp thông tin đăng nhập gmail của tôi để bạn có thể thử từ đầu của bạn. cảm ơn –

+0

Chắc chắn, hãy tiếp tục. – bdf7kt

+0

var a đại diện cho dù đây là lần đầu tiên truy cập vào gmail trên chạy hiện tại. Nó bỏ qua khía cạnh đăng nhập khi trình duyệt sẽ nhớ điều đó. Bạn nên thay đổi các dòng sau: – bdf7kt

Các vấn đề liên quan