WaitN中Alert,Confirm,FileUpload对话框使用

 

在自动化测试中,这三种对话框是相当常见:

下面的测试环境:

Win7 Vs2010 WaintN2.1  IE9

 

首先以管理员权限启动Vs2010,创建一个默认的Web项目及其测试项目,如下所示:
WaitN中Alert,Confirm,FileUpload对话框使用_第1张图片

 

1、confirm对话框使用

我们这里面测试的是asp.net Ajax控件

打开default.apsx,加入如下内容:

   1: <asp:UpdatePanel ID="UpdatePanel1" runat="server">
   2:        <ContentTemplate>
   3:            <fieldset>
   4:                <legend>异步获取时间测试,并且使用弹出对话框进行确认legend>
   5:                <hr />
   6:                当前时间:<asp:Label ID="lblDate" runat="server">asp:Label><asp:Button ID="btnGetDate"
   7:                    runat="server" Text="获取当前时间" OnClientClick="return confirm('获取当前时间吗?');" OnClick="btnGetDate_Click" />
   8:            fieldset>
   9:        ContentTemplate>
  10:    asp:UpdatePanel>

 

后台代码,如下:

   1: protected void btnGetDate_Click(object sender, EventArgs e)
   2:         {
   3:             this.lblDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
   4:         }

实现的功能就是,通过异步的方式获取服务器的当前时间,在调用服务器前会询问用户是否获取服务器的当前时间,

对于处理Confirm对话框,先对浏览器加入如下扩展方法:

   1: public static void CaptureConfirmDialog(this Browser browser, Action operation, bool isOk, int waitTimeInSeconds)
   2:         {
   3:             var handler = new ConfirmDialogHandler();
   4:             using (new UseDialogOnce(browser.DialogWatcher, handler))
   5:             {
   6:                 operation(handler);
   7:                 if (waitTimeInSeconds > 0)
   8:                     handler.WaitUntilExists(waitTimeInSeconds);
   9:                 else
  10:                     handler.WaitUntilExists();
  11:  
  12:                 if (handler.Exists())
  13:                     if (isOk)
  14:                         handler.OKButton.Click();
  15:                     else
  16:                         handler.CancelButton.Click();
  17:  
  18:                 browser.WaitForComplete();
  19:             }
  20:         }
  21:  
  22:         public static void CaptureConfirmDialog(this Browser browser, Action operation, bool isOk)
  23:         {
  24:             CaptureConfirmDialog(browser, operation, isOk, 0);
  25:         }

测试代码如下:

   1: [TestMethod]
   2:        public void TestConfirmText()
   3:        {
   4:            using (IE ie = GetIE())
   5:            {
   6:                Span span = ie.Span(c => c.Id.EndsWith("lblDate"));
   7:                string str = span.Text;
   8:                Button btn = ie.Button(c => c.Id.EndsWith("btnGetDate"));//点击id为"su"的按钮,就是百度的搜索按钮
   9:                ie.CaptureConfirmDialog(c => btn.ClickNoWait(), true);
  10:                span = ie.Span(c => c.Id.EndsWith("lblDate"));
  11:               
  12:                Assert.AreNotEqual(str, span.Text);
  13:                str = span.Text;
  14:                ie.WaitForComplete();
  15:                ie.CaptureConfirmDialog(c => btn.ClickNoWait(), false);
  16:              
  17:                span = ie.Span(c => c.Id.EndsWith("lblDate"));
  18:                Assert.AreEqual(str, span.Text);
  19:                str = span.Text;
  20:  
  21:  
  22:                ie.CaptureConfirmDialog(c => btn.ClickNoWait(), true);
  23:               
  24:                span = ie.Span(c => c.Id.EndsWith("lblDate"));
  25:                Assert.AreNotEqual(str, span.Text);
  26:            }
  27:        }

测试结果:

image

 

2、Alert对话框使用

在default.apsx页面中,加入如下内容:

   1: <asp:UpdatePanel ID="UpdatePanel2" runat="server">
   2:         <ContentTemplate>
   3:             <fieldset>
   4:                 <legend>Alert对话框测试legend>
   5:                 <hr />
   6:                 <asp:Button ID="btnAlert" runat="server" OnClientClick="javascript:window.alert('弹出窗体。');return false;"
   7:                     Text="显示Alert" />
   8:             fieldset>
   9:         ContentTemplate>
  10:     asp:UpdatePanel>

 

同样,也加入如下扩展方法:

   1: public static void CaptureAlertDialog(this Browser browser, Action operation, int waitTimeInSeconds)
   2:         {
   3:             var handler = new AlertDialogHandler();
   4:             using (new UseDialogOnce(browser.DialogWatcher, handler))
   5:             {
   6:                 operation(handler);
   7:                 if (waitTimeInSeconds > 0)
   8:                     handler.WaitUntilExists(waitTimeInSeconds);
   9:                 else
  10:                     handler.WaitUntilExists();
  11:  
  12:                 if (handler.Exists())
  13:                     handler.OKButton.Click();
  14:  
  15:                 browser.WaitForComplete();
  16:             }
  17:         }
  18:         public static void CaptureAlertDialog(this Browser browser, Action operation)
  19:         {
  20:             CaptureAlertDialog(browser, operation, 0);
  21:         }

