Here is a code snippet from book which I am using to learn Selenium
public class WindowHandlingTest {
WebDriver driver;
@BeforeMethod
public void setup() throws IOException {
System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
driver = new ChromeDriver();
driver.get("http://guidebook.seleniumacademy.com/Window.html");
}
@Test
public void handleWindow() {
String firstWindow = driver.getWindowHandle();
System.out.println("First Window Handle is: " + firstWindow);
WebElement link = driver.findElement(By.linkText("Google Search"));
link.click();
String secondWindow = driver.getWindowHandle();
System.out.println("Second Window Handle is: " + secondWindow);
System.out.println("Number of Window Handles so for: "
+ driver.getWindowHandles().size());
The problem of this code is, that when a new tab is opened, selenium still thinks that the first tab is opened, which makes the results absolutely wrong. Only when I create ArrayList of all windows and refer to specific tab/window by index, the code works as intended. Are there other "correct" ways of managing browser tabs/windows? Is the code from the book is incorrect?