The first thing you need to do is open a new tab and save its handle name. This is best done using javascript rather than keys (ctrl + t), since keys are not always available on automation servers. Example:
public static String openNewTab(String url) { executeJavaScript("window.parent = window.open('parent');"); ArrayList<String> tabs = new ArrayList<String>(bot.driver.getWindowHandles()); String handleName = tabs.get(1); bot.driver.switchTo().window(handleName); System.setProperty("current.window.handle", handleName); bot.driver.get(url); return handleName; }
The second thing you need to do is switch between tabs. Doing this only with the buttons on the switch window will not always work, since the tab you are working on will not always be in focus, and Selenium will refuse from time to time. As I said, using keys is a little problematic, and javascript doesn't really support tab switching, so I used warnings to switch tabs, and it worked like a charm:
public static void switchTab(int tabNumber, String handleName) { driver.switchTo().window(handleName); System.setProperty("current.window.handle", handleName); if (tabNumber==1) executeJavaScript("alert(\"alert\");"); else executeJavaScript("parent.alert(\"alert\");"); bot.wait(1000); driver.switchTo().alert().accept(); }
Ed Ram Oct 19 '16 at 12:55 2016-10-19 12:55
source share