我正在测试一个带有Canopy的网页,该网页有一个弹出窗口。有没有人知道Canopy是否有办法关闭弹出窗口?
否则我想这是一个浸入Selenium处理这个问题的案例?
答案 0 :(得分:3)
这并不太难。基本算法是:
在代码中:
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
假设这可能是您不止一次要做的事情,您可以重构以下库函数:
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()
原始示例可以重写:
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