KeyLimeTie Blog

Website Testing - Automation, Autofill, etc. (C# WinForms)

By Brian Pautsch – 3/9/2006 10:16:44 PM. Posted to Applications.

Looking to automate your web testing? Create your own Windows forms robot to test your website (and continue to test as you add more and more features!). Here's a basic example to get you started today:

BE SURE TO CHECK OUT PART 2 OF THIS ARTICLE WHERE FRAMES ARE INVOLVED

Download code

1. Create a new Windows Forms application and add in the "Microsoft Internet Controls" COM Component (shdocvw.dll).

2. Add a "Search" textbox, a couple of radio buttons, a web browser and a "Go" button. On click of the "Go" button, add the following code:
1privatevoid cmdGo_Click(object sender, System.EventArgs e)
2{
3//Navigate to the page4 Object objNull = null;
5 WebBrowser.Navigate("http://www.google.com", ref objNull, ref objNull, ref objNull, ref objNull);
6 mbSearching = true;
7}
3. Handle the web browser's "DocumentComplete" event like s
1privatevoid WebBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
2{
3//If we're not searching, exit4if (!mbSearching)
5return;
67 HTMLDocument myDoc = null;
8try9 {
10//Get the web browser document11  myDoc = new HTMLDocumentClass();
12  myDoc = (HTMLDocument) WebBrowser.Document;
1314//Find the textbox15  HTMLInputElement objTextBox = (HTMLInputElement) myDoc.all.item("q", 0);
16  objTextBox.value = txtSearchFor.Text;
1718//Click the selected button19string strBtn = (rdoClickSearch.Checked ? "btnG" : "btnI");
20  HTMLInputElement btnSearch = (HTMLInputElement) myDoc.all.item(strBtn, 0);
21  btnSearch.click();
22 }
23catch (Exception ex)
24 {
25  MessageBox.Show("Error:" + ex.Message);
26 }
27finally28 {
29//Release memory30  myDoc = null;
31 }
3233//Clear "searching" flag34 mbSearching = false;
35}
Some example screenshots



Comments

On 1/8/2010 Brian Pautsch said:
Herman,

On 1/8/2010 Dustin said:
Hi. I'm new to .NET. I was just wondering if you would tell me how you implemented the "Enter this code below" protection on this 'Leave a Comment' area.

On 8/8/2006 Herman said:
I've been searching for this code everywhere. This is brilliant. My only problem is that I want to let a user log into a website and log in automatically. The problem is that the site they're connecting to, contains frames and when I try and find the textboxes for the username and password, it tells me 'object reference is not set to an instance of an object'. But when I connect directly to the asp page, it finds the controls without a problem. What can I do to display all the frames but search for the control on one of the pages in another frame?

On 5/24/2006 Brian Pautsch said:
Dustin - Check out: http://www.brianpautsch.com/ShowItem28.aspx - I provided all of the source and example application.

On 3/25/2006 Brian Pautsch said:
That's strange...I just downloaded the file from the website and clicked Go and it worked fine. One thing to mention is that there is no delay coded, so the application will go to Google, set the search text and click Search immediately...no hesitation. But, with web latency, you should see it happen and get search results from Google.

On 3/22/2006 Christian Ostensen said:
I downloaded the code. Do I need to modify some feature for it to work? Atthe moment it doesn't do anything when I click the GO button.

Leave a Comment

Name:
Email:
URL:
Comment:
Security Code:
Type Security Code:

Photos on Flickr

More Photos »

Search Blog


Get Email Updates

Like what you read here at KeyLimeTie? Sign up for our email list!

Subscribe