我的第一个WatiN测试项目
真的是非常强大的工具,把它收入我的工具箱了!
Page html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="input" OnClick="Button2_Click" /></div>
</form>
</body>
</html>
Page cs代码:
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security;
using System.Security.Principal;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
[PrincipalPermission(SecurityAction.Demand,Name="asnet\\yzhou")]
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
AppDomain appDomain = AppDomain.CurrentDomain;
appDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal wp = (WindowsPrincipal)Thread.CurrentPrincipal;
this.TextBox1.Text = wp.Identity.Name.ToString();
Response.Write(this.TextBox1.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write(this.TextBox1.Text);
}
}
测试代码:
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WatiN.Core;
using WatiN.Core.Interfaces;
using WatiN.Core.UnitTests;
namespace TestProject
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
//
// TODO: Add constructor logic here
//
}
#region Additional test attributes
//
// You can use the following additional attributes as you write your tests:
//
// Use ClassInitialize to run code before running the first test in the class
// [ClassInitialize()]
// public static void MyClassInitialize(TestContext testContext) { }
//
// Use ClassCleanup to run code after all tests in a class have run
// [ClassCleanup()]
// public static void MyClassCleanup() { }
//
// Use TestInitialize to run code before running each test
// [TestInitialize()]
// public void MyTestInitialize() { }
//
// Use TestCleanup to run code after each test has run
// [TestCleanup()]
// public void MyTestCleanup() { }
//
#endregion
[TestMethod]
public void TestMethod1()
{
//
// TODO: Add test logic here
IBrowser ie = BrowserFactory.Create(BrowserType.FireFox);
//ie = new IE("http://localhost:13425/SecurityPractise/Default.aspx");
ie.GoTo("http://localhost:13425/SecurityPractise/Default.aspx");
//firefox = new firefox(
//ie.TextField(Find.ById("Button1").)ByName("q")).Value = "WatiN";
ie.Button(Find.ById("Button1")).Click();
Assert.IsTrue(ie.ContainsText(@"ASNET\yzhou"));
System.Threading.Thread.Sleep(2000);
ie.TextField(Find.ById("TextBox1")).Value = "WatiN";
ie.Button(Find.ById("Button2")).Click();
Assert.IsTrue(ie.ContainsText(@"WatiN"));
System.Threading.Thread.Sleep(2000);
//
}
}
}