测试代码如下:

   1: [TestMethod]
   2:        public void TestAlertText()
   3:        {
   4:            using (IE ie = GetIE())
   5:            {
   6:                Button btn = ie.Button(c => c.Id.EndsWith("btnAlert"));
   7:                ie.CaptureAlertDialog(obj => btn.ClickNoWait());
   8:            }
   9:        }

测试结果:

image

 

3、FileUpload使用

在default.aspx中加入如下内容:

   1: <asp:UpdatePanel ID="UpdatePanel4" runat="server">
   2:         <ContentTemplate>
   3:             <fieldset>
   4:                 <legend>文件上载测试legend>
   5:                 <hr />
   6:                 文件名:<asp:FileUpload ID="fuLoadFile" runat="server" /><asp:Button ID="btnUpload" runat="server"
   7:                     Text="上传" OnClick="btnUpload_Click" /><br />
   8:                 <asp:Label ID="lblInfo" runat="server">asp:Label>
   9:             fieldset>            
  10:         ContentTemplate>
  11:         <Triggers>
  12:             <asp:PostBackTrigger ControlID="btnUpload" />
  13:         Triggers>
  14:     asp:UpdatePanel>

后台代码如下:

   1: protected void btnUpload_Click(object sender, EventArgs e)
   2:         {
   3:             if(fuLoadFile.HasFile)
   4:             {
   5:                 lblInfo.Text = "上载成功。";
   6:             }
   7:             else
   8:             {
   9:                 lblInfo.Text = "上载失败";
  10:             }
  11:         }

测试代码如下:

   1: [TestMethod]
   2:         public void TestFileUpload()
   3:         {
   4:             using (IE ie = GetIE())
   5:             {
   6:                 
   7:                 Button btn = ie.Button(c => c.Id.EndsWith("btnUpload"));
   8:                 FileUpload fileUpload = ie.FileUpload(c => c.Id.EndsWith("fuLoadFile"));
   9:                 fileUpload.Set("E:\\taobao.txt");
  10:                 btn.Click();
  11:                 ie.WaitForComplete();
  12:                 var span = ie.Span(c => c.Id.EndsWith("lblInfo"));
  13:                 Assert.AreEqual("上载成功。", span.Text);
  14:             }
  15:         }

测试结果:

WaitN中Alert,Confirm,FileUpload对话框使用_第2张图片

 

测试完整代码:

   1: [TestClass]
   2:     public class WaitNTest
   3:     {
   4:         private IE GetIE(string url)
   5:         {
   6:             IE ie = new IE(url);
   7:             ie.AutoClose = true;
   8:             return ie;
   9:         }
  10:  
  11:         private IE GetIE()
  12:         {
  13:             return GetIE("http://localhost:10195/Default.aspx");
  14:         }
  15:  
  16:        
  17:  
  18:         [TestMethod]
  19:         public void TestAlertText()
  20:         {
  21:             using (IE ie = GetIE())
  22:             {
  23:                 Button btn = ie.Button(c => c.Id.EndsWith("btnAlert"));
  24:                 ie.CaptureAlertDialog(obj => btn.ClickNoWait());
  25:             }
  26:         }
  27:  
  28:         [TestMethod]
  29:         public void TestConfirmText()
  30:         {
  31:             using (IE ie = GetIE())
  32:             {
  33:                 Span span = ie.Span(c => c.Id.EndsWith("lblDate"));
  34:                 string str = span.Text;
  35:                 Button btn = ie.Button(c => c.Id.EndsWith("btnGetDate"));//点击id为"su"的按钮,就是百度的搜索按钮
  36:                 ie.CaptureConfirmDialog(c => btn.ClickNoWait(), true);
  37:                 span = ie.Span(c => c.Id.EndsWith("lblDate"));
  38:                
  39:                 Assert.AreNotEqual(str, span.Text);
  40:                 str = span.Text;
  41:                 ie.WaitForComplete();
  42:                 ie.CaptureConfirmDialog(c => btn.ClickNoWait(), false);
  43:               
  44:                 span = ie.Span(c => c.Id.EndsWith("lblDate"));
  45:                 Assert.AreEqual(str, span.Text);
  46:                 str = span.Text;
  47:  
  48:  
  49:                 ie.CaptureConfirmDialog(c => btn.ClickNoWait(), true);
  50:                
  51:                 span = ie.Span(c => c.Id.EndsWith("lblDate"));
  52:                 Assert.AreNotEqual(str, span.Text);
  53:             }
  54:         }
  55:  
  56:         [TestMethod]
  57:         public void TestFileUpload()
  58:         {
  59:             using (IE ie = GetIE())
  60:             {
  61:                 
  62:                 Button btn = ie.Button(c => c.Id.EndsWith("btnUpload"));
  63:                 FileUpload fileUpload = ie.FileUpload(c => c.Id.EndsWith("fuLoadFile"));
  64:                 fileUpload.Set("E:\\taobao.txt");
  65:                 btn.Click();
  66:                 ie.WaitForComplete();
  67:                 var span = ie.Span(c => c.Id.EndsWith("lblInfo"));
  68:                 Assert.AreEqual("上载成功。", span.Text);
  69:             }
  70:         }
  71:     }

你可能感兴趣的:(WaitN中Alert,Confirm,FileUpload对话框使用)