Broke out examples from test_application.py

This commit is contained in:
markm 2006-01-31 18:00:44 +00:00
parent dbab749863
commit 0d22a830d8
6 changed files with 698 additions and 0 deletions

@ -0,0 +1,56 @@
import time
from pprint import pprint
from pywinauto.application import Application
# start the application and wait for the Agent Dialog to be ready
app = Application().start_(r"c:\program files\agent\agent.exe")
app.Agent.WaitReady()
# if we get the Agent Setup wizard pops up close it
if app.AgentSetupWizard.Cancel.Exists():
app.AgentSetupWizard.Cancel.Click()
app.AgentSetupWizard2.Yes.Click()
# Select to emtpy trash
app.Agend.MenuSelect("File->EmptyTrash")
app.EmptyTrash.No.Click()
# Select some more menus (typo not important :-)
app.Agend.MenuSelect("File->Purge and Compact -> Compact All Folders")
app.Agent.OK.Click()
app.Agend.MenuSelect("File->Purge and Compact->PurgeFoldersInDesks")
app.PurgeFoldersInDesks.Cancel.Click()
# this is strange - when I do it by hand this is "Purge Folder" but during
# automation the text of the menu item is Purge Selected Folders
# FIXED - need to init the sub menu!
app.Agend.MenuSelect("File->Purge and Compact->Purge Folder")
app.AgentTip.OK.Click()
app.Agend.MenuSelect("File->Import and Export->Import Messages")
app.ImportMessages.Cancel.Click()
app.Agend.MenuSelect("File->Import and Export->Import Address Book")
app.ImportAddresses.Cancel.Click()
app.Agend.MenuSelect("File->Import and Export->Export Address Book")
app.ExportAddresses.Cancel.Click()
# pick something other then a file menu item
app.Agend.MenuSelect("Tools->ApplyFiltersToFolder")
app.AgentTip.OK.Click()
app.ApplyFiltersToFolders.Cancel.Click()
print "==" * 20
print "The Agent File Menu..."
print "==" * 20
pprint (app.Agent.MenuItems()[1])
app.Agent.MenuSelect("File->Print")
app.Print.Cancel.Click()
# quit Agent
app.Agent.MenuSelect("File -> Exit")

87
examples/MiscExamples.py Normal file

@ -0,0 +1,87 @@
# GUI Application automation and testing library
# Copyright (C) 2006 Mark Mc Mahon
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place,
# Suite 330,
# Boston, MA 02111-1307 USA
"Run some automations to test things"
__revision__ = "$Revision: 214 $"
import time
from pywinauto import application
from pywinauto import tests
from pywinauto.findbestmatch import MatchError
from pywinauto import findwindows
def TestExceptions():
"Test some things that should raise exceptions"
# test that trying to connect_ to a non existent app fails
try:
app = application.Application()
app.connect_(path = ur"No process with this please")
assert False
except application.ProcessNotFoundError:
pass
# test that trying to connect_ to a non existent app fails
try:
app = application.Application()
app.start_(cmd_line = ur"No process with this please")
assert False
except application.AppStartError:
pass
# try when it isn't connected
try:
app = application.Application()
#app.start_(ur"c:\windows\system32\notepad.exe")
app.Notepad.Click()
assert False
except application.AppNotConnected:
pass
def GetInfo():
app = application.Application()
app.start_(ur"notepad.exe")
app.Notepad.MenuSelect("File->PageSetup")
print "==" * 20
print "Windows of this application:", app.app_windows_()
print "The list of identifiers for the Page Setup dialog in Notepad"
print "==" * 20
app.PageSetup.print_control_identifiers()
print "==" * 20
print "The list of identifiers for the 2nd Edit control in the dialog"
app.PageSetup.Edit2.print_control_identifiers()
print "==" * 20
app.PageSetup.OK.CloseClick()
app.Notepad.MenuSelect("File->Exit")
if __name__ == '__main__':
TestExceptions()
GetInfo()

102
examples/mspaint.py Normal file

