We come across to handle switching b/w windows while accessing different webpages. Inroder to automate switching b/w windows, following steps will help you to write Selenium RC Script with the help of Java
The major command that will help to switch b/w windows is selectWindow(). Using thisSelenium Rc command we achive the desired automation script.
Following is the Selenium RC with Java script to handle popup windows:
// Code for JUnit
package com.seleniurc;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import static org.junit.Assert.*;
public class MultiWindows {
private Selenium selenium;
private SeleniumServer seleniumserver;
private RemoteControlConfiguration rc;
@Before
public void setUp() throws Exception {
rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://");
seleniumserver.start();
selenium.start();
}
@Test
public void testMultiWindows() throws Exception {
//To open targeted webpage
selenium.open("http://www.quackit.com/html/codes/");
// To hold the executing script for 5000 ms
Thread.sleep(5000);
selenium.click("link=Pop up windows");
selenium.waitForPageToLoad("30000");
// Perform click operation to open popup window
selenium.click("link=Open a popup window");
// waiting for popup window
selenium.waitForPopUp("popUpWindow", "30000");
// To switch b/w main window to popup window
selenium.selectWindow("name=popUpWindow");
Thread.sleep(3000);
// executing text present command on popup window
assertTrue(selenium.isTextPresent("Copy/Paste HTML Codes"));
// closing popup window
selenium.close();
// Switching control to main window
selenium.selectWindow(null);
// executing text present command on popup window
assertTrue(selenium.isTextPresent("Open a popup window"));
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();
}
}
// Code for TestNG
package com.seleniurc;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import com.thoughtworks.selenium.DefaultSelenium;
public class SwitchWindows {
private DefaultSelenium selenium;
private SeleniumServer server;
private RemoteControlConfiguration rcc;
@Test
public void testSwitchWindows() throws InterruptedException {
selenium.open("http://www.quackit.com/html/codes/");
assert(selenium.isTextPresent("HTML Codes - Free"));
selenium.click("link=Pop up windows");
//selenium.waitForPageToLoad("30000");
assert(selenium.isTextPresent("HTML Popup Window Code"));
Thread.sleep(3000);
selenium.click("link=Open a popup window");
selenium.waitForPopUp("popUpWindow", "30000");
selenium.selectWindow("name=popUpWindow");
assert(selenium.isTextPresent("Copy/Paste HTML Codes"));
selenium.close();
selenium.selectWindow(null);
assert(selenium.isTextPresent("HTML Popup Window Code"));
Thread.sleep(3000);
}
@BeforeClass
public void beforeClass() throws Exception {
rcc = new RemoteControlConfiguration();
server = new SeleniumServer(rcc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
server.start();
selenium.start();
selenium.windowMaximize();
}
@AfterClass
public void afterClass() {
selenium.stop();
server.stop();
}
}
The major command that will help to switch b/w windows is selectWindow(). Using thisSelenium Rc command we achive the desired automation script.
Following is the Selenium RC with Java script to handle popup windows:
// Code for JUnit
package com.seleniurc;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import static org.junit.Assert.*;
public class MultiWindows {
private Selenium selenium;
private SeleniumServer seleniumserver;
private RemoteControlConfiguration rc;
@Before
public void setUp() throws Exception {
rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://");
seleniumserver.start();
selenium.start();
}
@Test
public void testMultiWindows() throws Exception {
//To open targeted webpage
selenium.open("http://www.quackit.com/html/codes/");
// To hold the executing script for 5000 ms
Thread.sleep(5000);
selenium.click("link=Pop up windows");
selenium.waitForPageToLoad("30000");
// Perform click operation to open popup window
selenium.click("link=Open a popup window");
// waiting for popup window
selenium.waitForPopUp("popUpWindow", "30000");
// To switch b/w main window to popup window
selenium.selectWindow("name=popUpWindow");
Thread.sleep(3000);
// executing text present command on popup window
assertTrue(selenium.isTextPresent("Copy/Paste HTML Codes"));
// closing popup window
selenium.close();
// Switching control to main window
selenium.selectWindow(null);
// executing text present command on popup window
assertTrue(selenium.isTextPresent("Open a popup window"));
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();
}
}
// Code for TestNG
package com.seleniurc;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import com.thoughtworks.selenium.DefaultSelenium;
public class SwitchWindows {
private DefaultSelenium selenium;
private SeleniumServer server;
private RemoteControlConfiguration rcc;
@Test
public void testSwitchWindows() throws InterruptedException {
selenium.open("http://www.quackit.com/html/codes/");
assert(selenium.isTextPresent("HTML Codes - Free"));
selenium.click("link=Pop up windows");
//selenium.waitForPageToLoad("30000");
assert(selenium.isTextPresent("HTML Popup Window Code"));
Thread.sleep(3000);
selenium.click("link=Open a popup window");
selenium.waitForPopUp("popUpWindow", "30000");
selenium.selectWindow("name=popUpWindow");
assert(selenium.isTextPresent("Copy/Paste HTML Codes"));
selenium.close();
selenium.selectWindow(null);
assert(selenium.isTextPresent("HTML Popup Window Code"));
Thread.sleep(3000);
}
@BeforeClass
public void beforeClass() throws Exception {
rcc = new RemoteControlConfiguration();
server = new SeleniumServer(rcc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
server.start();
selenium.start();
selenium.windowMaximize();
}
@AfterClass
public void afterClass() {
selenium.stop();
server.stop();
}
}