RE: Want to open new tabs and switch between them

Hello

I have a scenario in which I need to open 2 tabs in the same browser and then perform actions that will run in a loop.

After the loop (set of actions) is complete then close the browser.

Is it possible to open 1st tab -> perform respective actions -> then open 2nd tab and perform  respective actions -> repeat these steps around 20 time s and then close it ????

som Train Asked on January 15, 2015 in Selenium WebDriver.
Add Comment
2 Answers

// 1) Launch new Browser, create an instance of FirefoxDriver.
driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

// 2) Open URL
driver.get(“http://www.toolsqa.com/automation-practice-switch-windows/”);

// 3) Get Window name (Use GetWindowHandle command)
// To get the window handle of the current window.
String handle = driver.getWindowHandle();//Return a string of alphanumeric window handle

System.out.println(handle);

// 4) Click on Button “New Message Window”, it will open a Pop Up Window
driver.findElement(By.name(“New Message Window”)).click();

// Store and Print the name of all the windows open, get all windows
Set<String> handles = driver.getWindowHandles();

// Pass a window handle to the other window
for(String handle1 : driver.getWindowHandles())
{
System.out.println(“Window Handle: ” + handle1);
driver.switchTo().window(handle1);
}

// Store and Print the name of all the windows open
Iterator<String> iteratorP = handles.iterator();
while (iteratorP.hasNext()) {
System.out.println(“Window Handle: ” + iteratorP.next());
}

// 6) Close the Pop Up Window (Use Switch Command to shift window)
driver.close();

driver.switchTo().window(handle); // switch back to parent window

//Close the browser
driver.quit();

Train Answered on January 22, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.