After I switch to a new window and complete the task, I want to close that new window and switch to the old window,
so here i written like code:
// P
public class First {
public static void main(String[] args) {
System.out.println("Welcome to Selenium");
WebDriver wd= new FirefoxDriver();
wd.manage().window().maximize();
wd.get("http://opensource.demo.orangehrmlive.com/");
wd.findElement(By.id("txtUsername")).sendKeys("Admin");
wd.findElement(By.id("txtPassword")).sendKeys("admin");
wd.findElement(By.id("btnLogin")).submit();
**wd.quit(); //--> this helps to close the web window automatically**
System.out.println("Tested Sucessfully ");
}
}
//store instance of main window first using below code
String winHandleBefore = driver.getWindowHandle();
Perform the click operation that opens new window
//Switch to new window opened
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
driver.close(); //this will close new opened window
//switch back to main window using this code
driver.switchTo().window(winHandleBefore);
// perform operation then close and quit
driver.close();
driver.quit();
The logic you've used for switching the control to popup is wrong
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
How the above logic will swtich the control to new window ?
Use below logic to switch the control to new window
// get all the window handles before the popup window appears
Set beforePopup = driver.getWindowHandles();
// click the link which creates the popup window
driver.findElement(by).click();
// get all the window handles after the popup window appears
Set afterPopup = driver.getWindowHandles();
// remove all the handles from before the popup window appears
afterPopup.removeAll(beforePopup);
// there should be only one window handle left
if(afterPopup.size() == 1) {
driver.switchTo().window((String)afterPopup.toArray()[0]);
}
// Perform the actions on new window
**`//Close the new window`**
driver.close();
//perform remain operations in main window
//close entire webDriver session
driver.quit();
To close a single browser window:
driver.close();
To close all (parent+child) browser windows and end the whole session:
driver.quit();
There are 2 ways to close the single child window:
Way 1:
driver.close();
Way 2: By using Ctrl+w keys from keyboard:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
There are some instances where a window will close itself after a valid window handle has been obtained from getWindowHandle() or getWindowHandles().
There is even a possibility that a window will close itself while getWindowHandles() is running, unless you create some critical section type code (ie freeze the browser while running the test code, until all window management operations are complete)
A quicker way to check the validity of the current driver is to check the sessionId, which is made null by driver.close() or by the window closing itself.
The WebDriver needs to be cast to the remote driver interface (RemoteWebDriver) in order to obtain the sessionId, as follows:
if (null == ((RemoteWebDriver)driver).sessionId) {
// current window is closed, switch to another or quit
} else {
// current window is open, send commands or close
}
Also note that closing the last window is equivalent to quit().