maandag 26 oktober 2009

WatiN and MbUnit, a nice combo

Just a shameless repost of a mail Jeff Brown did send to the WatiN mailinglists. Interesting stuff, go check it out!

ANN: New samples for using WatiN with MbUnit.

I just released Gallio / MbUnit v3.1 Update 1.

It's mainly a bug fix release but I also added some new samples
demonstrating one good way of using WatiN with MbUnit.

The sample introduces a new custom attribute called [Browser] with some
fancy behavior like:

* Capturing a screenshot if the test fails (or whenever you like).
* Embedding a video capture of the test run if the test fails (or whenever
you like).
* Running the same test repeatedly with different browsers.

The code is pretty easy to use so if you're already using MbUnit v3 and
WatiN then you should find it pretty easy to incorporate some of these
features from the samples into your own tests.

After installing Gallio v3.1 Update 1, look in %PROGRAMFILES%\Gallio\samples
for the WatiN samples and unzip them to a location of your choice.  You'll
need Visual Studio 2008 SP1 to run them out of the box.

Here's one of the sample tests to give you an idea of how this looks:


       /// <summary>
       /// Runs the same test repeatedly with different browsers.
       /// </summary>
       [Test]
       [Browser(BrowserType.IE)]
       [Browser(BrowserType.FireFox)]
       [Browser(BrowserType.Chrome)]
       public void MultipleBrowsers()
       {
           GoogleSearchPage.GoTo(Browser);
           Browser.Page<GoogleSearchPage>().Search("Fiddlesticks");
       }


Full announcement and download links:
http://blog.bits-in-motion.com/2009/10/announcing-gallio-and-mbunit-v31-upda
te.html


You can also see a sample video capture from a previous release here:
http://www.youtube.com/watch?v=rN0CmutflFs

Enjoy,
Jeff.

Technorati Tags:

3 reacties:

Sergej Andrejev zei

Hi there. First of all thanks for developing such a nice library as WatiN. I first used it early this year and was satisfied with it's ease of use. Now I'm back with need to use it for other project and was wondering when WatiN 2.0 beta2 will be released. Because according to some sources this version should fix black screenshots in internet explorer 8, which would be very handy in my situation.

shakil zei

Hi Watin is a wonderful tool for performing automation testing of web applications. Also that it supports MBUnit that is very nice. But I am currently trying to link watin with MBUnits's DataFixture attribute where i can provide multiple data in xml file and the test gets multiplied automatically. it would be wonderful if that is possible my test gets loaded but they are not running properly
Pasting my code for reference


using System.IO;
using System.Xml;
using Gallio.Framework;
using MbUnit.Framework;
using WatiN.Core;
namespace OTPAdmin.Tests
{
//[TestFixture(ApartmentState = ApartmentState.STA)]
[DataFixture(), XmlDataProvider(@"F:\Shakil\SourceCode\OTPAdmin.Tests\OTPAdmin.Tests\LoginTest.xml", "DataFixture/Data")]
public class LoginUsingXmlTest
{
public static IE window;
[TestFixtureSetUpAttribute]
public static void Setup()
{
window = new IE("about:blank");

}
[TestFixtureTearDown]
public static void TearDown()
{
window.Close();
window.Dispose();
}
[ForEachTest("//Item")]

public static void TestLoginViaGalioUsingXml(XmlNode node)
{
WatiN.Core.Logging.Logger.LogWriter = new GallioLogWatinWriter();
//try
//{
//IE window;
window = new IE("about:blank");
WatiN.Core.Logging.Logger.LogWriter = new GallioLogWatinWriter();
window.GoTo("http://localhost:1944/login.aspx");
window.TextField("txtUsername").TypeText(node.Attributes["Username"].Value);
window.TextField("txtPassword").TypeText(node.Attributes["Password"].Value);
WatiN.Core.Logging.Logger.LogWriter.LogAction("" + node.Attributes["Username"].Value + "::");
window.Button("btnLogin").Click();
Div Div_C = window.Div(Find.ByText("User name or Password is incorrect"));
Assert.IsTrue(Div_C.Exists);
if (TestContext.CurrentContext.Outcome == Gallio.Model.TestOutcome.Failed)
{
string filename = Path.GetTempFileName();
filename = Path.GetTempPath() + Path.GetFileNameWithoutExtension(filename) + ".bmp";
window.CaptureWebPageToFile(filename);
TestLog.EmbedImage("Test Screenshot", System.Drawing.Image.FromFile(filename));

WatiN.Core.Logging.Logger.LogWriter.LogAction("Test Failed::" + node.Attributes["Username"].Value + "::");
}
//window.Close();
//window.Dispose();
//}
//catch (Exception ex)
//{
// WatiN.Core.Logging.Logger.LogWriter.LogAction(ex.ToString());
// WatiN.Core.Logging.Logger.LogWriter.LogAction("Test Failed::" + node.Attributes["Username"].Value + "::");
//}
}
}
}


I am getting the following error when running the test

Message: The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.

Type: System.Threading.ThreadStateException
Source: WatiN.Core
TargetSite: Void CheckThreadApartmentStateIsSTA()
HelpLink: null
Stack: at WatiN.Core.IE.CheckThreadApartmentStateIsSTA()
at WatiN.Core.IE.CreateNewIEAndGoToUri(Uri uri, LogonDialogHandler logonDialogHandler, Boolean createInNewProcess)
at WatiN.Core.IE..ctor(String url)

shakil zei

Got it, Done and working fine for me now


The only thing i was missing was i didnt write the appartmentstate in teh datafixture

[DataFixture(ApartmentState = ApartmentState.STA), ....