This test opens a new tab, navigates to Google, verifies the page title, then switches back to the original window.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
public class NewWindowTabTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
// Open new tab
WebDriver newTab = driver.switchTo().newWindow(WindowType.TAB);
newTab.get("https://google.com");
// Check title contains 'Google'
if (newTab.getTitle().contains("Google")) {
System.out.println("New tab opened successfully with Google.");
} else {
System.out.println("Failed to open Google in new tab.");
}
// Switch back to original window
driver.switchTo().window(originalHandle);
System.out.println("Back to original window with title: " + driver.getTitle());
} finally {
driver.quit();
}
}
}