@ -0,0 +1,102 @@
# GUI Application automation and testing library
# Copyright (C) 2006 Mark Mc Mahon
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place,
# Suite 330,
# Boston, MA 02111-1307 USA
"Run some automations to test things"
__revision__ = "$Revision: 214 $"
import time
from pywinauto import application
from pywinauto import tests
from pywinauto.findbestmatch import MatchError
from pywinauto import findwindows
app = application.Application()
# for distribution we don't want to connect to anybodies application
# because we may mess up something they are working on!
#try:
# app.connect_(path = ur"c:\windows\system32\mspaint.exe")
#except application.ProcessNotFoundError:
# app.start_(ur"c:\windows\system32\mspaint.exe")
app.start_(ur"mspaint.exe")
pwin = app.window_(title_re = ".* - Paint")
# get the previous image size
pwin.MenuSelect("Image->Attributes")
prev_width = app.Attributes.Edit1.Texts()[1]
prev_height = app.Attributes.Edit2.Texts()[1]
# set our preferred area
app.Attributes.Edit1.TypeKeys("350") # you can use TypeKeys or
app.Attributes.Edit2.SetText("350") # SetText - they work differently!
app.Attributes.OK.CloseClick()
# get the reference to the Canvas window
canvas = pwin.Afx100000008
# make sure the pencil tool is selected
pwin.Tools2.Click(coords = (91, 16))
size = 15
num_slants = 20
# draw the axes
canvas.PressMouse(coords = (size, size * num_slants))
canvas.MoveMouse(coords = (size*num_slants, size*num_slants))
canvas.MoveMouse(coords = (size * num_slants, size))
canvas.ReleaseMouse()
# now draw the lines
print "*** if you move your mouse over Paint as it is drawing ***"
print "*** these lines then it will mess up the drawing! ***\n"
for i in range(1, num_slants):
endcoords = (size * (num_slants - i), size * num_slants)
canvas.PressMouse(coords = (size * num_slants, i * size)) # start
canvas.MoveMouse(coords = endcoords) # x and y axes
canvas.ReleaseMouse(coords = endcoords)
# may fail if PIL is not installed
image = canvas.CaptureAsImage()
if image:
image.save(r"Application_Paint_test.png")
print "Saved image as: Application_Paint_test.png"
# set it back to original width and height
pwin.MenuSelect("Image->Attributes")
# set our preferred area
app.Attributes.Edit1.TypeKeys(prev_width)
app.Attributes.Edit2.SetText(prev_height)
app.Attributes.OK.CloseClick()
# close Paint
pwin.MenuSelect("File->Exit")
if app.Paint.No.Exists():
#print "It existed!"
# Click the no button on the message box asking if we want to save
app.Paint.No.CloseClick()

175
examples/notepad_fast.py Normal file

