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.

Friday 6 March 2009

Post Four - Google Search Example

Now at last we can start some coding. We need to add a method to our 'WorkingExamples' class. Write or copy the following into our class:

public static public void GoogleSearch()
{
//Line 1
IBrowser browser = BrowserFactory.Create(BrowserType.InternetExplorer);
//Line 2.
browser.GoTo("www.google.co.uk");
//Line 3
ITextField searchText = browser.TextField(Find.ByName("q"));
//Line 4
searchText.Value = "Watin";
//Line 5
IButton = searchButton = browser.Button(Find.ByName("btnG"));
//Line 6
searchButton.Click();
//Line 7
browser.Dispose();
}


So the whole class should now look like this:

using WatiN.Core;
using WatiN.Core.Interfaces;
namespace WatinTestGround
{
public static class WorkingExamples
{
public static void GoogleSearch()
{
//Line 1
IBrowser browser = BrowserFactory.Create(BrowserType.InternetExplorer);
//Line 2.
browser.GoTo("www.google.co.uk");
//Line 3
ITextField searchText = browser.TextField(Find.ByName("q"));
//Line 4
searchText.Value = "Watin";
//Line 5
IButton = searchButton = browser.Button(Find.ByName("btnG"));
//Line 6
searchButton.Click();
//Line 7
browser.Dipose();
}
}
}


To run this code we need to reference it from our 'Program' class, so amend the 'Main' method within this class as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WatinTestGround
{
class Program
{
[STAThread]
static void Main(string[] args)
{
WorkingExamples.GoogleSearch();
}
}
}


So let's expain a bit more of whats going on within our 'GoogleSearch' method. First thing to notice is if you wish to put comments within your code to start the line with 2 forward slashes '//'.

Next 'using statements', hear were pulling in the the 'WatiN.Core' librarys that hold all of the classes we need to run WatiN.

Line 1. Now we create our browser object. In this instance we create an IE object but we can easily create a FireFox object the same way.

Line 2. We now tell our browser object to 'GoTo' a url address ie. Google.

A brief explaination regarding lines 3 & 5. We have created our browser object (browser) but every button, textbox, link, frame etc displayed in our browser is an object in itself. So we must create objects of the same type we wish to interact with within our browser object. Remember that every object you create has a set of 'Propertys' automatically added to it. To see and access these property's just type in your objects name followed by a .(dot) and you will get a popup list of all the associated propertys.

Line 3. So we must create a 'TextField' object to enter our search query into.

So what line 3 is saying is create me an object called 'searchText' of type 'ItextField' that is in my browser object and identified by the name of 'q'.

Line 4. The 'Value' property of our 'searchText' object is 'Watin', so put this value into the textbox with the name of 'q'.

Line 5. We must create a 'Button' object to click to instigate our search for 'Watin'. So line 5 is saying create me an object called 'searchButton' of type 'IButton' that is in my browser object and identified by the name of 'btnG'.

Line 6. Activate the 'Click' property of my 'searchButton' object.

Line 7. This is our clean up code to release the 'browser' object memory and close our ie browser window. Note: To see the search work and produce data comment out line 7 otherwise the browser gets closed before you see any results.

No comments:

Post a Comment