Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# probe for and capture Javascript Alert() and Confirm()

So I've been doing some research for a while and I'm at a dead end. I'm doing some IE automation. In C#/.NET, how do I probe for and consume a javascript alert() or confirm() so I can do things like grab its text and click the OK\Cancel buttons?

Update:

I need to reiterate: I need to be able to pull and verify the text from an alert() or confirm() as well as send it an OK or Cancel click. An example of such a test would be to make sure that, when I click on delete, the confirm() doesn't say "Are you sure you'd like to go to Mexico?" or anything else except the proper message.

Just in case, let me reiterate: For the purposes of this test, I have zero control over the source of the website in question.

Lastly, I'm using SHDocVw.InternetExplorer.

like image 298
Squirrelsama Avatar asked Dec 16 '10 01:12

Squirrelsama


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

I think you're going to have to use FindWindow, SendMessage, etc. This is how WatiN does its dialog handling.

like image 146
J.D. Avatar answered Oct 23 '22 04:10

J.D.


I think what you are looking for is the IDocHostshowUI COM interface. If I read the documentation correctly and my assumptions are correct ShowMessage function will be called when an alert() or confirm() is called in JavaScript.

I've not done this myself so I am only guessing. Here are some examples of someone showing an example of using that interface (and many others) in C#. WebBrowserSample2 sounds like what you are looking for.

like image 21
shf301 Avatar answered Oct 23 '22 03:10

shf301