3

I'm testing a web page with Canopy that has a popup window. Does anyone know if there is a way with Canopy to close the popup window?

Otherwise I guess it's a case of dipping into Selenium to handle this?

Mattl
  • 1,588
  • 3
  • 24
  • 49
  • 2
    You will have to dip into selenium to handle it. I haven't written a helper function for it. 'browser' is the instance of the webdriver. – lefthandedgoat Aug 03 '14 at 15:40

1 Answers1

3

This is not too difficult. The basic algorithm is:

  1. Get the window handle before you leave the main test thread
  2. Run code that will pop up new window
  3. Bring that window to the fore-front
  4. Run browser.Close() on the pop up window
  5. Switch back to the original window

In code:

let origWindow = browser.CurrentWindowHandle
// do something to open another window
// make the new window the focus of tests
let newWindow = browser.WindowHandles |> Seq.find (fun w -> w <> origWindow )
browser.SwitchTo().Window(newWindow) |> ignore
// optionally do asserts on the contents of the new window
// close the new window
browser.Close()
// switch back to the original window
browser.SwitchTo().Window(origWindow) |> ignore

Assuming this is something you might want to do more than once, you could refactor to the following library functions:

let switchToWindow window =
    browser.SwitchTo().Window(window) |> ignore

let getOtherWindow currentWindow =
    browser.WindowHandles |> Seq.find (fun w -> w <> currentWindow)

let switchToOtherWindow currentWindow =
    switchToWindow (getOtherWindow currentWindow) |> ignore

let closeOtherWindow currentWindow =
    switchToOtherWindow currentWindow
    browser.Close()

The original example could be rewritten:

let origWindow = browser.CurrentWindowHandle
// do something to open another window
// make the new window the focus of tests
switchToOtherWindow origWindow 
// optionally do asserts on the contents of the new window
// close the new window
closeOtherWindow origWindow 
// switch back to the original window
switchToWindow origWindow
Kevin Kershaw
  • 196
  • 2
  • 7