2014-11-07 16 views
7

Dopo aver aperto una nuova scheda (seconda) sto provando a passare alla prima scheda.goniometro passare alla scheda precedente

 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

Ho lo stesso problema, ho provato un approccio diverso come incapsulare questo in una promessa, usato browser.switchTo(). Finestra (tab1) .. nessun successo .. – Teodor

risposta

8

Il problema è che si sta cercando di passare alla scheda precedente utilizzando Ctrl + Tab, invece di utilizzare gestisce la finestra per tornare indietro. Questo non è supportato in WebDriver, perché l'uso di ctrl + tab è un'operazione di sistema e non può essere emulato sul tuo browser per tutti i SO/browser. Basta usare di nuovo browser.switchTo().

+0

E 'possibile chiudere una scheda tramite Goniometro? – Aaron

4

@Aaron Questo codice recupera tutte le maniglie del browser disponibili e risolve la promessa. Poi si apre la scheda creato da un <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://..."); 
     }); 
    }); 
}); 

Allo stesso modo, si potrebbe passare tra le schede:

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

E, naturalmente, chiudere una scheda:

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]); 
    }); 
}); 

Spero che questo ti aiuti!

Problemi correlati