Every day when i open up my computer, i open Yahoo mail, Gmail, hotmail and facebook. This is my every day routine. What if this can be automated ? You are automatically logged into your mail accounts and everything is ready when you start your PC ? Pretty cool han ?

Let’s try a small example and learn how to automate the Web. For an example, we will the following Steps

  1. Open IE or any other browser
  2. Go to Google.com
  3. Type in some text
  4. Get the number of results returned by Google

Steps for the solution

1- Download Watin package from the following URL first

http://sourceforge.net/projects/watin/files/WatiN%202.x/

2- Unpack the zip file and open the BIN folder.

3- Add the reference of the following 2 DLL’s in your project

  • Interop.SHDocVw.dll
  • WatiN.Core.dll

4- Following is the code for automating the steps mentioned above.


// Open a new Internet Explorer window and
// goto the google website.
IE myIE = new IE("http://www.google.com");

// Find the search text field and type Systems.
// In the source of IE window find the Text Field element with name property of "q" and type in the Text.
myIE.TextField(Find.ByName("q")).TypeText(txtInput.Text.Trim());

// Click the Google search button.
// In the source of IE window find the Button with name property of "btnK" and then click it.
myIE.Button(Find.ByName("btnK")).Click();

// Find the para which shows the search result statistics and get text.
//In the source of IE find the Div element with the ID property of "resultStats" and get that text.
string resultStats = myIE.Div(Find.ById("resultStats")).Text;

// Write these statistics to the Message box.
lblResult.Text = resultStats;

5- Done. You can download the attached project to see the working example.

 

Note: I use Google Chrome to get the properties i want to automate. Forexample, in the code above (line 6) i have used a property “q”.Following are the steps to find the properties.

1- Open Google.com in Chrome

2- Right click on the text box of google and select Inpect Element

 

3- See the following image. In the HTML of the google page, the textbox name property is “q” so i am using this name property (“q”) to automate.

 

Extra notes:

1- You can automate an element on any property like class,id,name,text etc

2- You can do the automation process in the background

References:

http://watin.org/

Download the example project:

WatinTesting