Click here to Skip to main content
11,431,483 members (55,076 online)
Click here to Skip to main content

Automating Internet Explorer

, 24 Feb 2005
Rate this:
Please Sign up or sign in to vote.
An article describing how to automate IE, mainly for testing purposes.

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":

IEDriver driver = new IEDriver();
driver.Navigate("http://www.google.com");
driver.SetInputStringValue("q", "Internet Explorer Automation");
driver.ClickButton("btnG");

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:

Right click references and select 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.

Select the Microsoft.mshtml .Net object

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

Select the Microsoft Internet Controls COM object

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:

class MyIEDriver : IEDriver {
    public void ClickSave() {
        ClickButton("SaveButton");
    }
}

This gives you compiler checking for things that may normally only be caught at runtime.

History

  • 2-23-2005: Initial release.

Other Articles

Writing Automated Tests With NUnit and IE.

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.

A list of licenses authors might use can be found here

Share

About the Author

Leslie Hanks
Web Developer
United States United States
A developer for ScienceTRAX, LLC

Web: www.codeoutlaw.com

Comments and Discussions

 
Questionhow to access different options on tool bar like bing search Pin
deepakraut14-Mar-14 7:21
memberdeepakraut14-Mar-14 7:21 
QuestionI have two questions. Can I pass a “page-down”? Can I do a “Save As”. Pin
rschnieber28-May-13 19:42
memberrschnieber28-May-13 19:42 
QuestionSetting Zoom Pin
closl24-Sep-12 9:46
memberclosl24-Sep-12 9:46 
SuggestionThread.Sleep(500); error Pin
donneverdies21-Jul-12 0:16
memberdonneverdies21-Jul-12 0:16 
Questioninvalid cast exception Pin
mani224628-Apr-12 3:35
membermani224628-Apr-12 3:35 
GeneralThank you Pin
Ravi Vooda10-Apr-12 2:27
memberRavi Vooda10-Apr-12 2:27 
Questionhow to click button if button has no ID and no Name and just value Pin
MustangU12-Nov-10 3:48
memberMustangU12-Nov-10 3:48 
GeneralInternet Explorer 8 resolution Pin
prosh0t13-May-10 8:55
memberprosh0t13-May-10 8:55 
GeneralRe: Internet Explorer 8 resolution Pin
martho21-Jul-10 6:39
membermartho21-Jul-10 6:39 
GeneralSmall Modification Pin
Ashrafur Rahaman2-Mar-10 8:19
memberAshrafur Rahaman2-Mar-10 8:19 
GeneralGreat!! Thanks Pin
Ashrafur Rahaman1-Mar-10 10:07
memberAshrafur Rahaman1-Mar-10 10:07 
GeneralSelectively Disabling Content Pin
Kevin Yochum21-Feb-10 16:40
memberKevin Yochum21-Feb-10 16:40 
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.)

