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

推荐订阅源

小众软件
小众软件
N
News and Events Feed by Topic
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
AI
AI
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
H
Hacker News: Front Page
F
Fortinet All Blogs
博客园_首页
S
Secure Thoughts
N
News and Events Feed by Topic
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Spread Privacy
Spread Privacy
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Hugging Face - Blog
Hugging Face - Blog
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
Know Your Adversary
Know Your Adversary
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Scott Helme
Scott Helme
P
Privacy & Cybersecurity Law Blog
S
Securelist
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
O
OpenAI News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
L
LangChain Blog
雷峰网
雷峰网
Security Archives - TechRepublic
Security Archives - TechRepublic
V2EX - 技术
V2EX - 技术

博客园 - 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自动化测试中,一般遇到的弹出窗口也就上面的四种,基本上都可以解决网页中的问题。