def SendKeysToAllWindows(self, title_regex): "Sends the keystroke to all windows whose title matches the regex" # We need to call find_windows on our own because Application.connect_ will # call find_window and throw if it finds more than one match. all_matches = pywinauto.findwindows.find_windows(title_re=title_regex) # We need to store all window handles that have been sent keys in order # to avoid reactivating windows and doing unnecesary refreshes. This is a # side effect of having to call Application.connect_ on each regex match. # We need to loop through each open window collection to support edge # cases like Google Canary where the Window title is identical to Chrome. processed_handles = [] for win in all_matches: app = Application() app.connect_(handle=win) open_windows = app.windows_(title_re=title_regex) for openwin in open_windows: if openwin.handle in processed_handles: continue openwin.TypeKeys("{F5}") processed_handles.append(openwin.handle) time.sleep(1)
def SendKeysToAllWindows(self, title_regex): "Sends the keystroke to all windows whose title matches the regex" # We need to call find_windows on our own because Application.connect_ will # call find_window and throw if it finds more than one match. all_matches = pywinauto.findwindows.find_windows(title_re=title_regex) # We need to store all window handles that have been sent keys in order # to avoid reactivating windows and doing unnecesary refreshes. This is a # side effect of having to call Application.connect_ on each regex match. # We need to loop through each open window collection to support edge # cases like Google Canary where the Window title is identical to Chrome. processed_handles = [] for win in all_matches: app = Application() app.connect_(handle=win) open_windows = app.windows_(title_re=title_regex) for openwin in open_windows: if openwin.handle in processed_handles: continue openwin.TypeKeys('{F5}') processed_handles.append(openwin.handle) time.sleep(1)
def testWindows(self): "Test that windows_() works correctly" app = Application() self.assertRaises(AppNotConnected, app.windows_, **{'title' : 'not connected'}) app.start('notepad.exe') notepad_handle = app.UntitledNotepad.handle self.assertEquals(app.windows_(visible_only = True), [notepad_handle]) app.UntitledNotepad.MenuSelect("Help->About Notepad") aboutnotepad_handle = app.AboutNotepad.handle self.assertEquals( app.windows_(visible_only = True, enabled_only = False), [aboutnotepad_handle, notepad_handle]) app.AboutNotepad.OK.Click() app.UntitledNotepad.MenuSelect("File->Exit")
def setUp(self): """Start the application set some data and ensure the application is in the state we want it.""" self.texts = ['Tooltip Tool 0', 'Tooltip Tool 1', 'Tooltip Tool 2'] # start the application from pywinauto.application import Application app = Application() app.start_(controlspy_folder + "Tooltip.exe") self.app = app self.dlg = app.MicrosoftControlSpy tips = app.windows_(visible_only=False, enabled_only=False, top_level_only=False, class_name="tooltips_class32") self.ctrl = WrapHandle(tips[1])
def setUp(self): """Start the application set some data and ensure the application is in the state we want it.""" self.texts = [u'Tooltip Tool 0', u'Tooltip Tool 1', u'Tooltip Tool 2'] # start the application from pywinauto.application import Application app = Application() app.start_(os.path.join(controlspy_folder, "Tooltip.exe")) self.app = app self.dlg = app.MicrosoftControlSpy tips = app.windows_( visible_only = False, enabled_only = False, top_level_only = False, class_name = "tooltips_class32") self.ctrl = WrapHandle(tips[1])
class UIAWrapperTests(unittest.TestCase): "Unit tests for the UIAWrapper class" def setUp(self): """Start the application set some data and ensure the application is in the state we want it.""" # start the application self.app = Application(backend='uia') self.app = self.app.Start(wpf_app_1) self.dlg = self.app.WPFSampleApplication def tearDown(self): "Close the application after tests" self.app.kill_() def testFriendlyClassName(self): "Test getting the friendly classname of the dialog" button = self.dlg.OK.WrapperObject() self.assertEqual(button.friendly_class_name(), "Button") def test_find_nontop_ctl_by_class_name_and_title(self): """Test getting a non-top control by a class name and a title""" # Look up for a non-top button control with 'Apply' caption self.dlg.Wait('ready') caption = 'Apply' wins = self.app.windows_(top_level_only=False, class_name='Button', title=caption) # Verify the number of found wrappers self.assertEqual(len(wins), 1) # Verify the caption of the found wrapper self.assertEqual(wins[0].texts()[0], caption) def test_find_top_win_by_class_name_and_title(self): """Test getting a top window by a class name and a title""" # Since the top_level_only is True by default # we don't specify it as a criteria argument self.dlg.Wait('ready') caption = 'WPF Sample Application' wins = self.app.windows_(class_name='Window', title=caption) # Verify the number of found wrappers self.assertEqual(len(wins), 1) # Verify the caption of the found wrapper self.assertEqual(wins[0].texts()[0], caption) def testClass(self): "Test getting the classname of the dialog" button = self.dlg.OK.WrapperObject() self.assertEqual(button.class_name(), "Button") def testWindowText(self): "Test getting the window Text of the dialog" label = self.dlg.TestLabel.WrapperObject() self.assertEqual(label.window_text(), u"TestLabel") def testControlID(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.control_id(), None) def testIsVisible(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.is_visible(), True) def testIsEnabled(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.is_enabled(), True) def testProcessID(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.process_id(), self.dlg.process_id()) self.assertNotEqual(button.process_id(), 0) def testIsDialog(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.is_dialog(), False) def testParent(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.parent(), self.dlg.WrapperObject()) def testTopLevelParent(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.top_level_parent(), self.dlg.WrapperObject()) def testTexts(self): self.assertEqual(self.dlg.texts(), ['WPF Sample Application']) def testChildren(self): button = self.dlg.OK.WrapperObject() self.assertEqual(len(button.children()), 1) self.assertEqual(button.children()[0].class_name(), "TextBlock") def testIsChild(self): button = self.dlg.OK.WrapperObject() self.assertEqual(button.is_child(self.dlg.WrapperObject()), True) def testEquals(self): button = self.dlg.OK.WrapperObject() self.assertNotEqual(button, self.dlg.WrapperObject()) self.assertEqual(button, button.element_info) self.assertEqual(button, button) #def testVerifyActionable(self): # self.assertRaises() #def testVerifyEnabled(self): # self.assertRaises() #def testVerifyVisible(self): # self.assertRaises() def testIsKeyboardFocusable(self): edit = self.dlg.TestLabelEdit.WrapperObject() label = self.dlg.TestLabel.WrapperObject() button = self.dlg.OK.WrapperObject() self.assertEqual(button.is_keyboard_focusable(), True) self.assertEqual(edit.is_keyboard_focusable(), True) self.assertEqual(label.is_keyboard_focusable(), False) def testHasKeyboardFocus(self): edit = self.dlg.TestLabelEdit.WrapperObject() edit.set_focus() self.assertEqual(edit.has_keyboard_focus(), True) def testSetFocus(self): edit = self.dlg.TestLabelEdit.WrapperObject() edit.set_focus() self.assertEqual(edit.has_keyboard_focus(), True) def testTypeKeys(self): edit = self.dlg.TestLabelEdit.WrapperObject() edit.type_keys("testTypeKeys") self.assertEqual(edit.window_text(), "testTypeKeys") def testNoPatternInterfaceError(self): "Test a query interface exception handling" button = self.dlg.OK.WrapperObject() elem = button.element_info.element self.assertRaises( uia_defs.NoPatternInterfaceError, uia_defs.get_elem_interface, elem, "Selection", ) def testGetProperties(self): uia_props = set([ 'class_name', 'friendly_class_name', 'texts', 'control_id', 'rectangle', 'is_visible', 'is_enabled', 'control_count', 'is_keyboard_focusable', 'has_keyboard_focus', 'selection_indices', ]) edit = self.dlg.TestLabelEdit.WrapperObject() props = set(edit.get_properties().keys()) self.assertEqual(props, uia_props)