@ -0,0 +1,175 @@
# GUI Application automation and testing library
# Copyright (C) 2006 Mark Mc Mahon
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place,
# Suite 330,
# Boston, MA 02111-1307 USA
"Run some automations to test things"
__revision__ = "$Revision: 214 $"
import time
from pywinauto import application
from pywinauto import tests
from pywinauto.findbestmatch import MatchError
from pywinauto import findwindows
application.set_timing(2, .01, 10, .001, .001, 0, 0, 0, 0, 0)
app = application.Application()
## for distribution we don't want to connect to anybodies application
## because we may mess up something they are working on!
#try:
# app.connect_(path = ur"c:\windows\system32\notepad.exe")
#except application.ProcessNotFoundError:
# app.start_(ur"c:\windows\system32\notepad.exe")
app.start_(ur"notepad.exe")
app.Notepad.MenuSelect("File->PageSetup")
# ----- Page Setup Dialog ----
# Select the 4th combobox item
app.PageSetupDlg.ComboBox1.Select(4)
# Select the 'Letter' combobox item
app.PageSetupDlg.ComboBox1.Select("Letter")
# run some tests on the Dialog. List of available tests:
# "AllControls",
# "AsianHotkey",
# "ComboBoxDroppedHeight",
# "CompareToRefFont",
# "LeadTrailSpaces",
# "MiscValues",
# "Missalignment",
# "MissingExtraString",
# "Overlapping",
# "RepeatedHotkey",
# "Translation",
# "Truncation",
bugs = app.PageSetupDlg.RunTests('RepeatedHotkey Truncation')
# if there are any bugs they will be printed to the console
# and the controls will be highlighted
tests.print_bugs(bugs)
# ----- Next Page Setup Dialog ----
app.PageSetupDlg.Printer.Click()
# do some radio button clicks
# Open the Connect to printer dialog so we can
# try out checking/unchecking a checkbox
app.PageSetupDlg.Network.Click()
# ----- Connect To Printer Dialog ----
# Select a checkbox
app.ConnectToPrinter.ExpandByDefault.Check()
app.ConnectToPrinter.ExpandByDefault.UnCheck()
# try doing the same by using click
app.ConnectToPrinter.ExpandByDefault.Click()
app.ConnectToPrinter.ExpandByDefault.Click()
# close the dialog
app.ConnectToPrinter.Cancel.CloseClick()
# ----- 2nd Page Setup Dialog again ----
app.PageSetupDlg2.Properties.Click()
doc_props = app.window_(title_re = ".*Document Properties")
# ----- Document Properties Dialog ----
# some tab control selections
# Two ways of selecting tabs with indices...
doc_props.TabCtrl.Select(0)
doc_props.TabCtrl.Select(1)
doc_props.TabCtrl.Select(2)
# or with text...
doc_props.TabCtrl.Select("PaperQuality")
doc_props.TabCtrl.Select("JobRetention")
doc_props.TabCtrl.Select("Layout")
# do some radio button clicks
doc_props.RotatedLandscape.Click()
doc_props.BackToFront.Click()
doc_props.FlipOnShortEdge.Click()
doc_props.Portrait.Click()
doc_props._None.Click()
doc_props.FrontToBack.Click()
# open the Advanced options dialog in two steps
advbutton = doc_props.Advanced
advbutton.Click()
# close the 4 windows
# ----- Advanced Options Dialog ----
app.window_(title_re = ".* Advanced Options").Ok.Click()
# ----- Document Properties Dialog again ----
doc_props.Cancel.CloseClick()
# ----- 2nd Page Setup Dialog again ----
app.PageSetupDlg2.OK.CloseClick()
# ----- Page Setup Dialog ----
app.PageSetupDlg.Ok.CloseClick()
# type some text - note that extended characters ARE allowed
app.Notepad.Edit.SetText(u"I am typing s\xe4me text to Notepad\r\n\r\n"
"And then I am going to quit")
# the following shows that Sendtext does not accept
# accented characters - but does allow 'control' characters
app.Notepad.Edit.TypeKeys(u"{END}{ENTER}SendText d\xf6\xe9s not "
u"s\xfcpp\xf4rt \xe0cce\xf1ted characters", with_spaces = True)
# Try and save
app.Notepad.MenuSelect("File->SaveAs")
app.SaveAs.ComboBox5.Select("UTF-8")
app.SaveAs.edit1.SetText("Example-utf8.txt")
app.SaveAs.Save.CloseClick()
# my machine has a weird problem - when connected to the network
# the SaveAs Dialog appears - but doing anything with it can
# cause a LONG delay - the easiest thing is to just wait
# until the dialog is no longer active
# - Dialog might just be gone - because click worked
# - dialog might be waiting to disappear
# so can't wait for next dialog or for it to be disabled
# - dialog might be waiting to display message box so can't wait
# for it to be gone or for the main dialog to be enabled.
# while the dialog exists wait upto 30 seconds (and yes it can
# take that long on my computer sometimes :-( )
app.SaveAs.Cancel.WaitNotEnabled()
try:
app.SaveAs.Yes.CloseClick()
except MatchError:
pass
# exit notepad
app.Notepad.MenuSelect("File->Exit")

100
examples/notepad_item.py Normal file

