StrikerVillain
StrikerVillain

Reputation: 3776

Switch to parent window after child window is closed by driver.close() throws Exception

I have a scenario where I perform the following steps:

  1. Open browser --> navigate to and click on link which opens a new window
  2. Switch to child window using driver.switchTo().window(handle) --> perform operations on the child window.
  3. Close the child window.
  4. Switch back to the parent window.
  5. Get the title of the current window.

Steps 1 to 3 works just fine. But step 4 throws an exception. And for better clarity I have included the log messages before the exception is throw.n

// Switching to child window
[testng] 23:40:33.794 INFO - Executing: [switch to window: 3efe9598-2a08-40ec-a950-d4a6e2182b20])
[testng] W 2016-02-08 23:40:33:808 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
[testng] W 2016-02-08 23:40:33:821 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
[testng] 23:40:33.942 INFO - Done: [switch to window: 3efe9598-2a08-40ec-a950-d4a6e2182b20]
[testng] 23:40:33.944 INFO - Executing: [get title])
[testng] 23:40:33.955 INFO - Done: [get title]
[testng] 23:41:34.708 INFO - Executing: [close window])
[testng] 23:41:34.719 INFO - Done: [close window]
// Switch to parent window
[testng] 23:41:34.751 INFO - Executing: [switch to window: b2dd5b6e-c891-498f-871b-1fc80b4afef2])
[testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
[testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(504) Unable to find current browser
[testng] W 2016-02-08 23:41:34:765 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
[testng] W 2016-02-08 23:41:34:865 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
[testng] W 2016-02-08 23:42:34:875 response.cc(69) Error response has status code 21 and message 'Timed out waiting for page to load.' message
[testng] 23:42:34.879 WARN - Exception thrown
[testng] org.openqa.selenium.TimeoutException: Timed out waiting for page to load. (WARNING: The server did not provide any stacktrace information)
[testng] Command duration or timeout: 60.13 seconds
[testng] Build info: version: '2.43.1', revision: '5163bceef1bc36d43f3dc0b83c88998168a363a0', time: '2014-09-10 09:43:55'
[testng] System info: host: 'x-x-x-x', ip: 'x.x.x.x', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
[testng] Driver info: org.openqa.selenium.ie.InternetExplorerDriver
[testng] Capabilities [{browserAttachTimeout=0, enablePersistentHover=false, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:42778/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=true, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=accept}]`

We are using a special framework based on flashSelenium and our developers here have managed to integrate flash Selenium with WebDriver (Originally flash Selenium was intended to work with RC). From my understanding, our framework involves flashSelenium, RC and Webdriver components. I am new to the team and the framework API is not in active development (no support).

When I put a try catch around step 4, step 5 is executed correctly. I can see that the exception block around step 4 printing the error!!

So here goes my question: Does driver.switchTo().window(parentWindow) give an exception as shown above in any circumstance?

As from the below log segment I can see that the switch to window executes but the internal method of switch window throws an Exception as the consecutive lines suggest.

[testng] 23:41:34.751 INFO - Executing: [switch to window: b2dd5b6e-c891-498f-871b-1fc80b4afef2])
[testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
[testng] W 2016-02-08 23:41:34:753 IECommandExecutor.cpp(504) Unable to find current browser
[testng] W 2016-02-08 23:41:34:765 Browser.cpp(241) Unable to get window name, IHTMLWindow2::get_name failed or returned a NULL value
[testng] W 2016-02-08 23:41:34:865 IECommandExecutor.cpp(595) Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20
[testng] W 2016-02-08 23:42:34:875 response.cc(69) Error response has status code 21 and message 'Timed out waiting for page to load.' message
[testng] 23:42:34.879 WARN - Exception thrown

Code looks like below

String parentHandle = driver.getWindowHandle();
// Click on the element code goes here (This is in flash selenium)
Set<String> windowhandles = driver.getWindowHandles(); 
for (String windowHandle : windowHandles) {
    driver.switchTo().window(windowHandle);
}

System.out.println("Child title : " + driver.getTitle());
driver.close;
// Exception is thrown in the below line. If a try catch is placed
// the last line gets executed (sysout). But I can see the catch getting executed.
driver.switchTo().window(parentHandle);
System.out.println("Child title : " + driver.getTitle());

Upvotes: 0

Views: 3544

Answers (1)

Guy
Guy

Reputation: 50809

You are correct, Selenium doesn't switch automatically to parent window after using driver.close(). When you tried driver.switchTo().window(currentWindow) the driver was still focused on the closed window's window handle, that what caused the Unable to find managed browser with id 3efe9598-2a08-40ec-a950-d4a6e2182b20 and Unable to find current browser messages in the error log.

If you want to switch back to parent handle you should save it before the switch

String parentHandle = driver.getWindowHandle();
driver.switchTo().window(childHandle); // switch to child window
driver.close(); // close child window
driver.switchTo().window(parentHandle); // switch focus back to parent window

Upvotes: 1

Related Questions