473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Invoking Javascript Functions from an Instance of Internet Explorer

Hello,
I am automating Internet Explorer in order to do some simple automated
testing of a web application. How do I invoke Javascript functions in
the web page I load?

I can successfully start an instance of Internet Explorer and control
the DOM, but I don't know how to call Javascript functions. Here is
the code I am using so far:

public class WebBrowser
{
public SHDocVw.InternetExplorerClass ie;

public WebBrowser( string url )
{
ie = new SHDocVw.InternetExplorerClass();
ie.Visible = true;

Object Flags = null, TargetFrameName = null, PostData = null,
Headers = null;
ie.Navigate( url, ref Flags, ref TargetFrameName, ref PostData, ref
Headers );

while( ie.Busy )
{
Thread.Sleep( 500 );
}

mshtml.HTMLDocumentClass document =
((mshtml.HTMLDocumentClass)ie.Document);

// here is where I would like to invoke a javascript function in my
document

ie.Quit();

}
}

May 30 '07 #1
3 9559
Matthew,

This is pretty easy. Cast your document class to the IHTMLDocument
interface. Then get the object returned from the Script property. Finally,
use reflection to make a call to the underlying javascript method, like so:

// Get the document
mshtml.IHTMLDocument document = ((mshtml.IHTMLDocument) ie.Document);

// Get the script object.
object script = document.Script;

// Make the call:
script.GetType().InvokeMember("my function", BindingFlags.InvokeMethod,
null, script, null);

// Release the script object, and possibly the document object.
Marshal.ReleaseComObject(script);

If you have to pass parameters to the method, you would create an array
which represent the parameters and then pass that into the call to
InvokeMember.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>
// here is where I would like to invoke a javascript function in my
document
"Matthew Lock" <lo******@gmail.comwrote in message
news:11**********************@g37g2000prf.googlegr oups.com...
Hello,
I am automating Internet Explorer in order to do some simple automated
testing of a web application. How do I invoke Javascript functions in
the web page I load?

I can successfully start an instance of Internet Explorer and control
the DOM, but I don't know how to call Javascript functions. Here is
the code I am using so far:

public class WebBrowser
{
public SHDocVw.InternetExplorerClass ie;

public WebBrowser( string url )
{
ie = new SHDocVw.InternetExplorerClass();
ie.Visible = true;

Object Flags = null, TargetFrameName = null, PostData = null,
Headers = null;
ie.Navigate( url, ref Flags, ref TargetFrameName, ref PostData, ref
Headers );

while( ie.Busy )
{
Thread.Sleep( 500 );
}

mshtml.HTMLDocumentClass document =
((mshtml.HTMLDocumentClass)ie.Document);

// here is where I would like to invoke a javascript function in my
document

ie.Quit();

}
}

May 30 '07 #2
On May 31, 2:07 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Matthew,

This is pretty easy. Cast your document class to the IHTMLDocument
interface. Then get the object returned from the Script property. Finally,
use reflection to make a call to the underlying javascript method, like so:
Excellent. This is exactly what I was after.

I also discovered document.parentWindow.execScript but unlike
InvokeMethod it doesn't seem to make the return value available.

Thanks,
Matthew

May 31 '07 #3
On May 31, 2:07 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>
If you have to passparametersto the method, you would create an array
which represent theparametersand then pass that into the call to
InvokeMember.
How would I pass a Javascript object (using this notation
{person:"John", age:"30"} ) as a parameter? I can only seem to pass
string parameters.

Regards,
Matthew Lock

Jun 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: autobest.be | last post by:
Hello, I play recently with javascript and thought this graphical effect could be interesting for those who like it :) http://www.autobest.be/test/flag.htm (err...internet explorer only)
25
by: Jeff | last post by:
Use the MS Script Editor included free with MS Office 2002 and above, for debugging Internet Explorer (IE). This subject is of great interest to many JS developers, as there is no obvious, low...
2
by: COHENMARVIN | last post by:
I am writing a javascript page. There is an error with the pagee, and it does not work. Internet Explorer gives a message on the status bar that there is a javascript error, but it doesn't tell...
11
by: Doug van Vianen | last post by:
Hi, I often like to include some JavaScript coding in my web pages to make them more interesting. Unfortunately, even when this coding is as simple as a check to see what the display width is in...
11
by: GHUM | last post by:
Hello, I created some rather complex Intranet Application, using lots of JavaScript, DOM-Maninpulation and XMLHTTPRequest communication. I developed on FireFox, with the excellent firebug ......
9
by: -Lost | last post by:
http://blogs.msdn.com/ie/archive/2007/12/19/internet-explorer-8-and- acid2-a-milestone.aspx Oh my! A somewhat standards compliant Internet Explorer? What about JavaScript? Not that it proves...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.