In Selenium WebDriver, it uses TargetLocator interface to handle this switch. You may use it to switch to,
- Frame (using index, id or name, WebElement)
- Alert
- Window (using window handle)
Uses "driver.switchTo()" to get TargetLocator, and make sure using "driver.switchTo().defaultContent()" to switch back to main window.
Here is an example to demonstrate all these cases.
package me.simplejavautomation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SwitchApiTest {
private WebDriver driver;
private String url;
@Before
public void setUp() {
driver = new ChromeDriver();
url = "http://simplejavautomation.blogspot.com/2018/07/demo-page-for-selenium-webdriver-apis.html";
// go to the demo page
driver.get(url);
// maximize the browser window
driver.manage().window().maximize();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void testSwitchWindow() {
// wait 10 seconds if web element is not present
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
String currentWindow = driver.getWindowHandle();
// find element
driver.findElement(By.linkText("Selenium")).click();
assertEquals(currentWindow, driver.getWindowHandle());
String[] windowHandles = driver.getWindowHandles().toArray(new String[0]);
assertTrue(windowHandles.length > 1);
driver.switchTo().window(windowHandles[1]);
assertEquals(windowHandles[1], driver.getWindowHandle());
}
@Test
public void testSwitchAlert() {
// wait 10 seconds if web element is not present
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// find element
driver.findElement(By.id("simpleAlert")).click();
Alert alert = driver.switchTo().alert();
assertEquals("Simple Java Automation", alert.getText());
alert.accept();
driver.findElement(By.id("confirmAlert")).click();
alert = driver.switchTo().alert();
assertEquals("Simple Java Automation", alert.getText());
alert.dismiss();
driver.findElement(By.id("promptAlert")).click();
alert = driver.switchTo().alert();
assertEquals("Is Java Automation simple?", alert.getText());
alert.sendKeys("Yes");
alert.accept();
}
@Test
public void testSwitchFrame() {
JavascriptExecutor exe = (JavascriptExecutor) driver;
int numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
assertEquals(6, numberOfFrames);
// By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
assertEquals(6, iframeElements.size());
// switch frame by index
driver.switchTo().frame(0);
// switch back
driver.switchTo().defaultContent();
// switch frame by id
driver.switchTo().frame("iframeA");
driver.switchTo().defaultContent();
WebElement iframeB = driver.findElement(By.id("iframeB"));
// switch frame by web element
driver.switchTo().frame(iframeB);
driver.switchTo().defaultContent();
}
@Test
public void testSwitchFrame2() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// switch frame by id
driver.switchTo().frame("iframeA");
driver.findElement(By.id("street")).sendKeys("Whangaparaoa Rd");
driver.findElement(By.id("suburb")).sendKeys("Red Beach");
driver.findElement(By.id("submit")).click();
Alert alert = driver.switchTo().alert();
assertEquals("Street:Whangaparaoa Rd Suburb:Red Beach", alert.getText());
alert.accept();
// switch back
driver.switchTo().defaultContent();
// switch frame by index
driver.switchTo().frame("iframeB");
driver.findElement(By.id("city")).sendKeys("Auckland");
driver.findElement(By.id("country")).sendKeys("New Zealand");
driver.findElement(By.id("submit")).click();
alert = driver.switchTo().alert();
assertEquals("City:Auckland Country:New Zealand", alert.getText());
alert.accept();
driver.switchTo().defaultContent();
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class SwitchApiTest {
private WebDriver driver;
private String url;
@Before
public void setUp() {
driver = new ChromeDriver();
url = "http://simplejavautomation.blogspot.com/2018/07/demo-page-for-selenium-webdriver-apis.html";
// go to the demo page
driver.get(url);
// maximize the browser window
driver.manage().window().maximize();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void testSwitchWindow() {
// wait 10 seconds if web element is not present
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
String currentWindow = driver.getWindowHandle();
// find element
driver.findElement(By.linkText("Selenium")).click();
assertEquals(currentWindow, driver.getWindowHandle());
String[] windowHandles = driver.getWindowHandles().toArray(new String[0]);
assertTrue(windowHandles.length > 1);
driver.switchTo().window(windowHandles[1]);
assertEquals(windowHandles[1], driver.getWindowHandle());
}
@Test
public void testSwitchAlert() {
// wait 10 seconds if web element is not present
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// find element
driver.findElement(By.id("simpleAlert")).click();
Alert alert = driver.switchTo().alert();
assertEquals("Simple Java Automation", alert.getText());
alert.accept();
driver.findElement(By.id("confirmAlert")).click();
alert = driver.switchTo().alert();
assertEquals("Simple Java Automation", alert.getText());
alert.dismiss();
driver.findElement(By.id("promptAlert")).click();
alert = driver.switchTo().alert();
assertEquals("Is Java Automation simple?", alert.getText());
alert.sendKeys("Yes");
alert.accept();
}
@Test
public void testSwitchFrame() {
JavascriptExecutor exe = (JavascriptExecutor) driver;
int numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
assertEquals(6, numberOfFrames);
// By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
assertEquals(6, iframeElements.size());
// switch frame by index
driver.switchTo().frame(0);
// switch back
driver.switchTo().defaultContent();
// switch frame by id
driver.switchTo().frame("iframeA");
driver.switchTo().defaultContent();
WebElement iframeB = driver.findElement(By.id("iframeB"));
// switch frame by web element
driver.switchTo().frame(iframeB);
driver.switchTo().defaultContent();
}
@Test
public void testSwitchFrame2() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// switch frame by id
driver.switchTo().frame("iframeA");
driver.findElement(By.id("street")).sendKeys("Whangaparaoa Rd");
driver.findElement(By.id("suburb")).sendKeys("Red Beach");
driver.findElement(By.id("submit")).click();
Alert alert = driver.switchTo().alert();
assertEquals("Street:Whangaparaoa Rd Suburb:Red Beach", alert.getText());
alert.accept();
// switch back
driver.switchTo().defaultContent();
// switch frame by index
driver.switchTo().frame("iframeB");
driver.findElement(By.id("city")).sendKeys("Auckland");
driver.findElement(By.id("country")).sendKeys("New Zealand");
driver.findElement(By.id("submit")).click();
alert = driver.switchTo().alert();
assertEquals("City:Auckland Country:New Zealand", alert.getText());
alert.accept();
driver.switchTo().defaultContent();
}
}
No comments:
Post a Comment