Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / Visual Basic

WatiN Test Recorder

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
14 Jun 2007GPL34 min read 148.1K   5.2K   43  
Automate web test recording into C#, VB.NET and PHP
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using WatiN.Core;
using System.Runtime.InteropServices;
using System.Drawing;


namespace DemoApp
{
    [TestFixture]
    public class FunctionPageTest
    {
        [Test]
        public void LoadTest()
        {
            FunctionManager fmgr = new FunctionManager(@"C:\Development\TestRecorder\bin\Debug\Functions");
            //FunctionPage fcn = new FunctionPage(@"c:\Development\TestRecorder\TestFunction.htm");
        }

        [Test]
        public void LoadWindow()
        {
            frmFunctionChooser frm = new frmFunctionChooser();
            frm.ShowDialog();
        }
    }

    [TestFixture]
    public class WatinScriptTest
    {
        [Test]
        public void CompileTest()
        {
            WatinScript script = new WatinScript();
            string Code = "IE ie = new IE();";
            //script.ScriptFormatting = WatinScript.ScriptFormats.Console;
            //script.CompileScript(Code, 0, true);
        }
    }

    [TestFixture]
    public class RunningTests
    {
        frmMain main = new frmMain();
        public static string MainTestURL = @"C:\Program Files\WatiN\trunk\src\UnitTests\html\main.html";

        [SetUp]
        public void OpenWindow()
        {
            main.Show();
            main.wscript.Recording = true;
        }

        [TearDown]
        public void CloseWindow()
        {
            main.wscript.settings.WarnWhenUnsaved = false;
            main.Close();
        }

        [Test]
        public void SurfToGoogle()
        {
            main.txtCode.Clear();
            main.NavToUrl("http://www.google.com");
            StringAssert.Contains("ie.GoTo(\"http://www.google.com\");", main.txtCode.Text);
        }

        [Test]
        public void TestPageClick()
        {
            main.NavToUrl(MainTestURL);
            main.txtCode.Clear();
            /*
            WatiN.Core.IE.Settings.EmbeddedIE = true;
            IE ie = WatiN.Core.IE.AttachToIE(WatiN.Core.Find.ByUrl(MainTestURL));
            RadioButton btn = ie.RadioButton(Find.ById("Radio1"));
            mshtml.IHTMLElement helem = btn.HTMLElement as mshtml.IHTMLElement;
            helem.scrollIntoView(false);
            uint x = (uint)helem.offsetLeft;
            uint y = (uint)helem.offsetTop;
            */

            StringAssert.Contains("ie.RadioButton(Find.ById(\"Radio1\")).Click();", main.txtCode.Text);
        }

        [Test]
        public void TestPageKeys()
        {
            main.NavToUrl(MainTestURL);
            main.txtCode.Clear();
            WatiN.Core.IE.Settings.EmbeddedIE = true;
            IE ie = WatiN.Core.IE.AttachToIE(WatiN.Core.Find.ByUrl(MainTestURL));
            ie.TextField(Find.ById("name")).TypeText("abc");
            StringAssert.Contains("ie.TextField(Find.ById(\"name\")).TypeText(\"abc\");", main.txtCode.Text);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
United States United States
Raised by wolves in the mean streets of Macintosh C/C++ for IBM and Motorola, moved on to Delphi and now C#. Ah, the mother's milk of Microsoft...

Comments and Discussions