2014-11-07 30 views
7

Sau khi mở tab mới (thứ hai), tôi đang cố gắng chuyển sang tab đầu tiên.thước đo góc chuyển sang tab trước

 common.clickOpenNewSession(); //it opens the new tab 

browser.getAllWindowHandles().then(function (handles) { 
    var secondWindowHandle = handles[1]; 
    var firstWindowHandle = handles[0]; 
    browser.switchTo().window(secondWindowHandle).then(function() { //the focus moves on new tab 
     browser.sleep(3000); 
     expect(browser.driver.getCurrentUrl()).toContain("url"); 
//do some actions 

    }); 
//below it doesn't work. I try to go back on previous tab without closing the second tab 
    browser.actions().sendKeys(protractor.Key.CONTROL).sendKeys(protractor.Key.TAB).perform(); 
    browser.sleep(4000); 
    browser.driver.switchTo().window(firstWindowHandle); 
    browser.setLocation('http://google.com'); 
}); 
+0

tôi có cùng một vấn đề, tôi đã thử cách tiếp cận khác nhau như gói gọn trong lời hứa, sử dụng browser.switchTo(). Cửa sổ (tab1) .. không thành công .. – Teodor

Trả lời

8

Vấn đề là bạn đang cố chuyển đến tab trước bằng cách sử dụng tab ctrl + thay vì sử dụng tay cầm cửa sổ để chuyển về. Điều này không được hỗ trợ trong WebDriver, bởi vì việc sử dụng tab ctrl + là một thao tác hệ thống và không thể được mô phỏng trên trình duyệt của bạn cho tất cả OS/trình duyệt. Chỉ cần sử dụng lại browser.switchTo().

+0

Có thể đóng tab thông qua thước đo góc không? – Aaron

4

@Aaron Mã này nhận tất cả các trình điều khiển trình duyệt khả dụng và giải quyết lời hứa. Sau đó, mở tab được tạo ra bởi một <a href='newlink' target='_blank'>Link</a>:

POFile.buttonWithABlankTarget.click().then(function() { 
    browser.getAllWindowHandles().then(function(handles) { 
     var newTabHandle = handles[1]; 
     browser.switchTo().window(newTabHandle).then(function() { 
      // Expect the newly opened tab URL to equal the href of the invitation 
      expect(browser.driver.getCurrentUrl()).toContain("http://..."); 
     }); 
    }); 
}); 

Trong cùng một thời trang, bạn có thể chuyển đổi giữa các tab:

it("should open the first tab", function() { 
    browser.getAllWindowHandles().then(function (handles) { 
     browser.switchTo().window(handles[0]); 
    }); 
}); 

Và, tất nhiên, đóng một tab:

it("should close the second tab and return to the first tab", function() { 
    browser.getAllWindowHandles().then(function (handles) { 
     // We currently are on the second tab... 
     browser.driver.close(); 
     browser.switchTo().window(handles[0]); 
    }); 
}); 

Hi vọng điêu nay co ich!

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