Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

win32 and javascript alerts

0 views
Skip to first unread message

Bryan L. Fordham

unread,
Oct 17, 2002, 1:53:26 PM10/17/02
to
I'm using those nifty win32com extensions to do some unit testing.
I'm able to control IE with no problems, fill out forms and push the
pretty buttons to make things go.

The one issue I've run into is this: Say there's an input box called
Spam, that requires a value. If you click the Go button and Spam is
empty, a little javascript alert box pops up and says "Hey, fill in
Spam!"

well, if I load the page using python and click the Go button with
Spam empty, the javascript alert pops up... and I don't know how to
get rid of it. Python hangs until I manually clear the alert.

Any ways around this?

Thanks
--B

Dave Brueck

unread,
Oct 17, 2002, 3:30:06 PM10/17/02
to

Well, the way I get around this feels like a horrible, horrible hack, but
it works pretty well. Below is some code from a class I made called
WebSerf.

Use the code in CaptureWindowEvents to install new window.alert and
window.open functions (I call this method after I click a Submit button,
click a link, or navigate to a whole new page - be sure the new document
has fully loaded first!).

The code in GetLatestAlertMessage and GetLatestOpenString let you see what
actions the page tried to take.

You'll have to modify the code somewhat to work with your approach,
but I hope it at least helps in some way.

-Dave

The code:

CAPTURE_JSCRIPT = '''var serfcapturedevents = 1;
var serfalertmsg="";
var serfopenwin=null;
function serfalert(arg) { serfalertmsg=arg; }
var serfcapturedevents = 1;
var serfalertmsg="";
var serfopenwin=null;
function serfalert(arg) { serfalertmsg=arg; }
function serfopen()
{
var args = new Array(null, null, null, null);
fargs = serfopen.arguments;
for (i=0; i < fargs.length; i++)
args[i] = fargs[i];
serfopenwin = serfoldwindowopen(args[0], args[1], args[2], args[3]);
return serfopenwin;
}

function serfclosepopup()
{ if (serfopenwin != null)
serfopenwin.close();
}

window.attachEvent('onunload', serfclosepopup);
window.alert=serfalert;
serfoldwindowopen = window.open;
window.open=serfopen;
var serfpromptvalue = '';
function serfprompt() { return serfpromptvalue; }
window.prompt = serfprompt;
var serfconfirmvalue = true;
function serfconfirm() { return serfconfirmvalue; }
window.confirm = serfconfirm;'''

Now a few methods from the WebSerf class:

def CaptureWindowEvents(self):
'Installs a Javascript routine to reroute any window.alert/.open
calls'
pw = self.doc.parentWindow
try:
pw.serfcapturedevents
return # Already captured
except AttributeError:
pass # Ok, now go capture them

try:
pw.execScript(CAPTURE_JSCRIPT)
except:
traceback.print_exc()

def GetLatestAlertMsg(self):
'Returns and clears the most recent window.alert message'
try:
pw = self.doc.parentWindow
msg = pw.serfalertmsg
if msg:
pw.serfalertmsg = ''
except:
msg = ''
traceback.print_exc()
return msg

def GetLatestOpenString(self):
'Returns and clears the most recent window.open string or None'
try:
pw = self.doc.parentWindow
win = pw.serfopenwin
if win:
pw.serfopenwin = None
except:
win = None
traceback.print_exc()
return win


Bryan L. Fordham

unread,
Oct 18, 2002, 8:22:52 AM10/18/02
to
Dave Brueck <da...@pythonapocrypha.com> wrote in message news:<mailman.1034878702...@python.org>...

> Well, the way I get around this feels like a horrible, horrible hack, but
> it works pretty well. Below is some code from a class I made called
> WebSerf.

[snip]

Well, maybe it is a hack. But since it worked beautifully, I'm
certainly not going to judge 8)

Thanks
--B

0 new messages