惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
I
Intezer
Y
Y Combinator Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Stack Overflow Blog
Stack Overflow Blog
Hacker News: Ask HN
Hacker News: Ask HN
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
T
Tor Project blog
C
Cybersecurity and Infrastructure Security Agency CISA
云风的 BLOG
云风的 BLOG
博客园_首页
V2EX - 技术
V2EX - 技术
T
Threat Research - Cisco Blogs
腾讯CDC
宝玉的分享
宝玉的分享
博客园 - 叶小钗
罗磊的独立博客
S
Securelist
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
博客园 - 司徒正美
W
WeLiveSecurity
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Secure Thoughts
NISL@THU
NISL@THU
N
News and Events Feed by Topic
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
K
Kaspersky official blog
IT之家
IT之家

博客园 - stonespawn

Vue 组件库2 Vue 的组件库 算法小题目 VS2019 自动代码补全功能 GIT 删除操作 vue-router 注意事项 Vue中axios访问 后端跨域问题 Vue2.0 搭配 axios Vue 脚手架搭建 grunt使用入门 Git相关操作及记录 Ajax在调用含有SoapHeader的webservice方法 关于JS 树形结构 导出EXCEL【Web方式HTML通过拼接html中table】 链接点击跳动问题 WatiN——Web自动化测试(二) WatiN——Web自动化测试(一) 网页调用服务程序 - stonespawn - 博客园 小问题 小技巧 :创建虚拟目录并将IIS里面.net配置版本设为2.0 - stonespawn
WatiN——Web自动化测试(三)【弹出窗口处理】
stonespawn · 2011-04-08 · via 博客园 - stonespawn

    上一节我们说了关于WatiN的自动化的框架的设计,一般的系统应用应该可以。关于Case的本身的编写在实际应用中也会有一些问题和难题。这一节我将

WatiN的弹出框作一下详细的总结。在实际网页中,操作按钮可能弹出各种样式的弹出框,如何进行有效的处理呢?

1、Alert Dialog

Alert对话框很简单,弹出之后只是一个提示作用,弹出之后进行确认即可。

public static void CaptureAlertDialog(this Browser browser, Action<AlertDialogHandler> operation, int waitTimeInSeconds)
{
  var handler = new AlertDialogHandler();
  using (new UseDialogOnce(browser.DialogWatcher, handler))
  {
    operation(handler);
    handler.WaitUntilExists(waitTimeInSeconds);
    if (handler.Exists())
      handler.OKButton.Click();
  }
}

CaptureAlertDialog:是处理alert对话窗方法,其传入的参数分别是:Browser浏览器对象、Acation<AlertDialogHandler> alert句柄、waitTimeSeconds 等待时间

context.Browser.CaptureAlertDialog((AlertDialogHandler handler) => { btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout); }, 5);

btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout); 为button的点击事件。

2、Confirm Dialog

public static void CaptureConfirmDialog(this Browser browser, Action<ConfirmDialogHandler> operation, int waitTimeInSeconds)
{
  var handler = new ConfirmDialogHandler();
  using (new UseDialogOnce(browser.DialogWatcher, handler))
  {
    operation(handler);
    handler.WaitUntilExists(waitTimeInSeconds);
    if (handler.Exists())
    {
      handler.OKButton.Click();//确认按钮 handler.CancelButton.Click();取消按钮
    }
  }
}

context.Browser.CaptureConfirmDialog((ConfirmDialogHandler handler) =>
{
btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout);
}, 5);


btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout);为Button点击事件

3、文件下载对话框FileDownloadHandler

var btn = "获取button按钮";

var fileName = System.Windows.Forms.Application.StartupPath + "保存路径文件名";
FileDownloadHandler fileDownloadHandler = new FileDownloadHandler(fileName);
using (new UseDialogOnce(context.Browser.DialogWatcher, fileDownloadHandler))
{
  btn.WaitUntilExistsAndClickNoWait(context.TestConfig.Timeout);
  context.Browser.WaitUntil(5);
  fileDownloadHandler.WaitUntilFileDownloadDialogIsHandled(60);
  fileDownloadHandler.WaitUntilDownloadCompleted(200);
}

4、网页对话框(window.open)

有的网页对话框通过window.open的方式进行打开的是其他的页面,比如通过其他的页面进行添加分类等等,遇到此处的时候应该如何处理呢?其实这等窗口是页

面,内容可以通过Browser对象来进行获取。

首先需要将主浏览器对象进行保存,让这个browser对象再打开新窗口,从中获取窗口页面的URL

如下代码:

var orginBrowser = context.Browser;//context.Browser浏览器对象
try
{
  Div.Button(btn => btn.ClassName == "ButtonStyle").WaitUntilExistsAndClick(context.TestConfig.Timeout);//Button按钮,进行onclick事件
  context.Browser.WaitUntil(3);//等待3秒
  context.Browser = WatiN.Core.Browser.AttachTo(context.Browser.GetType(), Find.ByUrl(url => url.IndexOf("页面名称") > -1), context.TestConfig.Timeout);//查找新窗口的页面名称
  context.Browser.Refresh();//进行刷新
  ///
  ///对窗口中的内容进行操作
  ///
  context.Browser.Close();
}
catch { }
 context.Browser = orginBrowser;

在WatiN自动化测试中,一般遇到的弹出窗口也就上面的四种,基本上都可以解决网页中的问题。