zoukankan      html  css  js  c++  java
  • WatiN——Web自动化测试(三)【弹出窗口处理】

        上一节我们说了关于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自动化测试中,一般遇到的弹出窗口也就上面的四种,基本上都可以解决网页中的问题。
  • 相关阅读:
    /bin/bash^M: bad interpreter: No such file or dire
    ****LINUX命令(含GIT命令)个人总结
    创建和编辑 crontab 文件
    Linux下用于查看系统当前登录用户信息的4种方法
    linux下cat命令详解
    crontab 指定执行用户
    crontab定时运行git命令 更新代码库
    ubuntu添加环境变量【原创】
    ubuntu下设置环境变量的三种方法【转】
    笔记三、apache搭建gitweb【转】
  • 原文地址:https://www.cnblogs.com/stonespawn/p/2009341.html
Copyright © 2011-2022 走看看