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]
- mvp@spam.guard.caspershouse.com
[quoted text, click to view] >
> // here is where I would like to invoke a javascript function in my
> document
"Matthew Lock" <lockster@gmail.com> wrote in message
news:1180506015.296910.258490@g37g2000prf.googlegroups.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();
>
> }
> }
>