How can I switch between browser tabs in my automation test?

How can I switch between browser tabs in my automation test?

Open a new tab using Ctrl + t Driver control automatically switches to the newly opened tab Perform the required operations here. Next switch back to the old tab using Ctrl + Tab. You need to keep pressing this unless you reach the desired tab. Once the desired tab is reached, then perform the operations in that tab. Get the current window handle and open a new tab using Ctrl + t

   driver.get("http://google.com");
    String windowHandle = driver.getWindowHandle();
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

Check the size of the output of getWindowHandles().

Then Use:

 ArrayList tabs = new ArrayList (driver.getWindowHandles());
    System.out.println(tabs.size());
    driver.switchTo().window(tabs.get(0));

The control is now in the new tab-

driver.get("Your application URL");
    //perform other operations on new tab.

Switch to the old tab using Ctrl + Tab:

driver.switchTo().window(mainWindowHandle);
    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
    driver.switchTo().defaultContent();