简体   繁体   English

F#机盖关闭弹出窗口

[英]F# Canopy Close Popup Window

I'm testing a web page with Canopy that has a popup window. 我正在用Canopy测试一个具有弹出窗口的网页。 Does anyone know if there is a way with Canopy to close the popup window? 有谁知道Canopy是否可以关闭弹出窗口?

Otherwise I guess it's a case of dipping into Selenium to handle this? 否则我想这是浸入Selenium来处理此事的情况?

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 在弹出窗口中运行browser.Close()
  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM