Working surgery for Watin.Net. All examples were created within Visual Studio 2008 set up for C# language. My aim is to show simple complete examples of working code mainly focused around google's website. By doing this will allow you the user to apply these examples to an actual website for themselves. If you are new to WatiN I suggest you follow the posts in order.

Tuesday 24 February 2009

Post Three - Set Up

You will need Visual Studio installed, all examples are build with 2008 version but I'm sure 2003 or 2005 should work just as well. The examples use the C# language as this appears the most popular at present.

So let's start by creating our project. Start Visual Studio, from the 'File' menu select the following options: 'New/Project'. The 'New Project' dialog will display. Ensure that the 'Windows' option displayed within the left hand side 'Project Types' pane is highlighted. Now select and highlight on the 'Console Application' option displayed within the right hand 'Templates' pane. Within the 'Name' textbox displayed under both panes enter the name as: 'WatinTestGround' this automatically changes the 'Solution Name' textbox to match. Leave the 'Location' textbox to it's default unless you have your own source code structure in place. Click the 'OK' button to generate your project.

Your project will open displaying the following page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WatinTestGround
{
class Program
{
static void Main(string[] args)
{
}
}
}


We need add the following line before our 'Main' method within our 'Program' class.
[STAThread]
Note: Include the open\close square brackets. So your class code should
look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WatinTestGround
{
class Program
{
[STAThread]
static void Main(string[] args)
{

}
}
}


For a full explanation on the reason for this refer to the following documentation:
http://watin.sourceforge.net/apartmentstateinfo.html

We now need to created two further class files within our project. To do this right click on the 'WatinTestGround' class name within the 'Solution Explorer' usually found on the right hand side of the VS interface. If this is not displayed you can display it by clicking the 'Solution Explorer' option found under the 'View' menu option. After right clicking the above option from the dialog box displayed click the 'Add/Class' option. Enter into the displayed 'Add New Item' dialog within the 'Name' textbox 'WorkingExamples.cs' and click the 'Add' button. Right click the 'WatinTestGround' class again within 'Solution Explorer' and add a further class naming it 'Element_Id.cs'.

Now we need to make sure our 'WorkingExamples' class is in the correct format. So within 'Solution Explorer' click and display the 'WorkingExample.cs' class. When it loads it should look like this:

using WatiN.Core;
using WatiN.Core.Interfaces;

namespace WatinTestGround
{
public static class WorkingExamples
{

}
}

If you need to change anything in your class to match the above do it and save your changes.

Lastly we need to reference our Watin libary (installed earlier) so our project has access to all the required watin classes. Right click the 'Reference' option found within the 'Solution Explorer' within our 'WatinTestGround' class and click on the 'Add Reference...' option. The 'Add Reference' dialog will display (there may be a short delay before it loads). Click on the 'Browse' tab and using the file dialog displayed navigate to our newly created 'Watin2.0' folder. Locate the 'Bin' folder and select and highlight the 'watiN.Core.dll' file and click the 'OK' button. Thats it we have referenced our project to the watin librarys.

No comments:

Post a Comment