This is a very simple automation sample, the automation simulates the user web browsing, I built 2 automation samples:
- Automate Google Search: will navigate to google website, enter "Dareen Alhiyari" in the search textbox, and then click the search button.
- Automate My Blog: The second automation will navigate to my blog's "Contact Me" page, and then click on my yahoo email link.
Download Executable you also need to check Microsoft.mshtml.dll (?)
Download Source Code
You can also build your own toolbar to the browser with your customized buttons, for example you can add a button that will collect all the images in a web page.. whatever comes to your mind!
You can also use this concept to build automated tests for your web applications, simulating the end user browsing activities.
A quick look at the code
In the event handler of the click event of the sub-menu "Automate My Blog", I attach a new event handler to the DocumentComplete event of the WebBrowser, and navigate the web browser to my blog's "Contact Me" page url, and then wait for the event handler to be invoked to complete the automation.
In the sub menu click event handler:
try { this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted_AutomateMyBlog); this.webBrowser1.Navigate("http://dareen-h.blogspot.com/p/contact-me.html"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!"); }
In the WebBrowser document complete event handler, I find the yahoo mail link object, and click on it:
try { IHTMLDocument3 doc = this.webBrowser1.Document.DomDocument as IHTMLDocument3; if (doc != null) { IHTMLElement yahooMail = doc.getElementById("yahoomail"); if (yahooMail != null) { yahooMail.click(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!"); } finally { this.webBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted_AutomateMyBlog); }
Microsoft.mshtml.dll
You will need Microsoft.mshtml.dll that usually ships with IE, to make sure if you have it installed in the GAC, open Start Menu->Run and type assembly then hit enter, the GAC folder will open up, make sure that you have the dll in the dlls list
If you don't find the dll in the list, you may download Office 2003 Update that contains the dll, or if you find it too big for you, you may download it from dll download websites
After you get the dll, drag it and drop it into the assembly folder "%OS Drive%\Windows\Assembly"
You will need Microsoft.mshtml.dll that usually ships with IE, to make sure if you have it installed in the GAC, open Start Menu->Run and type assembly then hit enter, the GAC folder will open up, make sure that you have the dll in the dlls list
If you don't find the dll in the list, you may download Office 2003 Update that contains the dll, or if you find it too big for you, you may download it from dll download websites
After you get the dll, drag it and drop it into the assembly folder "%OS Drive%\Windows\Assembly"