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

推荐订阅源

MyScale Blog
MyScale Blog
A
About on SuperTechFans
G
Google Developers Blog
B
Blog RSS Feed
F
Fortinet All Blogs
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Jina AI
Jina AI
IT之家
IT之家
P
Proofpoint News Feed
美团技术团队
量子位
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
B
Blog
有赞技术团队
有赞技术团队
U
Unit 42

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