Pages

Thursday, February 27, 2014

Automate Web Based Application using shdocvw.dll & mshtml.dll and C#

I covered few things in automation testing and framework in my earlier posts. Its time to do some practical, I will demonstrate one simple step to automate web application using Internet Explore and C#. If you remember in my earlier posts I said I won't use any ready made framework and reveal some of hidden treasure inside the window. This article is revealing one of the hidden treasure. 

shdocvw.dll and microsoft.mshtml.dll are one of the hidden treasure. These two dlls are the very important part of Internet Explorer installed on your computer of any version. So we'll see how to use them to automate web applications. But let me give some technical background about these two dlls.

Shdocvw.dll:  It supplies the functionality associated with navigation, in-place linking, favorites and history management, and PICS support. In short this dll is used inside the Internet Explore to provide most of the functionality in IE browser, like Navigation, Page History, favorite etc. So whatever you see on the Internet Explorer like, Address bar, buttons to go back, go forward, book mark etc they all are using this dll. More detail you can see here 

Microsoft.mshtml.dll:  It is an Active Document that is capable of rendering HTML and laying out contained ActiveX Controls. In this document, it refers to an instance of the MSHTML COM object that implements IOleObject, IOleDocument, IOleDocumentView, and many other interfaces. In Short it is responsible for the document area in Internet Explorer, which renders Html content and display your requested URL. You can see more details here

Hope we got enough information about these two dlls, now I will show you how to automate a web page using them. But you might be thinking why not to use selenium or any other tools. Don't worry I will cover them later so that you can understand the difference among different automation tools and framework.

Step 1: Create a simple Console Application in visual studio. Here is the details how to create Console application
Step 2: Add reference of these two dlls in your console application. Here you can find the files and how to add as reference
  • Shdocvw.dll from C:\Windows\System32
  • Microsoft.mshtml.dll from C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies or C:\Program Files\Microsoft.NET\Primary Interop Assemblies.
  • How to add reference in C# Console application. Click here
Step 3: Add these two directives. 
           using SHDocVw;
           using mshtml;
Step 4: Copy and paste the code below.. 

Hey hey wait.. are you trying to copy the code? That is just an image. Copy the code from here
Step 5: Press F5 and wait, if steps executed very fast then add the Thread.Sleep(3000) in between of steps.
Note: You might have question, how did I find the Ids of the web elements. You can see my earlier post Searching Web Element property using inbuilt browser tools

This is just a sample which shows how to work on the core level of automation, the QTP or other keyword driven framework does the same thing but it hides the code complexity and you only get one GUI control instead of code. 

I know it is still difficult for some manual testers, but don't afraid of the code. Just try to play with it and use other website to automate you will love it. And Visual Studio is intelligent enough to give you more details of supported method and property. I am trying the same article for Java Users, but not very sure when I can post so forgive if it take long time.

Have fun and send comment if code is not working.

3 comments: