Search code examples
javascriptseleniumselenium-webdrivertabssendkeys

Selenium Webdriver trying to use 'ctrl + t' to open a tab - Javascript


I am running Selenium Webdriver to test a web application that is under development (I have created quite a few tests so far). I have been trying to open a few new tabs in the window opened by Selenium but no success so far. I looked through quite a bit of different solutions but most of them for Java or Python and I'm using Javascript (I need to use Javascript).

Selenium Webdriver: v.3.1.0 OS: Xubuntu 16.04 Browsers: Chrome 55.0.2883.87 and Firefox 50.1.0

I have tried various solutions including:

  • action sequences, which do nothing in both Chrome and Firefox but complains in Firefox:

    driver.actions().keyDown(Key.CONTROL).sendKeys('n').keyUp(Key.CONTROL).perform();

  • using Key.chord(), which results in no errors, no reaction but it does send the keys - Firefox giving a strange charCode after pressing the buttons

    driver.findElement(By.css("body")).sendKeys(Key.chord(Key.CONTROL, 't'));

  • Key.CONTROL only, which also results in no errors, no reaction but it does send the keys - Firefox giving a strange charCode after pressing the buttons

    driver.findElement(By.css("body")).sendKeys(Key.CONTROL + "t");

What I do at the moment is to navigate the driver to a website with javascript keypress detection and see if they were clicked after the 'aaa' :

driver.get("http://unixpapa.com/js/testkey.html");
driver.findElement(By.css("body")).sendKeys("aaa");
driver.findElement(By.css("body")).sendKeys(Key.CONTROL + "t");

This navigates to the page and it it oputputs that on the page detection area:

keydown  keyCode=17        which=17        charCode=0        
keydown  keyCode=84  (T)   which=84  (T)   charCode=0        
keypress keyCode=116 (t)   which=116 (t)   charCode=116 (t)  
keyup    keyCode=84  (T)   which=84  (T)   charCode=0        
keyup    keyCode=17        which=17        charCode=0  

which I believe means that they have been clicked. However, there is no reaction and no tabs created. No error displayed anywhere, no complains. Nothing. I was not sure if this is a bug or a problem or something that I might not be doing right. So if anyone has any idea, please help.


Solution

  • To open new tab you can try to use

    driver.executeScript('window.open();');