package _06_WorkingWithWindowSwapping;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class _01_SwappingWindows {

	/**
	 * @summary Create a driver, navigate to a window, capture the first window
	 *          handle, click a link in such a way as to open a second window,
	 *          determine the second window handle then swap to it, interact
	 *          with an object on the second window, swap back to the first
	 *          window, close and then quit the browser
	 * @throws InterruptedException
	 */
	@Test
	public void test() throws InterruptedException {
		//Create a driver
		WebDriver driver = new FirefoxDriver();
		//Define the key chain used to click a link and open a new window
		String selectLinkOpeninNewTab = Keys.chord(Keys.SHIFT, Keys.RETURN);
		//Define a variable to hold the first window handle
		String firstWindowHandle = driver.getWindowHandle();
		//Define a variable to hold the second window handle
		String secondWindowHandle = null;
		
		//Navigate to the desired webpage
		driver.get("http://the-internet.herokuapp.com/");
		//Locate and element and send the key chain to open the second window
		driver.findElement(By.linkText("Dropdown")).sendKeys(selectLinkOpeninNewTab);

		//Wait until the driver contains more than one window handle
		do {
			Thread.sleep(500);
		} while (driver.getWindowHandles().size() < 2);
		//Determine the second window handle
		for (String handle : driver.getWindowHandles()) {
			if (!handle.equalsIgnoreCase(firstWindowHandle)) {
				secondWindowHandle = handle;
			}
		}
		//Switch to the new window
		driver.switchTo().window(secondWindowHandle);
		//Define an element on the second window
		Select select = new Select(driver.findElement(By.id("dropdown")));
		//Interact with the element on the second page
		select.selectByVisibleText("Option 1");
		Thread.sleep(1000);
		select.selectByVisibleText("Option 2");
		Thread.sleep(1000);
		//Switch back to the first window
		driver.switchTo().window(firstWindowHandle);

		//Close the driver
		driver.close();
		//Quit the browser session
		driver.quit();
		System.out.println("First Window Handle: " + firstWindowHandle);
		System.out.println("Second Window Handle: " + secondWindowHandle);
	}
}

@@ -761,16 +761,17 @@
 n %7B%0A%09%09//
+ 
 Create a
@@ -820,24 +820,25 @@
 iver();%0A%09%09//
+ 
 Define the k
@@ -960,24 +960,25 @@
 ETURN);%0A%09%09//
+ 
 Define a var
@@ -1070,24 +1070,25 @@
 ndle();%0A%09%09//
+ 
 Define a var
@@ -1166,15 +1166,14 @@
 ll;%0A
-%09%09
 %0A%09%09//
+ 
 Navi
@@ -1256,16 +1256,17 @@
 %22);%0A%09%09//
+ 
 Locate a
@@ -1410,16 +1410,17 @@
 );%0A%0A%09%09//
+ 
 Wait unt
@@ -1553,16 +1553,17 @@
 2);%0A%09%09//
+ 
 Determin
@@ -1737,24 +1737,25 @@
 %09%09%7D%0A%09%09%7D%0A%09%09//
+ 
 Switch to th
@@ -1819,32 +1819,26 @@
 e);%0A
+%0A
 %09%09//
-Define an element on
+Interact with
  the
@@ -1858,267 +1858,59 @@
 w%0A%09%09
-Select select = new Select(driver.findElement(By.id(%22dropdown%22))
+interactWithNewWindow(driver
 );%0A
+%0A
 %09%09//
-Interact with the element on the second page%0A%09%09select.selectByVisibleText(%22Option 1%22);%0A%09%09Thread.sleep(1000);%0A%09%09select.selectByVisibleText(%22Option 2%22);%0A%09%09Thread.sleep(1000);%0A%09%09//Switch back
+ Return control
  to 
@@ -1978,16 +1978,17 @@
 );%0A%0A%09%09//
+ 
 Close th
@@ -2018,16 +2018,17 @@
 ();%0A%09%09//
+ 
 Quit the
@@ -2199,11 +2199,646 @@
 dle);%0A%09%7D
+%0A%0A%09/**%0A%09 * @summary Contains behavior to interact with a second window, which to%0A%09 *          which control has been given to the driver%0A%09 * @param driver - WebDriver for the test%0A%09 * @throws InterruptedException - Exception handling for Thread.sleep()%0A%09 */%0A%09private void interactWithNewWindow(WebDriver driver) throws InterruptedException %7B%0A%09%09// Define an element on the second window%0A%09%09Select select = new Select(driver.findElement(By.id(%22dropdown%22)));%0A%09%09// Interact with the element on the second page%0A%09%09select.selectByVisibleText(%22Option 1%22);%0A%09%09Thread.sleep(1000);%0A%09%09select.selectByVisibleText(%22Option 2%22);%0A%09%09Thread.sleep(1000);%0A%09%7D
 %0A%7D%0A

add class to demonstrate swapping windows