Tuesday, February 9, 2010

Selenium - Handling Dialogs with Python

I am currently scripting Selenium tests using Python (selenium.py)
One of the main issues I have found are looping, parametrization and handling modal dialogs

Dialogs:

Dialog boxes are invisible to selenium, it just can't see them.
The reason is the dialogs are native to the operating system you are running.
Selenium only identifies the web components (elements)

In Internet Explorer I created a work around for handling dialogs in Python such as File Download, SaveAs, Save and Close. This workaround could also be used for other dialogs.
Although this may not be the most elegant method it works for me.


import os, sys, time
import win32com
from pywinauto import application
import winGuiAuto as wga
import time
import SendKeys
import win32gui
import win32con


###############################
class AutoTools:
def __init__(self):

self.app = application.Application()


def clickFileDL(self):
"""
Find the Download Dialog and Click "Save"
"""
time.sleep(5)

# Draw and Outline so I know it's the right control and give focus to the control
self.app.FileDownload["&Save"].DrawOutline()
time.sleep(1)
# Click the Save Button
self.app.FileDownload["&Save"].ClickInput()
time.sleep(1)
# Get a Handle to the File Download Window by title
hwnd = wga.findTopWindow(wantedText="File Download")
time.sleep(1)
# Simulate Mouse Down and Mouse Up
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, 0, 0)
# Important timing issue between clicks
time.sleep(2)
win32gui.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, 0)
#
SendKeys.SendKeys("""{ENTER}""")

def clickSaveAs(self):
"""
Find the Save As Dialog and Click "Save"
"""
time.sleep(1)
self.app.SaveAs.PrintControlIdentifiers()
self.app.SaveAs.ComboBox3.Select("All Files")
# Find the Save As Dialog and Click "Save"
self.app.SaveAs.Save.DrawOutline()
time.sleep(1)
self.app.SaveAs["&Save"].ClickInput()
time.sleep(1)
SendKeys.SendKeys("""{ENTER}""")


def clickDLClose(self):
"""
Find the Downloadcomplete Dialog and Click "Close"
"""

time.sleep(1)
# Find the Download complete Dialog and Click "Close"
time.sleep(2)
SendKeys.SendKeys("""{ENTER}""")

#########################################

Refs:
http://pywinauto
.openqa.org/

http://www.brunningonline.net/simon/blog/.../winGuiAuto.py.html


Next I will explain how to use Autoit with FireFox to handle dialogs...

No comments:

Post a Comment