@ -0,0 +1,100 @@
# GUI Application automation and testing library
# Copyright (C) 2006 Mark Mc Mahon
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place,
# Suite 330,
# Boston, MA 02111-1307 USA
"Run some automations to test things"
__revision__ = "$Revision: 214 $"
import time
from pywinauto import application
from pywinauto import tests
from pywinauto.findbestmatch import MatchError
from pywinauto import findwindows
application.set_timing(3, .5, 10, .5, .4, .2, .2, .1, .2, .5)
"Run a quick test on Notepad"
app = application.Application()
app.start_(ur"notepad.exe")
app['Notepad'].WaitReady()
app['Notepad'].MenuSelect("File->PageSetup")
# ----- Page Setup Dialog ----
# Select the 4th combobox item
app['PageSetupDlg']['ComboBox1'].Select(4)
# Select the 'Letter' combobox item
app['PageSetupDlg']['ComboBox1'].Select("Letter")
# ----- Next Page Setup Dialog ----
app['PageSetupDlg']['Printer'].Click()
app['PageSetupDlg']['Network'].Click()
# ----- Connect To Printer Dialog ----
# Select a checkbox
app['ConnectToPrinter']['ExpandByDef'].Check()
# Uncheck it again - but use Click this time!
app['ConnectToPrinter']['ExpandByDef'].Click()
app['ConnectToPrinter']['OK'].CloseClick()
# ----- 2nd Page Setup Dialog again ----
app['PageSetupDlg2']['Properties'].Click()
# ----- Document Properties Dialog ----
doc_props = app.window_(title_re = ".*Document Properties")
# Two ways of selecting tabs
doc_props['TabCtrl'].Select(2)
doc_props['TabCtrl'].Select("Layout")
# click a Radio button
doc_props['RotatedLandscape'].Click()
doc_props['Portrait'].Click()
# open the Advanced options dialog in two steps
advbutton = doc_props['Advanced']
advbutton.Click()
# ----- Advanced Options Dialog ----
# close the 4 windows
app.window_(title_re = ".* Advanced Options")['Ok'].Click()
# ----- Document Properties Dialog again ----
doc_props['Cancel'].CloseClick()
# ----- 2nd Page Setup Dialog again ----
app['PageSetup2']['OK'].CloseClick()
# ----- Page Setup Dialog ----
app['PageSetup']['Ok'].CloseClick()
# type some text
app['Notepad']['Edit'].SetText(u"I am typing s\xe4me text to Notepad"
"\r\n\r\nAnd then I am going to quit")
# exit notepad
app['NotepadDialog'].MenuSelect("File->Exit")
app['Notepad']['No'].CloseClick()

178
examples/notepad_slow.py Normal file