Thanks!
GeneralProblem with running in IE 7+ Pin
Hessam Abbasi10-Apr-09 1:19
memberHessam Abbasi10-Apr-09 1:19 
QuestionRe: Problem with running in IE 7+ Pin
Member 32368633-Jun-09 17:50
memberMember 32368633-Jun-09 17:50 
AnswerRe: Problem with running in IE 7+ Pin
bigheart00117-Jan-10 19:03
memberbigheart00117-Jan-10 19:03 
GeneralRe: Problem with running in IE 7+ Pin
daihugo26-Mar-10 17:08
memberdaihugo26-Mar-10 17:08 
GeneralRe: Problem with running in IE 7+ Pin
Bassem Zeft28-Jun-10 7:33
memberBassem Zeft28-Jun-10 7:33 
GeneralGetElementByValueOnce Pin
Noname_vn1-Dec-08 4:10
memberNoname_vn1-Dec-08 4:10 
QuestionExcellent - can the update for class constructor as previously posted be added to the source code? Pin
Member 47604192-May-08 14:04
memberMember 47604192-May-08 14:04 
GeneralIE7 support Pin
Dvir13-Feb-08 5:09
memberDvir13-Feb-08 5:09 
GeneralCool! Pin
Dev1238-Jan-08 14:25
memberDev1238-Jan-08 14:25 
AnswerRe: Cool! Pin
soptest14-May-08 10:10
membersoptest14-May-08 10:10 
Generalgreat Pin
ppcode12-Dec-07 23:38
memberppcode12-Dec-07 23:38 
QuestionHow to autoprint Pin
Nguoi Lap Trinh22-Nov-07 19:40
memberNguoi Lap Trinh22-Nov-07 19:40 
AnswerRe: How to autoprint Pin
Alex Furman26-Nov-07 14:35
memberAlex Furman26-Nov-07 14:35 
GeneralRe: How to autoprint Pin
Nguoi Lap Trinh26-Nov-07 16:26
memberNguoi Lap Trinh26-Nov-07 16:26 
GeneralRe: How to autoprint Pin
martho21-Jul-10 6:41
membermartho21-Jul-10 6:41 
GeneralRe: How to autoprint Pin
Pavan Nagumalli19-Jul-12 1:11
memberPavan Nagumalli19-Jul-12 1:11 
GeneralAnother solution Pin
Morningkill9-Nov-07 7:06
memberMorningkill9-Nov-07 7:06 
GeneralRe: Another solution Pin
ZaunCPP28-Oct-08 12:04
memberZaunCPP28-Oct-08 12:04 
GeneralRe: Another solution Pin
Morningkill28-Oct-08 21:58
memberMorningkill28-Oct-08 21:58 
GeneralWeb Testing Integrated With Visual Studio Pin
Frederic Torres7-Oct-07 11:11
memberFrederic Torres7-Oct-07 11:11 
QuestionHow to automate security alert Pin
dvenkat3-Sep-07 3:12
memberdvenkat3-Sep-07 3:12 
AnswerRe: How to automate security alert Pin
Frederic Torres7-Oct-07 11:03
memberFrederic Torres7-Oct-07 11:03 
QuestionRunning the code from web server Pin
atul.pioneers18-Aug-07 3:04
memberatul.pioneers18-Aug-07 3:04 
AnswerRe: Running the code from web server Pin
Alex Furman7-Nov-07 16:30
memberAlex Furman7-Nov-07 16:30 
QuestionNullReferenceException Error in VS 2005 Pin
Cyborgcom5-Jul-07 13:16
memberCyborgcom5-Jul-07 13:16 
AnswerRe: NullReferenceException Error in VS 2005 Pin
Rbearl2-Sep-09 12:31
memberRbearl2-Sep-09 12:31 
GeneralFrames Security, and Normailzation Pin
TriLogic27-May-07 7:11
memberTriLogic27-May-07 7:11 
GeneralRe: Frames Security, and Normailzation Pin
ywusa200126-Aug-07 4:50
memberywusa200126-Aug-07 4:50 
GeneralUnspecified exception thrown when trying to find IE Pin
blottie4-Apr-07 11:38
memberblottie4-Apr-07 11:38 
GeneralRe: Unspecified exception thrown when trying to find IE Pin
blottie4-Apr-07 13:13
memberblottie4-Apr-07 13:13 
GeneralRe: Unspecified exception thrown when trying to find IE Pin
benjiwa26-Apr-07 3:45
memberbenjiwa26-Apr-07 3:45 
GeneralRe: Unspecified exception thrown when trying to find IE Pin
popicul198028-Apr-09 23:41
memberpopicul198028-Apr-09 23:41 
GeneralException thorows Pin
yousefk27-Mar-07 1:27
memberyousefk27-Mar-07 1:27 
GeneralRe: Exception thorows Pin
blottie4-Apr-07 13:14
memberblottie4-Apr-07 13:14 
GeneralAutomation of complex DHTML/AJAX applications Pin
Alex Furman23-Mar-07 8:54
memberAlex Furman23-Mar-07 8:54 
AnswerRe: Automation of complex DHTML/AJAX applications Pin
ZaunCPP28-Oct-08 12:00
memberZaunCPP28-Oct-08 12:00 
GeneralIE Automation Pin
Goku2619-Mar-07 4:52
memberGoku2619-Mar-07 4:52 
GeneralAdded useful functionality... hope this helps. Pin
TriLogic16-Mar-07 6:00
memberTriLogic16-Mar-07 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 25 Feb 2005
Article Copyright 2005 by Leslie Hanks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid