My name is Michal Sporek. I am a contractor in IT (software development). I am available for IT contracting jobs working remotely (telecommuting).

Please visit my site to learn more...

Browser automation with WatiN and C# .NET

A few years ago - while trying to create a simple code that could automate operations I do frequently at my bank I found a great library called WatiN. You can download WatiN here and it is free-of-charge. WatiN is meant for automation of testing against web applications, and indeed it makes it easy to write your own tests and then have them running as unit tests. But the scope of WatiN is much more than just to automate tests of web applications and write unit tests. What WatiN actually allows to do, is to automate almost every activity that you do in the internet via webbrowser. With the use of WatiN referenced by your C# .NET code you can write applications that will do search on the internet, log into your bank to check account balance, add new posts on forums, and crawl data from the internet.

What is really special about WatiN is that you not only have a complete control over a webbrowser, you can direct it to anywhere you want, but you also have control over the DOM (Document Object Model) elements so that you can access div, span, img, form, input elements (like input fields, checkboxes, buttons), and many other HTML elements in the document, you can get their properties, you can change their values. You can click buttons and hyperlinks, you can fill up and submit forms, etc.

Below you can find a short example of how WatiN makes it easy to integrate Google Search with your applications:

//First we open a new Internet Explorer browser, making sure it will be visible...
WatIn.Core.IE ie = new WatIn.Core.IE() { Visible = true };

// Then we follow up with the browser to open website.

// Then we find the search box and we put the searched term in it.
// In this example the code will search for phrase: "it blog"
TextField searchField = ie.TextField(Find.ByName("q")); // "q" is the name of the text field on Google Search page.
if (!searchField.Exists)
    // Report an error, because the search box was not found...

searchField.Value = "it blog";

// Now we need to find the button to be clicked to launch the search...
Button launchSearch = ie.Button(Find.ByName("btnG");
if (!launchSearch.Exists)
    // Report an error, because the search button was not found...

ie.NativeDocument.Body.SetFocus(); // Focusing on the document...
launchSearch.Click(); // And launching the search...

// Here we can get the search results from Google...
string fullResultsHtml = ie.Html; // Getting the results in HTML format, and then we can use XML parser to parse it.

The code above is very simple usage of WatiN:

  1. First the code launches a new browser which you can see on the screen.
  2. Then it redirects the browser to go to address.
  3. As the next step we use WatiN to find the search box. Having previously checked the HTML code of Google website, we already know that the search box field has name "q", so we find it by its name, and then we put a value inside it. When you run the code, you will be able to see the text is actually inserted into the search box.
  4. When the text is inserted into the text box, then the code searches for the "Search" button, and when found - it clicks it.
  5. In the end you can see the results returned in the open browser. There are many possible ways you can access the results - either with the Html property of the WatIn.Core.IE class, or with alternative ways, ex. you can direct WatiN to search for DIVs and SPANs inside the document, and get the results from there.

      The capabilities of WatiN are robust, and you can develop various solutions with it - solutions that utilize external services. It is particularly helpful for fetching data from external resources if those resources do not provide a concise API for access.

      With WatiN I have not only managed to satisfy my needs to create a simple C# .NET banking application that checks the banking balance and wire transfers made, but I have created other many applications with it including applications for searching on the web, logging into different websites and crawling data from there, and many others ;-).

Posted by Michal Sporek