@ -0,0 +1,178 @@
# GUI Application automation and testing library
# Copyright (C) 2006 Mark Mc Mahon
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place,
# Suite 330,
# Boston, MA 02111-1307 USA
"Run some automations to test things"
__revision__ = "$Revision: 214 $"
import time
from pywinauto import application
from pywinauto import tests
from pywinauto.findbestmatch import MatchError
from pywinauto import findwindows
print "Setting timings to slow settings, may be necessary for"
print "slow applications or slow machines."
application.set_timing(3, .5, 10, .5, .4, .2, .2, .1, .2, .5)
app = application.Application()
## for distribution we don't want to connect to anybodies application
## because we may mess up something they are working on!
#try:
# app.connect_(path = ur"c:\windows\system32\notepad.exe")
#except application.ProcessNotFoundError:
# app.start_(ur"c:\windows\system32\notepad.exe")
app.start_(ur"notepad.exe")
app.Notepad.MenuSelect("File->PageSetup")
# ----- Page Setup Dialog ----
# Select the 4th combobox item
app.PageSetupDlg.ComboBox1.Select(4)
# Select the 'Letter' combobox item
app.PageSetupDlg.ComboBox1.Select("Letter")
# run some tests on the Dialog. List of available tests:
# "AllControls",
# "AsianHotkey",
# "ComboBoxDroppedHeight",
# "CompareToRefFont",
# "LeadTrailSpaces",
# "MiscValues",
# "Missalignment",
# "MissingExtraString",
# "Overlapping",
# "RepeatedHotkey",
# "Translation",
# "Truncation",
bugs = app.PageSetupDlg.RunTests('RepeatedHotkey Truncation')
# if there are any bugs they will be printed to the console
# and the controls will be highlighted
tests.print_bugs(bugs)
# ----- Next Page Setup Dialog ----
app.PageSetupDlg.Printer.Click()
# do some radio button clicks
# Open the Connect to printer dialog so we can
# try out checking/unchecking a checkbox
app.PageSetupDlg.Network.Click()
# ----- Connect To Printer Dialog ----
# Select a checkbox
app.ConnectToPrinter.ExpandByDefault.Check()
app.ConnectToPrinter.ExpandByDefault.UnCheck()
# try doing the same by using click
app.ConnectToPrinter.ExpandByDefault.Click()
app.ConnectToPrinter.ExpandByDefault.Click()
# close the dialog
app.ConnectToPrinter.Cancel.CloseClick()
# ----- 2nd Page Setup Dialog again ----
app.PageSetupDlg2.Properties.Click()
doc_props = app.window_(title_re = ".*Document Properties")
# ----- Document Properties Dialog ----
# some tab control selections
# Two ways of selecting tabs with indices...
doc_props.TabCtrl.Select(0)
doc_props.TabCtrl.Select(1)
doc_props.TabCtrl.Select(2)
# or with text...
doc_props.TabCtrl.Select("PaperQuality")
doc_props.TabCtrl.Select("JobRetention")
doc_props.TabCtrl.Select("Layout")
# do some radio button clicks
doc_props.RotatedLandscape.Click()
doc_props.BackToFront.Click()
doc_props.FlipOnShortEdge.Click()
doc_props.Portrait.Click()
doc_props._None.Click()
doc_props.FrontToBack.Click()
# open the Advanced options dialog in two steps
advbutton = doc_props.Advanced
advbutton.Click()
# close the 4 windows
# ----- Advanced Options Dialog ----
app.window_(title_re = ".* Advanced Options").Ok.Click()
# ----- Document Properties Dialog again ----
doc_props.Cancel.CloseClick()
# ----- 2nd Page Setup Dialog again ----
app.PageSetupDlg2.OK.CloseClick()
# ----- Page Setup Dialog ----
app.PageSetupDlg.Ok.CloseClick()
# type some text - note that extended characters ARE allowed
app.Notepad.Edit.SetText(u"I am typing s\xe4me text to Notepad\r\n\r\n"
"And then I am going to quit")
# the following shows that Sendtext does not accept
# accented characters - but does allow 'control' characters
app.Notepad.Edit.TypeKeys(u"{END}{ENTER}SendText d\xf6\xe9s not "
u"s\xfcpp\xf4rt \xe0cce\xf1ted characters", with_spaces = True)
# Try and save
app.Notepad.MenuSelect("File->SaveAs")
app.SaveAs.ComboBox5.Select("UTF-8")
app.SaveAs.edit1.SetText("Example-utf8.txt")
app.SaveAs.Save.CloseClick()
# my machine has a weird problem - when connected to the network
# the SaveAs Dialog appears - but doing anything with it can
# cause a LONG delay - the easiest thing is to just wait
# until the dialog is no longer active
# - Dialog might just be gone - because click worked
# - dialog might be waiting to disappear
# so can't wait for next dialog or for it to be disabled
# - dialog might be waiting to display message box so can't wait
# for it to be gone or for the main dialog to be enabled.
# while the dialog exists wait upto 30 seconds (and yes it can
# take that long on my computer sometimes :-( )
app.SaveAs.Cancel.WaitNotEnabled()
try:
app.SaveAs.Yes.CloseClick()
except MatchError:
pass
# exit notepad
app.Notepad.MenuSelect("File->Exit")