Introduction

I attended an ASP.NET Users Group meeting and saw a presentation given by Demetrie Gerodimos. He is an Architect at Dell and was giving a presentation on Test Driven Development. An interesting point of his presentation for me was when he explained how they execute their tests. A discussion with him after the meeting revealed that they were automating Internet Explorer to test their ASPX pages.

After spending some time researching how easily you can automate Internet Explorer in C# (with the help of some URLs given to me by Demetrie), I began writing my own version of IEDriver. The URLs I looked at had a basic example of how to instruct IE to perform various actions. Over the course of the last year, I have enhanced my IEDriver class to handle other types of interactions with IE that I have required. While this is by no means a complete automation class, I believe it performs many of the common functions needed when writing automated tests.

Background

The basic idea here is that we will use Interop services to invoke methods on the COM interfaces built into Internet Explorer. I have written this article because the documentation available on these interfaces is scarce at best. I have spent a lot of time researching on the web and doing trial and error experimentation with these interfaces to produce much of this IEDriver class.

Using the code

The gist of it is a class called IEDriver. This class' constructor will create a new IEXPLORE.EXE process and attach to it. From then on, the methods on the IEDriver class can be used to simulate a user using his browser. While I have not implemented every type of interaction you could have with your browser, I have implemented most of the routines that I have needed to write automated tests for my product.

I have tried to hide the inner workings of the driver from the user of the class. You will notice that there are ClickXXX methods and GetXXX values that will deal with all the COM interfaces to perform the actions necessary or return the value you are interested in.

Here is an example of how easy this class is to use. This example will simply navigate to Google and search for "Automating Internet Explorer":

In this example, "q" is the name of the input control and "btnG" is the name of the button control on the Google page. Preferably, you will have ID attributes on your HTML elements, but if there is no ID attribute, the driver will find the element with a name attribute.

In order to use this code, you will need to do a couple of things. First of all, you will need to add a couple of references to your project. To do this, right click on References and click Add Reference:

When the Add Reference dialog comes up, first select the .NET tab. Scroll down to the Microsoft.MSHtml object, and click the Select button.

Next, click on the COM tab. Scroll down to the Microsoft Internet Controls object and click the Select button.

Now click on OK. This will add the necessary references to your project that will allow IEDriver class to make the necessary calls to automate IE.

Once you have added the appropriate references to your project, just include the IEDriver in your project. Now, all you have to do is import the IEAutomation namespace into your class and you can begin writing automated tests using IE.

Points of Interest

One thing to keep in mind. The documentation on this stuff is not very good so if you need to enhance IEDriver to support a feature that it currently doesn't, your best bet is probably to use your intellisense to invoke a method that you think might do what you want, and assign the variable to some temporary local variable. Then, fire up your debugger and use the watch window to find out all you can about the objects returned.

Tips

While writing my own automated tests, I have discovered that it is useful to extend the IEDriver class and add methods that do tasks that I might perform quite often so that I can use the intellisense and the compiler to eliminate the possibility of mistyping control names. For example:

History

Other Articles

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

this causing error because IE takes different time to load up i mean if IE is already open it will take less time or if IE is not open it will take long time.. so no fix time tht how much time thread should sleep so i got a solution for it just change..

IE 8 introduces something called "Loosely Coupled IE" (LCIE), which launches extra processes for use in additional tabs and such. If you try to use this code and launch an IE window after an existing one is already open this code will not work. When attempting to spawn the new process and navigate to a page, it pops up with the error 'Process has exited, so the requested information is not available.' I had to resolve this by changing a registry setting

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main

then add TabProcGrowth as a new DWord 32 bit value, and set it to '0'. Restart all instances of IE. It should work after that and you can open subsequent IE processes at any time, no matter how many previous ones are open.

That article really helped me a lot. I was facing problem to insert data in the text box and click button. It worked fine in my local computer where VS is installed but for the computers where VS is not installed it shown exception to convert _Comobject to mshtml.HTMLInputElementClass.

So I modified a bit. here is the modified file http://www.box.net/shared/zymkn7x5yv

I have my code to automate IE working, but it could be made faster by eliminating all of the extraneous downloading - images, JavaScript, Flash advertisements, etc. Is there a way to disable some or all of these items on an instance by instance basis? For example, for project 1, disable images and flash and for Project 2, disable JavaScript and flash? So far, all of my attempts to do so enable images for example in all browser sessions - even those that are not automated. (It changes all Internet Explorer options globally.)

First thank you for this useful article.I have a problem in working with IE 7 +. My program does not work in IE 7. Is it normal?So what should we do in IE 7?(For example IE 7 has multiple tabs, how to select one tab, do i need another dll?)