コード例 #1
0
 def is_radians(h):
     res = False
     hwwrp = HwndWrapper(h)
     if hwwrp.FriendlyClassName() == u'RadioButton':
         if hwwrp.Texts() == [u'Radians']:
             res = True
     return res
コード例 #2
0
    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()
        if is_x64_Python() or not is_x64_OS():
            self.app.start_(r"C:\Windows\System32\notepad.exe")
        else:
            self.app.start_(r"C:\Windows\SysWOW64\notepad.exe")

        # Get the old font
        self.app.UntitledNotepad.MenuSelect("Format->Font")

        self.old_font = self.app.Font.FontComboBox.SelectedIndex()
        self.old_font_style = self.app.Font.FontStyleCombo.SelectedIndex()

        # ensure we have the correct settings for this test
        self.app.Font.FontStyleCombo.Select(0)
        self.app.Font.FontComboBox.Select("Lucida Console")
        self.app.Font.OK.Click()

        self.dlg = self.app.Window_(title='Untitled - Notepad',
                                    class_name='Notepad')
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
        self.dlg.edit.SetEditText("Here is some text\r\n and some more")
コード例 #3
0
 def is_checkbox(elem):
     res = False
     if elem.handle is None:
         return False
     hwwrp = HwndWrapper(elem.handle)
     if hwwrp.friendly_class_name() == u'CheckBox':
         if hwwrp.texts() == [u'Show']:
             res = True
     return res
コード例 #4
0
 def is_checkbox(elem):
     res = False
     if elem.handle is None:
         return False
     hwwrp = HwndWrapper(elem.handle)
     if hwwrp.friendly_class_name() == u'CheckBox':
         if hwwrp.texts() == [u'Show']:
             res = True
     return res
コード例 #5
0
    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.start(os.path.join(mfc_samples_folder, u"CmnCtrl3.exe"))

        self.dlg = self.app.Common_Controls_Sample
        self.dlg.TabControl.Select('CButton (Command Link)')
        self.ctrl = HwndWrapper(self.dlg.NoteEdit.handle)
コード例 #6
0
    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()
        self.app.start_("calc.exe")

        self.dlg = self.app.Calculator
        self.ctrl = HwndWrapper(self.dlg.Backspace.handle)
コード例 #7
0
    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()
        self.app.start_("notepad.exe")

        self.dlg = self.app.UntitledNotepad
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
        self.dlg.edit.SetEditText("Here is some text\r\n and some more")
コード例 #8
0
    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()
        if is_x64_Python() or not is_x64_OS():
            self.app.start_(r"C:\Windows\System32\calc.exe")
        else:
            self.app.start_(r"C:\Windows\SysWOW64\calc.exe")

        self.dlg = self.app.Calculator
        self.dlg.MenuSelect('View->Scientific\tAlt+2')
        self.ctrl = HwndWrapper(self.dlg.Button2.handle)  # Backspace
コード例 #9
0
 def testTexts(self):
     self.assertEqual(self.dlg.Texts(), ['Calculator'])
     self.assertEqual(
         HwndWrapper(self.dlg.Degrees.handle).Texts(), [u'Degrees'])
     self.assertEqual(
         self.dlg.ChildWindow(class_name='Static', ctrl_index=5).Texts(),
         ['0'])
コード例 #10
0
    def setUp(self):
        """Start the application set some data and ensure the application
        is in the state we want it."""

        self.app = Application().start(os.path.join(mfc_samples_folder, u"CmnCtrl3.exe"))

        self.dlg = self.app.Common_Controls_Sample
        self.dlg.TabControl.Select('CButton (Command Link)')
        self.ctrl = HwndWrapper(self.dlg.Command_button_here.handle)
コード例 #11
0
    def setUp(self):
        """Start the application set some data and ensure the application
        is in the state we want it."""

        self.app = Application()
        self.app.start(_notepad_exe())

        self.dlg = self.app.UntitledNotepad
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
コード例 #12
0
    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()
        self.app.start_(_notepad_exe())

        self.dlg = self.app.Window_(title='Untitled - Notepad', class_name='Notepad')
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
        self.dlg.edit.SetEditText("Here is some text\r\n and some more")

        self.app2 = Application.start(_notepad_exe())
コード例 #13
0
    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()
        self.app.start_("notepad.exe")

        # Get the old font
        self.app.UntitledNotepad.MenuSelect("Format->Font")

        self.old_font = self.app.Font.FontComboBox.SelectedIndex()
        self.old_font_style = self.app.Font.FontStyleCombo.SelectedIndex()

        # ensure we have the correct settings for this test
        self.app.Font.FontStyleCombo.Select(0)
        self.app.Font.FontComboBox.Select("Lucida Console")
        self.app.Font.OK.Click()

        self.dlg = self.app.UntitledNotepad
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
        self.dlg.edit.SetEditText("Here is some text\r\n and some more")
コード例 #14
0
    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()
        if is_x64_Python() or not is_x64_OS():
            self.app.start_(r"C:\Windows\System32\notepad.exe")
        else:
            self.app.start_(r"C:\Windows\SysWOW64\notepad.exe")

        self.dlg = self.app.UntitledNotepad
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
コード例 #15
0
else:
    app = application.Application().connect(title_re=".*Mozilla Firefox")
    mozilla = app.window_(title_re=".*Mozilla Firefox")

# ie doesn't define it's menus as Menu's but actually as a toolbar!
print("No Menu's in FireFox:", mozilla.menu_items())

# File -> Save As
mozilla.type_keys("%FA")
#ie.Toolbar3.press_button("File")
app.SaveAs.Edit.set_edit_text(outputfilename)

app.SaveAs.Save.close_click()

try:
    # if asked to overwrite say yes
    if app.SaveAs.Yes.Exists():
        app.SaveAs.Yes.close_click()
except WindowAmbiguousError as e:
    for w in e.windows:
        w = HwndWrapper(w)
        print(w.window_text(), w.class_name())

print("saved:", outputfilename)

# File close tab or close
#(Firefox makes it easy for us having the same shortcut for both!
mozilla.type_keys("%FC")

コード例 #16
0
class HwndWrapperMouseTests(unittest.TestCase):
    "Unit tests for mouse actions of the HwndWrapper 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()
        self.app.start_("notepad.exe")

        # Get the old font
        self.app.UntitledNotepad.MenuSelect("Format->Font")

        self.old_font = self.app.Font.FontComboBox.SelectedIndex()
        self.old_font_style = self.app.Font.FontStyleCombo.SelectedIndex()

        # ensure we have the correct settings for this test
        self.app.Font.FontStyleCombo.Select(0)
        self.app.Font.FontComboBox.Select("Lucida Console")
        self.app.Font.OK.Click()

        self.dlg = self.app.UntitledNotepad
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
        self.dlg.edit.SetEditText("Here is some text\r\n and some more")

    def tearDown(self):
        "Close the application after tests"

        # Set the old font again
        self.app.UntitledNotepad.MenuSelect("Format->Font")
        self.app.Font.FontComboBox.Select(self.old_font)
        self.app.Font.FontStyleCombo.Select(self.old_font_style)
        self.app.Font.OK.Click()

        # close the application
        self.dlg.TypeKeys("%{F4}")
        if self.app.Notepad.No.Exists():
            self.app.Notepad.No.Click()

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.WindowText(), "Untitled - Notepad")

    def testClick(self):
        self.ctrl.Click(coords=(50, 10))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (5, 5))

    def testClickInput(self):
        self.ctrl.ClickInput(coords=(50, 10))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (5, 5))

    def testDoubleClick(self):
        self.ctrl.DoubleClick(coords=(60, 30))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (24, 29))

    def testDoubleClickInput(self):
        self.ctrl.DoubleClickInput(coords=(60, 30))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (24, 29))

    def testMenuSelectNotepad_bug(self):
        "In notepad - MenuSelect Edit->Paste did not work"

        text = u'Here are some unicode characters \xef\xfc\r\n'
        app2 = Application.start("notepad")
        app2.UntitledNotepad.Edit.SetEditText(text)

        app2.UntitledNotepad.MenuSelect("Edit->Select All")
        app2.UntitledNotepad.MenuSelect("Edit->Copy")

        self.dlg.MenuSelect("Edit->Select All")
        self.dlg.MenuSelect("Edit->Paste")
        self.dlg.MenuSelect("Edit->Paste")
        self.dlg.MenuSelect("Edit->Paste")

        app2.UntitledNotepad.MenuSelect("File->Exit")
        app2.Notepad.No.Click()

        self.assertEquals(self.dlg.Edit.TextBlock(), text * 3)
コード例 #17
0
class HwndWrapperTests(unittest.TestCase):
    "Unit tests for the TreeViewWrapper class"

    def setUp(self):
        """Start the application set some data and ensure the application
        is in the state we want it."""

        self.app = Application().start(
            os.path.join(mfc_samples_folder, u"CmnCtrl3.exe"))

        self.dlg = self.app.Common_Controls_Sample
        self.dlg.TabControl.Select('CButton (Command Link)')
        self.ctrl = HwndWrapper(self.dlg.Command_button_here.handle)

        #self.dlg = self.app.Calculator
        #self.dlg.MenuSelect('View->Scientific\tAlt+2')
        #self.ctrl = HwndWrapper(self.dlg.Button2.handle) # Backspace

    def tearDown(self):
        "Close the application after tests"
        # close the application
        #self.dlg.type_keys("%{F4}")
        #self.dlg.Close()
        self.app.kill_()

    def testInvalidHandle(self):
        "Test that an exception is raised with an invalid window handle"
        self.assertRaises(InvalidWindowHandle, HwndWrapper, -1)

    def testFriendlyClassName(self):
        "Test getting the friendly classname of the control"
        self.assertEquals(self.ctrl.friendly_class_name(), "Button")

    def testClass(self):
        "Test getting the classname of the control"
        self.assertEquals(self.ctrl.class_name(), "Button")

    def testWindowText(self):
        "Test getting the window Text of the control"
        self.assertEquals(
            HwndWrapper(self.dlg.Set.handle).window_text(), u'Set')

    def testStyle(self):

        self.dlg.Style()

        self.assertEquals(
            self.ctrl.Style(), win32defines.WS_CHILD | win32defines.WS_VISIBLE
            | win32defines.WS_TABSTOP | win32defines.BS_COMMANDLINK)

    def testExStyle(self):
        self.assertEquals(
            self.ctrl.ExStyle(), win32defines.WS_EX_NOPARENTNOTIFY
            | win32defines.WS_EX_LEFT | win32defines.WS_EX_LTRREADING
            | win32defines.WS_EX_RIGHTSCROLLBAR)
        """self.assertEquals(self.dlg.ExStyle(),
            win32defines.WS_EX_WINDOWEDGE |
            win32defines.WS_EX_LEFT |
            win32defines.WS_EX_LTRREADING |
            win32defines.WS_EX_RIGHTSCROLLBAR |
            win32defines.WS_EX_CONTROLPARENT |
            win32defines.WS_EX_APPWINDOW)"""

    def testControlID(self):
        self.assertEquals(self.ctrl.control_id(), 1037)
        self.dlg.control_id()

    def testUserData(self):
        self.ctrl.UserData()
        self.dlg.UserData()

    def testContextHelpID(self):
        self.ctrl.ContextHelpID()
        self.dlg.ContextHelpID()

    def testIsVisible(self):
        self.assertEqual(self.ctrl.is_visible(), True)
        self.assertEqual(self.dlg.is_visible(), True)

    def testIsUnicode(self):
        self.assertEqual(self.ctrl.IsUnicode(), True)
        self.assertEqual(self.dlg.IsUnicode(), True)

    def testIsEnabled(self):
        self.assertEqual(self.ctrl.is_enabled(), True)
        self.assertEqual(self.dlg.is_enabled(), True)
        #self.assertEqual(self.dlg.Note.is_enabled(), False); # Button26 = '%'

    def testRectangle(self):
        "Test getting the rectangle of the dialog"
        rect = self.dlg.rectangle()

        self.assertNotEqual(rect.top, None)
        self.assertNotEqual(rect.left, None)
        self.assertNotEqual(rect.bottom, None)
        self.assertNotEqual(rect.right, None)

        if abs(rect.height() - 423) > 5:
            self.assertEqual(rect.height(), 423)
        if abs(rect.width() - 506) > 5:
            self.assertEqual(rect.width(), 506)

    def testClientRect(self):
        rect = self.dlg.rectangle()
        cli = self.dlg.ClientRect()

        self.assertEqual(cli.left, 0)
        self.assertEqual(cli.top, 0)

        assert (cli.width() < rect.width())
        assert (cli.height() < rect.height())

    def testFont(self):
        self.assertNotEqual(self.dlg.Font(), self.ctrl.Font())

    def testProcessID(self):
        self.assertEqual(self.ctrl.process_id(), self.dlg.process_id())
        self.assertNotEqual(self.ctrl.process_id(), 0)

    def testHasStyle(self):
        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_CHILD), True)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_CHILD), False)

        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_SYSMENU), False)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_SYSMENU), True)

    def testHasExStyle(self):
        self.assertEqual(
            self.ctrl.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), True)
        self.assertEqual(
            self.dlg.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), False)

        self.assertEqual(self.ctrl.HasExStyle(win32defines.WS_EX_APPWINDOW),
                         False)
        #self.assertEqual(self.dlg.HasExStyle(win32defines.WS_EX_APPWINDOW), True)

    def testIsDialog(self):
        self.assertEqual(self.ctrl.is_dialog(), False)
        self.assertEqual(self.dlg.is_dialog(), True)

    def testParent(self):
        self.assertEqual(self.ctrl.parent().parent(), self.dlg.handle)

    def testTopLevelParent(self):
        self.assertEqual(self.ctrl.top_level_parent(), self.dlg.handle)
        self.assertEqual(self.dlg.top_level_parent(), self.dlg.handle)

    def testTexts(self):
        self.assertEqual(self.dlg.texts(), ['Common Controls Sample'])
        self.assertEqual(HwndWrapper(self.dlg.Show.handle).texts(), [u'Show'])
        self.assertEqual(
            self.dlg.ChildWindow(class_name='Button', found_index=2).texts(),
            [u'Elevation Icon'])

    def testFoundIndex(self):
        "test an access to a control by found_index"

        ctl = self.dlg.ChildWindow(class_name='Button', found_index=3)
        self.assertEqual(ctl.texts(), [u'Show'])
        ctl.DrawOutline('blue')  # visualize

        # Test an out-of-range access
        # Notice:
        # A ChildWindow call only creates a WindowSpecification object.
        # The exception is raised later when we try to find the window.
        # For this reason we can't use an assertRaises statement here because
        # the exception is raised before actual call to DrawOutline
        ctl = self.dlg.ChildWindow(class_name='Button', found_index=3333)
        self.assertRaises(ElementNotFoundError, ctl.WrapperObject)

    def testSearchWithPredicateFunc(self):
        "test an access to a control by filtering with a predicate function"

        def is_checkbox(elem):
            res = False
            if elem.handle is None:
                return False
            hwwrp = HwndWrapper(elem.handle)
            if hwwrp.friendly_class_name() == u'CheckBox':
                if hwwrp.texts() == [u'Show']:
                    res = True
            return res

        ctl = self.dlg.ChildWindow(predicate_func=is_checkbox)
        self.assertEqual(ctl.texts(), [u'Show'])
        ctl.DrawOutline('red')  # visualize

    def testClientRects(self):
        self.assertEqual(self.ctrl.ClientRects()[0], self.ctrl.ClientRect())
        self.assertEqual(self.dlg.ClientRects()[0], self.dlg.ClientRect())

    def testFonts(self):
        self.assertEqual(self.ctrl.Fonts()[0], self.ctrl.Font())
        self.assertEqual(self.dlg.Fonts()[0], self.dlg.Font())

    def testChildren(self):
        self.assertEqual(self.ctrl.children(), [])
        self.assertNotEqual(self.dlg.children(), [])

    def testIsChild(self):
        self.assertEqual(self.ctrl.is_child(self.dlg.WrapperObject()), True)
        self.assertEqual(self.dlg.is_child(self.ctrl), False)

    def testSendMessage(self):
        vk = self.dlg.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Edit.SendMessage(win32defines.WM_GETDLGCODE)
        # The expected return code is: "Edit" ? # "Button" = 0x2000 and "Radio" = 0x40
        expected = 0x89  # 0x2000 + 0x40
        self.assertEqual(expected, code)

    def testSendMessageTimeout(self):
        default_timeout = Timings.sendmessagetimeout_timeout
        Timings.sendmessagetimeout_timeout = 0.1
        vk = self.dlg.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Show.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        # The expected return code is: "Button" = 0x2000 # and "Radio" = 0x40
        expected = 0x2000  #+ 0x40
        Timings.sendmessagetimeout_timeout = default_timeout
        self.assertEqual(expected, code)

    def testPostMessage(self):
        self.assertNotEquals(0, self.dlg.PostMessage(win32defines.WM_PAINT))
        self.assertNotEquals(0,
                             self.dlg.Show.PostMessage(win32defines.WM_PAINT))

#    def testNotifyMenuSelect(self):
#        "Call NotifyMenuSelect to ensure it does not raise"
#        self.ctrl.NotifyMenuSelect(1234)
#        self.dlg.NotifyMenuSelect(1234)

    def testNotifyParent(self):
        "Call NotifyParent to ensure it does not raise"
        self.ctrl.NotifyParent(1234)
        #self.dlg.NotifyParent(1234)

    def testGetProperties(self):
        "Test getting the properties for the HwndWrapped control"
        props = self.dlg.GetProperties()

        self.assertEquals(self.dlg.friendly_class_name(),
                          props['friendly_class_name'])

        self.assertEquals(self.dlg.texts(), props['texts'])

        for prop_name in props:
            self.assertEquals(getattr(self.dlg, prop_name)(), props[prop_name])

#    def testCaptureAsImage(self):
#        pass

# def testDrawOutline(self):
#     """Test the outline was drawn."""
#     # make sure window is ready
#     self.dlg.Wait('active')
#     self.dlg.Show.Click()
#
#     # not sure why, but this extra call makes the test stable
#     self.dlg.draw_outline()
#
#     # outline control
#     self.dlg.Show.draw_outline()
#     img1 = self.dlg.Show.capture_as_image()
#     self.assertEqual(img1.getpixel((0, 0)), (0, 255, 0))  # green
#
#     # outline window
#     self.dlg.draw_outline(colour="red")
#     img2 = self.dlg.capture_as_image()
#     self.assertEqual(img2.getpixel((0, 0)), (255, 0, 0))  # red

    def testEquals(self):
        self.assertNotEqual(self.ctrl, self.dlg.handle)
        self.assertEqual(self.ctrl, self.ctrl.handle)
        self.assertEqual(self.ctrl, self.ctrl)


#    def testVerifyActionable(self):
#        self.assertRaises()

#    def testVerifyEnabled(self):
#        self.assertRaises()

#    def testVerifyVisible(self):
#        self.assertRaises()

    def testMoveWindow_same(self):
        "Test calling movewindow without any parameters"
        prevRect = self.dlg.rectangle()
        self.dlg.MoveWindow()
        self.assertEquals(prevRect, self.dlg.rectangle())

    def testMoveWindow(self):
        "Test moving the window"

        dlgClientRect = self.ctrl.parent().rectangle(
        )  # use the parent as a reference

        prev_rect = self.ctrl.rectangle() - dlgClientRect

        new_rect = win32structures.RECT(prev_rect)
        new_rect.left -= 1
        new_rect.top -= 1
        new_rect.right += 2
        new_rect.bottom += 2

        self.ctrl.MoveWindow(
            new_rect.left,
            new_rect.top,
            new_rect.width(),
            new_rect.height(),
        )
        time.sleep(0.1)

        print('prev_rect = ', prev_rect)
        print('new_rect = ', new_rect)
        print('dlgClientRect = ', dlgClientRect)
        print('self.ctrl.rectangle() = ', self.ctrl.rectangle())
        self.assertEquals(self.ctrl.rectangle(), new_rect + dlgClientRect)

        self.ctrl.MoveWindow(prev_rect)

        self.assertEquals(self.ctrl.rectangle(), prev_rect + dlgClientRect)

    def testMaximize(self):
        self.dlg.Maximize()

        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMAXIMIZED)
        self.dlg.Restore()

    def testMinimize(self):
        self.dlg.Minimize()
        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMINIMIZED)
        self.dlg.Restore()

    def testRestore(self):
        self.dlg.Maximize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

        self.dlg.Minimize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

    def testGetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), None)
        self.assertEqual(self.dlg.GetFocus(), self.ctrl.GetFocus())

        self.dlg.Set.set_focus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Set.handle)

    def testSetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), self.dlg.Set.handle)
        self.dlg.Set.set_focus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Set.handle)
コード例 #18
0
class HwndWrapperTests(unittest.TestCase):
    "Unit tests for the TreeViewWrapper 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()
        if is_x64_Python() or not is_x64_OS():
            self.app.start_(r"C:\Windows\System32\calc.exe")
        else:
            self.app.start_(r"C:\Windows\SysWOW64\calc.exe")

        self.dlg = self.app.Calculator
        self.dlg.MenuSelect('View->Scientific\tAlt+2')
        self.ctrl = HwndWrapper(self.dlg.Button2.handle)  # Backspace

    def tearDown(self):
        "Close the application after tests"
        # close the application
        #self.dlg.TypeKeys("%{F4}")
        #self.dlg.Close()
        self.app.kill_()

    def testInvalidHandle(self):
        "Test that an exception is raised with an invalid window handle"
        self.assertRaises(InvalidWindowHandle, HwndWrapper, -1)

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.WindowText(), "Untitled - Notepad")

    def testFriendlyClassName(self):
        "Test getting the friendly classname of the dialog"
        self.assertEquals(self.ctrl.FriendlyClassName(), "Button")

    def testClass(self):
        "Test getting the classname of the dialog"
        self.assertEquals(self.ctrl.Class(), "Button")

    def testWindowText(self):
        "Test getting the window Text of the dialog"
        self.assertEquals(self.ctrl.WindowText(), '\uf013')  #"Backspace")

    def testStyle(self):

        self.dlg.Style()

        self.assertEquals(
            self.ctrl.Style(), win32defines.WS_CHILD | win32defines.WS_VISIBLE
            | win32defines.BS_PUSHBUTTON | win32defines.BS_TEXT)

    def testExStyle(self):
        self.assertEquals(
            self.ctrl.ExStyle(), win32defines.WS_EX_NOPARENTNOTIFY
            | win32defines.WS_EX_LEFT | win32defines.WS_EX_LTRREADING
            | win32defines.WS_EX_RIGHTSCROLLBAR)
        """self.assertEquals(self.dlg.ExStyle(),
            win32defines.WS_EX_WINDOWEDGE |
            win32defines.WS_EX_LEFT |
            win32defines.WS_EX_LTRREADING |
            win32defines.WS_EX_RIGHTSCROLLBAR |
            win32defines.WS_EX_CONTROLPARENT |
            win32defines.WS_EX_APPWINDOW)"""

    def testControlID(self):
        self.assertEquals(self.ctrl.ControlID(), 83)
        self.dlg.ControlID()

    def testUserData(self):
        self.ctrl.UserData()
        self.dlg.UserData()

    def testContextHelpID(self):
        self.ctrl.ContextHelpID()
        self.dlg.ContextHelpID()

    def testIsVisible(self):
        self.assertEqual(self.ctrl.IsVisible(), True)
        self.assertEqual(self.dlg.IsVisible(), True)

    def testIsUnicode(self):
        self.assertEqual(self.ctrl.IsUnicode(), True)
        self.assertEqual(self.dlg.IsUnicode(), True)

    def testIsEnabled(self):
        self.assertEqual(self.ctrl.IsEnabled(), True)
        self.assertEqual(self.dlg.IsEnabled(), True)
        self.assertEqual(
            self.dlg.ChildWindow(title='%', enabled_only=False).IsEnabled(),
            False)

    def testCloseClick_bug(self):
        self.dlg.MenuSelect('Help->About Calculator')
        self.app.AboutCalculator.CloseButton.CloseClick()
        Timings.closeclick_dialog_close_wait = .5
        try:
            self.app.AboutCalculator.CloseClick()
        except timings.TimeoutError:
            pass

        self.app.AboutCalculator.Close()

        #self.assertEquals(self.app.StatisticsBox.Exists(), False)

    def testRectangle(self):
        "Test getting the rectangle of the dialog"
        rect = self.dlg.Rectangle()
        self.assertNotEqual(rect.top, None)
        self.assertNotEqual(rect.left, None)
        self.assertNotEqual(rect.bottom, None)
        self.assertNotEqual(rect.right, None)

        self.assertEqual(rect.height(), 310)
        self.assertEqual(rect.width(), 413)

    def testClientRect(self):
        rect = self.dlg.Rectangle()
        cli = self.dlg.ClientRect()

        self.assertEqual(cli.left, 0)
        self.assertEqual(cli.top, 0)

        assert (cli.width() < rect.width())
        assert (cli.height() < rect.height())

    def testFont(self):
        self.assertNotEqual(self.dlg.Font(), self.ctrl.Font())

    def ProcessID(self):
        self.assertEqual(self.ctrl.ProcessID(), self.dlg.ProcessID)
        self.assertNotEqual(self.ctrl.ProcessID(), 0)

    def testHasStyle(self):
        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_CHILD), True)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_CHILD), False)

        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_SYSMENU), False)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_SYSMENU), True)

    def testHasExStyle(self):
        self.assertEqual(
            self.ctrl.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), True)
        self.assertEqual(
            self.dlg.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), False)

        self.assertEqual(self.ctrl.HasExStyle(win32defines.WS_EX_APPWINDOW),
                         False)
        #self.assertEqual(self.dlg.HasExStyle(win32defines.WS_EX_APPWINDOW), True)

    def testIsDialog(self):
        self.assertEqual(self.ctrl.IsDialog(), False)
        self.assertEqual(self.dlg.IsDialog(), True)

    def testMenuItems(self):
        self.assertEqual(self.ctrl.MenuItems(), [])
        self.assertEqual(self.dlg.MenuItems()[1]['Text'], '&Edit')

    def testParent(self):
        self.assertEqual(self.ctrl.Parent().Parent().Parent(), self.dlg.handle)

    def testTopLevelParent(self):
        self.assertEqual(self.ctrl.TopLevelParent(), self.dlg.handle)
        self.assertEqual(self.dlg.TopLevelParent(), self.dlg.handle)

    def testTexts(self):
        self.assertEqual(self.dlg.Texts(), ['Calculator'])
        self.assertEqual(self.ctrl.Texts(), ['\uf013'])  #u'Backspace'])
        self.assertEqual(
            self.dlg.ChildWindow(class_name='Static', ctrl_index=5).Texts(),
            ['0'])

    def testClientRects(self):
        self.assertEqual(self.ctrl.ClientRects()[0], self.ctrl.ClientRect())
        self.assertEqual(self.dlg.ClientRects()[0], self.dlg.ClientRect())

    def testFonts(self):
        self.assertEqual(self.ctrl.Fonts()[0], self.ctrl.Font())
        self.assertEqual(self.dlg.Fonts()[0], self.dlg.Font())

    def testChildren(self):
        self.assertEqual(self.ctrl.Children(), [])
        self.assertNotEqual(self.dlg.Children(), [])

    def testIsChild(self):
        self.assertEqual(self.ctrl.IsChild(self.dlg.WrapperObject()), True)
        self.assertEqual(self.dlg.IsChild(self.ctrl), False)

    def testSendMessage(self):
        vk = self.dlg.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Inv.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

    def testSendMessageTimeout(self):

        vk = self.dlg.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Inv.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

    def testPostMessage(self):
        self.assertNotEquals(0, self.dlg.PostMessage(win32defines.WM_PAINT))
        self.assertNotEquals(0,
                             self.dlg.Inv.PostMessage(win32defines.WM_PAINT))

#    def testNotifyMenuSelect(self):
#        "Call NotifyMenuSelect to ensure it does not raise"
#        self.ctrl.NotifyMenuSelect(1234)
#        self.dlg.NotifyMenuSelect(1234)

    def testNotifyParent(self):
        "Call NotifyParent to ensure it does not raise"
        self.ctrl.NotifyParent(1234)
        #self.dlg.NotifyParent(1234)

    def testGetProperties(self):
        "Test getting the properties for the HwndWrapped control"
        props = self.dlg.GetProperties()

        self.assertEquals(self.dlg.FriendlyClassName(),
                          props['FriendlyClassName'])

        self.assertEquals(self.dlg.Texts(), props['Texts'])

        for prop_name in props:
            self.assertEquals(getattr(self.dlg, prop_name)(), props[prop_name])

#    def testCaptureAsImage(self):
#        pass

    def testEquals(self):
        self.assertNotEqual(self.ctrl, self.dlg.handle)
        self.assertEqual(self.ctrl, self.ctrl.handle)
        self.assertEqual(self.ctrl, self.ctrl)


#    def testVerifyActionable(self):
#        self.assertRaises()

#    def testVerifyEnabled(self):
#        self.assertRaises()

#    def testVerifyVisible(self):
#        self.assertRaises()

    def testMoveWindow_same(self):
        "Test calling movewindow without any parameters"
        prevRect = self.dlg.Rectangle()
        self.dlg.MoveWindow()
        self.assertEquals(prevRect, self.dlg.Rectangle())

    def testMoveWindow(self):
        "Test moving the window"

        dlgClientRect = self.dlg.Rectangle()  #.ClientAreaRect()

        prev_rect = self.ctrl.Rectangle() - dlgClientRect

        new_rect = win32structures.RECT(prev_rect)
        new_rect.left -= 1
        new_rect.top -= 1
        new_rect.right += 2
        new_rect.bottom += 2

        self.ctrl.MoveWindow(
            new_rect.left,
            new_rect.top,
            new_rect.width(),
            new_rect.height(),
        )
        time.sleep(0.1)

        print('prev_rect = ', prev_rect)
        print('new_rect = ', new_rect)
        print('dlgClientRect = ', dlgClientRect)
        print('self.ctrl.Rectangle() = ', self.ctrl.Rectangle())
        self.assertEquals(self.ctrl.Rectangle(), new_rect + dlgClientRect)

        self.ctrl.MoveWindow(prev_rect)

        self.assertEquals(self.ctrl.Rectangle(), prev_rect + dlgClientRect)

    def testMaximize(self):
        self.dlg.Maximize()

        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMAXIMIZED)
        self.dlg.Restore()

    def testMinimize(self):
        self.dlg.Minimize()
        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMINIMIZED)
        self.dlg.Restore()

    def testRestore(self):
        self.dlg.Maximize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

        self.dlg.Minimize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

    def testGetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), None)
        self.assertEqual(self.dlg.GetFocus(), self.ctrl.GetFocus())

        self.dlg.Radians.SetFocus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Radians.handle)

    def testSetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), self.dlg.Radians.handle)
        self.dlg.Radians.SetFocus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Radians.handle)

    def testMenuSelect(self):
        "Test selecting a menu item"

        if not self.dlg.MenuItem("View -> Digit grouping").IsChecked():
            self.dlg.MenuSelect("View -> Digit grouping")

        self.dlg.TypeKeys("1234567")
        self.dlg.MenuSelect("Edit->Copy\tCtrl+C")
        self.dlg.CE.Click()
        self.assertEquals(
            self.dlg.ChildWindow(class_name='Static', ctrl_index=5).Texts()[0],
            "0")
        self.dlg.MenuSelect("Edit->Paste\tCtrl+V")
        self.assertEquals(
            self.dlg.ChildWindow(class_name='Static', ctrl_index=5).Texts()[0],
            "1 234 567")

    def testClose(self):
        "Test the Close() method of windows"
        # open about dialog
        self.dlg.MenuSelect('Help->About Calculator')

        # make sure it is open and visible
        self.assertTrue(
            self.app.Window_(title='About Calculator').IsVisible(), True)

        # close it
        self.app.Window_(title='About Calculator',
                         class_name='#32770').Close(1)

        # make sure that it is not visible
        try:
            #self.assertRaises(WindowNotFoundError, self.app.Window_(title='About Calculator', class_name='#32770').WrapperObject())
            # vvryabov: TimeoutError is caught by assertRaises, so the second raise is not caught correctly
            self.app.Window_(title='About Calculator',
                             class_name='#32770').WrapperObject()
        except WindowNotFoundError:
            print('WindowNotFoundError exception is raised as expected. OK.')

        # make sure the main calculator dialog is still open
        self.assertEquals(self.dlg.IsVisible(), True)
コード例 #19
0
class HwndWrapperMouseTests(unittest.TestCase):
    "Unit tests for mouse actions of the HwndWrapper class"

    def setUp(self):
        """Start the application set some data and ensure the application
        is in the state we want it."""

        self.app = Application().start(os.path.join(mfc_samples_folder, u"CmnCtrl3.exe"))

        self.dlg = self.app.Common_Controls_Sample
        self.dlg.TabControl.Select('CButton (Command Link)')
        self.ctrl = HwndWrapper(self.dlg.NoteEdit.handle)

    def tearDown(self):
        "Close the application after tests"

        # close the application
        try:
            self.dlg.Close(0.5)
        except Exception: # TimeoutError:
            pass
        finally:
            self.app.kill_()

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.window_text(), "Untitled - Notepad")


    def testClick(self):
        self.ctrl.Click(coords = (50, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (9,9))

    def testClickInput(self):
        self.ctrl.click_input(coords = (50, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (9,9))

    def testDoubleClick(self):
        self.ctrl.DoubleClick(coords = (50, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (8,13))

    def testDoubleClickInput(self):
        self.ctrl.double_click_input(coords = (80, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (13,18))

#    def testRightClick(self):
#        pass

    def testRightClickInput(self):
        self.dlg.Edit.type_keys('{HOME}')
        self.dlg.Edit.Wait('enabled').right_click_input()
        self.app.PopupMenu.Wait('ready').Menu().GetMenuPath('Select All')[0].click_input()
        self.dlg.Edit.type_keys('{DEL}')
        self.assertEquals(self.dlg.Edit.TextBlock(), '')

    def testPressMoveRelease(self):
        self.dlg.NoteEdit.PressMouse(coords=(0, 5))
        self.dlg.NoteEdit.MoveMouse(coords=(65, 5))
        self.dlg.NoteEdit.ReleaseMouse(coords=(65, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (0,12))

    def testDragMouse(self):
        self.dlg.NoteEdit.DragMouse(press_coords=(0, 5), release_coords=(65, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (0,12))
        
        # continue selection with pressed Shift key
        self.dlg.NoteEdit.DragMouse(press_coords=(65, 5), release_coords=(90, 5), pressed='shift')
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (0,17))

    def testDebugMessage(self):
        self.dlg.NoteEdit.DebugMessage('Test message')
        # TODO: add screenshots comparison

    #def testDrawOutline(self):
    #    # TODO: add screenshots comparison
    #    self.dlg.DrawOutline()

#    def testSetWindowText(self):
#        pass
#
#    def testTypeKeys(self):
#        pass

    def testSetTransparency(self):
        self.dlg.SetTransparency()
        self.assertRaises(ValueError, self.dlg.SetTransparency, 256)
コード例 #20
0
class HwndWrapperTests(unittest.TestCase):
    "Unit tests for the TreeViewWrapper 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()
        if is_x64_Python() or not is_x64_OS():
            self.app.start(r"C:\Windows\System32\calc.exe")
        else:
            self.app.start(r"C:\Windows\SysWOW64\calc.exe")

        self.dlg = self.app.Calculator
        self.dlg.MenuSelect('View->Scientific\tAlt+2')
        self.ctrl = HwndWrapper(self.dlg.Button2.handle)  # Backspace

    def tearDown(self):
        "Close the application after tests"
        # close the application
        #self.dlg.TypeKeys("%{F4}")
        #self.dlg.Close()
        self.app.kill_()

    def testInvalidHandle(self):
        "Test that an exception is raised with an invalid window handle"
        self.assertRaises(InvalidWindowHandle, HwndWrapper, -1)

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.WindowText(), "Untitled - Notepad")

    def testFriendlyClassName(self):
        "Test getting the friendly classname of the dialog"
        self.assertEquals(self.ctrl.FriendlyClassName(), "Button")

    def testClass(self):
        "Test getting the classname of the dialog"
        self.assertEquals(self.ctrl.Class(), "Button")

    def testWindowText(self):
        "Test getting the window Text of the dialog"
        self.assertEquals(
            HwndWrapper(self.dlg.Degrees.handle).WindowText(), u'Degrees')

    def testStyle(self):

        self.dlg.Style()

        self.assertEquals(
            self.ctrl.Style(), win32defines.WS_CHILD | win32defines.WS_VISIBLE
            | win32defines.BS_PUSHBUTTON | win32defines.BS_TEXT)

    def testExStyle(self):
        self.assertEquals(
            self.ctrl.ExStyle(), win32defines.WS_EX_NOPARENTNOTIFY
            | win32defines.WS_EX_LEFT | win32defines.WS_EX_LTRREADING
            | win32defines.WS_EX_RIGHTSCROLLBAR)
        """self.assertEquals(self.dlg.ExStyle(),
            win32defines.WS_EX_WINDOWEDGE |
            win32defines.WS_EX_LEFT |
            win32defines.WS_EX_LTRREADING |
            win32defines.WS_EX_RIGHTSCROLLBAR |
            win32defines.WS_EX_CONTROLPARENT |
            win32defines.WS_EX_APPWINDOW)"""

    def testControlID(self):
        self.assertEquals(self.ctrl.ControlID(), 83)
        self.dlg.ControlID()

    def testUserData(self):
        self.ctrl.UserData()
        self.dlg.UserData()

    def testContextHelpID(self):
        self.ctrl.ContextHelpID()
        self.dlg.ContextHelpID()

    def testIsVisible(self):
        self.assertEqual(self.ctrl.IsVisible(), True)
        self.assertEqual(self.dlg.IsVisible(), True)

    def testIsUnicode(self):
        self.assertEqual(self.ctrl.IsUnicode(), True)
        self.assertEqual(self.dlg.IsUnicode(), True)

    def testIsEnabled(self):
        self.assertEqual(self.ctrl.IsEnabled(), True)
        self.assertEqual(self.dlg.IsEnabled(), True)
        self.assertEqual(self.dlg.Button26.IsEnabled(), False)
        # Button26 = '%'

    def testCloseClick_bug(self):
        self.dlg.MenuSelect('Help->About Calculator')
        self.app.AboutCalculator.Wait("visible", 10)
        self.app.AboutCalculator.CloseButton.CloseClick()
        Timings.closeclick_dialog_close_wait = .7
        try:
            self.app.AboutCalculator.CloseClick()
        except TimeoutError:
            pass

        self.app.AboutCalculator.Close()

        #self.assertEquals(self.app.StatisticsBox.Exists(), False)

    def testCloseAltF4(self):
        self.dlg.MenuSelect('Help->About Calculator')
        AboutCalculator = self.app.Window_(title='About Calculator',
                                           active_only=True,
                                           class_name='#32770')
        AboutWrapper = AboutCalculator.Wait("enabled")
        AboutCalculator.CloseAltF4()
        AboutCalculator.WaitNot('visible')
        self.assertNotEqual(AboutWrapper.IsVisible(), True)

    def testRectangle(self):
        "Test getting the rectangle of the dialog"
        rect = self.dlg.Rectangle()
        self.assertNotEqual(rect.top, None)
        self.assertNotEqual(rect.left, None)
        self.assertNotEqual(rect.bottom, None)
        self.assertNotEqual(rect.right, None)

        if abs(rect.height() - 323) > 2:
            if rect.height() != 310:
                self.assertEqual(rect.height(), 323)
        if abs(rect.width() - 423) > 2:
            if rect.width() != 413:
                self.assertEqual(rect.width(), 423)

    def testClientRect(self):
        rect = self.dlg.Rectangle()
        cli = self.dlg.ClientRect()

        self.assertEqual(cli.left, 0)
        self.assertEqual(cli.top, 0)

        assert (cli.width() < rect.width())
        assert (cli.height() < rect.height())

    def testFont(self):
        self.assertNotEqual(self.dlg.Font(), self.ctrl.Font())

    def ProcessID(self):
        self.assertEqual(self.ctrl.ProcessID(), self.dlg.ProcessID)
        self.assertNotEqual(self.ctrl.ProcessID(), 0)

    def testHasStyle(self):
        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_CHILD), True)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_CHILD), False)

        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_SYSMENU), False)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_SYSMENU), True)

    def testHasExStyle(self):
        self.assertEqual(
            self.ctrl.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), True)
        self.assertEqual(
            self.dlg.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), False)

        self.assertEqual(self.ctrl.HasExStyle(win32defines.WS_EX_APPWINDOW),
                         False)
        #self.assertEqual(self.dlg.HasExStyle(win32defines.WS_EX_APPWINDOW), True)

    def testIsDialog(self):
        self.assertEqual(self.ctrl.IsDialog(), False)
        self.assertEqual(self.dlg.IsDialog(), True)

    def testMenuItems(self):
        self.assertEqual(self.ctrl.MenuItems(), [])
        self.assertEqual(self.dlg.MenuItems()[1]['Text'], '&Edit')

    def testParent(self):
        self.assertEqual(self.ctrl.Parent().Parent().Parent(), self.dlg.handle)

    def testTopLevelParent(self):
        self.assertEqual(self.ctrl.TopLevelParent(), self.dlg.handle)
        self.assertEqual(self.dlg.TopLevelParent(), self.dlg.handle)

    def testTexts(self):
        self.assertEqual(self.dlg.Texts(), ['Calculator'])
        self.assertEqual(
            HwndWrapper(self.dlg.Degrees.handle).Texts(), [u'Degrees'])
        self.assertEqual(
            self.dlg.ChildWindow(class_name='Static', ctrl_index=5).Texts(),
            ['0'])

    def testFoundIndex(self):
        "test an access to a control by found_index"

        # The edit box with '0' can be accessed directly by control_index = 5
        # or by a search combination: class_name='Static', found_index=3
        ctl = self.dlg.ChildWindow(ctrl_index=5)
        self.assertEqual(ctl.Texts(), [u'0'])
        ctl = self.dlg.ChildWindow(class_name='Static', found_index=3)
        self.assertEqual(ctl.Texts(), [u'0'])
        ctl.DrawOutline('blue')  # visualize

        # Test an out-of-range access
        # Notice:
        # A ChildWindow call only creates a WindowSpecification object.
        # The exception is raised later when we try to find the window.
        # For this reason we can't use an assertRaises statement here because
        # the exception is raised before actual call to DrawOutline
        ctl = self.dlg.ChildWindow(class_name='Static', found_index=3333)
        self.assertRaises(WindowNotFoundError, ctl.WrapperObject)

    def testSearchWithPredicateFunc(self):
        "test an access to a control by filtering with a predicate function"

        def is_radians(h):
            res = False
            hwwrp = HwndWrapper(h)
            if hwwrp.FriendlyClassName() == u'RadioButton':
                if hwwrp.Texts() == [u'Radians']:
                    res = True
            return res

        ctl = self.dlg.ChildWindow(predicate_func=is_radians)
        self.assertEqual(ctl.Texts(), [u'Radians'])
        ctl.DrawOutline('red')  # visualize

    def testClientRects(self):
        self.assertEqual(self.ctrl.ClientRects()[0], self.ctrl.ClientRect())
        self.assertEqual(self.dlg.ClientRects()[0], self.dlg.ClientRect())

    def testFonts(self):
        self.assertEqual(self.ctrl.Fonts()[0], self.ctrl.Font())
        self.assertEqual(self.dlg.Fonts()[0], self.dlg.Font())

    def testChildren(self):
        self.assertEqual(self.ctrl.Children(), [])
        self.assertNotEqual(self.dlg.Children(), [])

    def testIsChild(self):
        self.assertEqual(self.ctrl.IsChild(self.dlg.WrapperObject()), True)
        self.assertEqual(self.dlg.IsChild(self.ctrl), False)

    def testSendMessage(self):
        vk = self.dlg.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Degrees.SendMessage(win32defines.WM_GETDLGCODE)
        # The expected return code is: "Button" = 0x2000 and "Radio" = 0x40
        expected = 0x2000 + 0x40
        self.assertEqual(expected, code)

    def testSendMessageTimeout(self):
        default_timeout = Timings.sendmessagetimeout_timeout
        Timings.sendmessagetimeout_timeout = 0.1
        vk = self.dlg.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Degrees.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        # The expected return code is: "Button" = 0x2000 and "Radio" = 0x40
        expected = 0x2000 + 0x40
        Timings.sendmessagetimeout_timeout = default_timeout
        self.assertEqual(expected, code)

    def testPostMessage(self):
        self.assertNotEquals(0, self.dlg.PostMessage(win32defines.WM_PAINT))
        self.assertNotEquals(
            0, self.dlg.Degrees.PostMessage(win32defines.WM_PAINT))

#    def testNotifyMenuSelect(self):
#        "Call NotifyMenuSelect to ensure it does not raise"
#        self.ctrl.NotifyMenuSelect(1234)
#        self.dlg.NotifyMenuSelect(1234)

    def testNotifyParent(self):
        "Call NotifyParent to ensure it does not raise"
        self.ctrl.NotifyParent(1234)
        #self.dlg.NotifyParent(1234)

    def testGetProperties(self):
        "Test getting the properties for the HwndWrapped control"
        props = self.dlg.GetProperties()

        self.assertEquals(self.dlg.FriendlyClassName(),
                          props['FriendlyClassName'])

        self.assertEquals(self.dlg.Texts(), props['Texts'])

        for prop_name in props:
            self.assertEquals(getattr(self.dlg, prop_name)(), props[prop_name])

#    def testCaptureAsImage(self):
#        pass

    def testEquals(self):
        self.assertNotEqual(self.ctrl, self.dlg.handle)
        self.assertEqual(self.ctrl, self.ctrl.handle)
        self.assertEqual(self.ctrl, self.ctrl)


#    def testVerifyActionable(self):
#        self.assertRaises()

#    def testVerifyEnabled(self):
#        self.assertRaises()

#    def testVerifyVisible(self):
#        self.assertRaises()

    def testMoveWindow_same(self):
        "Test calling movewindow without any parameters"
        prevRect = self.dlg.Rectangle()
        self.dlg.MoveWindow()
        self.assertEquals(prevRect, self.dlg.Rectangle())

    def testMoveWindow(self):
        "Test moving the window"

        dlgClientRect = self.ctrl.Parent().Rectangle(
        )  # use the parent as a reference

        prev_rect = self.ctrl.Rectangle() - dlgClientRect

        new_rect = win32structures.RECT(prev_rect)
        new_rect.left -= 1
        new_rect.top -= 1
        new_rect.right += 2
        new_rect.bottom += 2

        self.ctrl.MoveWindow(
            new_rect.left,
            new_rect.top,
            new_rect.width(),
            new_rect.height(),
        )
        time.sleep(0.1)

        print('prev_rect = ', prev_rect)
        print('new_rect = ', new_rect)
        print('dlgClientRect = ', dlgClientRect)
        print('self.ctrl.Rectangle() = ', self.ctrl.Rectangle())
        self.assertEquals(self.ctrl.Rectangle(), new_rect + dlgClientRect)

        self.ctrl.MoveWindow(prev_rect)

        self.assertEquals(self.ctrl.Rectangle(), prev_rect + dlgClientRect)

    def testMaximize(self):
        self.dlg.Maximize()

        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMAXIMIZED)
        self.dlg.Restore()

    def testMinimize(self):
        self.dlg.Minimize()
        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMINIMIZED)
        self.dlg.Restore()

    def testRestore(self):
        self.dlg.Maximize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

        self.dlg.Minimize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

    def testGetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), None)
        self.assertEqual(self.dlg.GetFocus(), self.ctrl.GetFocus())

        self.dlg.Radians.SetFocus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Radians.handle)

    def testSetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), self.dlg.Radians.handle)
        self.dlg.Radians.SetFocus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Radians.handle)

    def testMenuSelect(self):
        "Test selecting a menu item"

        if not self.dlg.MenuItem("View -> Digit grouping").IsChecked():
            self.dlg.MenuSelect("View -> Digit grouping")

        self.dlg.TypeKeys("1234567")
        self.dlg.MenuSelect("Edit->Copy\tCtrl+C")
        self.dlg.Button8.Click(
        )  # 'Button8' is a class name of the 'CE' button
        self.assertEquals(
            self.dlg.ChildWindow(class_name='Static', ctrl_index=5).Texts()[0],
            "0")

        # get a pasted text
        self.dlg.MenuSelect("Edit->Paste\tCtrl+V")
        cur_str = self.dlg.ChildWindow(class_name='Static',
                                       ctrl_index=5).Texts()[0]

        # use a regular expression to match the typed string
        # because on machines with different locales
        # the digit groups can have different spacers. For example:
        # "1,234,567" or "1 234 567" and so on.
        exp_pattern = u"1.234.567"
        res = re.match(exp_pattern, cur_str)
        self.assertNotEqual(res, None)

    def testClose(self):
        "Test the Close() method of windows"
        # open about dialog
        self.dlg.MenuSelect('Help->About Calculator')

        # make sure it is open and visible
        self.app.AboutCalculator.Wait("visible", 20)
        self.assertTrue(
            self.app.Window_(title='About Calculator').IsVisible(), True)

        # close it
        self.app.Window_(title='About Calculator',
                         class_name='#32770').Close(1)

        # make sure that it is not visible
        try:
            #self.assertRaises(WindowNotFoundError, self.app.Window_(title='About Calculator', class_name='#32770').WrapperObject())
            # vvryabov: TimeoutError is caught by assertRaises, so the second raise is not caught correctly
            self.app.Window_(title='About Calculator',
                             class_name='#32770').WrapperObject()
        except WindowNotFoundError:
            print('WindowNotFoundError exception is raised as expected. OK.')

        # make sure the main calculator dialog is still open
        self.assertEquals(self.dlg.IsVisible(), True)
コード例 #21
0
class HwndWrapperMouseTests(unittest.TestCase):
    "Unit tests for mouse actions of the HwndWrapper 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()
        if is_x64_Python() or not is_x64_OS():
            self.app.start_(r"C:\Windows\System32\notepad.exe")
        else:
            self.app.start_(r"C:\Windows\SysWOW64\notepad.exe")

        # Get the old font
        self.app.UntitledNotepad.MenuSelect("Format->Font")

        self.old_font = self.app.Font.FontComboBox.SelectedIndex()
        self.old_font_style = self.app.Font.FontStyleCombo.SelectedIndex()

        # ensure we have the correct settings for this test
        self.app.Font.FontStyleCombo.Select(0)
        self.app.Font.FontComboBox.Select("Lucida Console")
        self.app.Font.OK.Click()

        self.dlg = self.app.Window_(title='Untitled - Notepad',
                                    class_name='Notepad')
        self.ctrl = HwndWrapper(self.dlg.Edit.handle)
        self.dlg.edit.SetEditText("Here is some text\r\n and some more")

    def tearDown(self):
        "Close the application after tests"

        # Set the old font again
        self.app.UntitledNotepad.MenuSelect("Format->Font")
        self.app.Font.FontComboBox.Select(self.old_font)
        self.app.Font.FontStyleCombo.Select(self.old_font_style)
        self.app.Font.OK.Click()
        self.app.Font.WaitNot('visible')

        # close the application
        try:
            self.dlg.Close(0.5)
            if self.app.Notepad["Do&n't Save"].Exists():
                self.app.Notepad["Do&n't Save"].Click()
                self.app.Notepad["Do&n't Save"].WaitNot('visible')
        except:  # timings.TimeoutError:
            pass
        finally:
            self.app.kill_()

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.WindowText(), "Untitled - Notepad")

    def testClick(self):
        self.ctrl.Click(coords=(50, 10))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (6, 6))

    def testClickInput(self):
        self.ctrl.ClickInput(coords=(50, 10))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (6, 6))

    def testDoubleClick(self):
        self.ctrl.DoubleClick(coords=(60, 30))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (24, 29))

    def testDoubleClickInput(self):
        self.ctrl.DoubleClickInput(coords=(60, 30))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (24, 29))

    def testMenuSelectNotepad_bug(self):
        "In notepad - MenuSelect Edit->Paste did not work"

        text = b'Here are some unicode characters \xef\xfc\r\n'
        app2 = Application.start("notepad")
        app2.UntitledNotepad.Edit.SetEditText(text)

        app2.UntitledNotepad.MenuSelect("Edit->Select All")
        app2.UntitledNotepad.MenuSelect("Edit->Copy")

        self.dlg.MenuSelect("Edit->Select All")
        self.dlg.MenuSelect("Edit->Paste")
        self.dlg.MenuSelect("Edit->Paste")
        self.dlg.MenuSelect("Edit->Paste")

        app2.UntitledNotepad.MenuSelect("File->Exit")
        app2.Window_(title='Notepad',
                     class_name='#32770')["Don't save"].Click()

        self.assertEquals(
            self.dlg.Edit.TextBlock().encode(locale.getpreferredencoding()),
            text * 3)
コード例 #22
0
class HwndWrapperMouseTests(unittest.TestCase):
    "Unit tests for mouse actions of the HwndWrapper 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().start(
            os.path.join(mfc_samples_folder, u"CmnCtrl3.exe"))

        self.dlg = self.app.Common_Controls_Sample
        self.dlg.TabControl.Select('CButton (Command Link)')
        self.ctrl = HwndWrapper(self.dlg.NoteEdit.handle)

    def tearDown(self):
        "Close the application after tests"

        # close the application
        try:
            self.dlg.Close(0.5)
        except Exception:  # TimeoutError:
            pass
        finally:
            self.app.kill_()

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.WindowText(), "Untitled - Notepad")

    def testClick(self):
        self.ctrl.Click(coords=(50, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (9, 9))

    def testClickInput(self):
        self.ctrl.ClickInput(coords=(50, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (9, 9))

    def testDoubleClick(self):
        self.ctrl.DoubleClick(coords=(50, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (8, 13))

    def testDoubleClickInput(self):
        self.ctrl.DoubleClickInput(coords=(80, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (13, 18))

#    def testRightClick(self):
#        pass

    def testRightClickInput(self):
        self.dlg.Edit.TypeKeys('{HOME}')
        self.dlg.Edit.Wait('enabled').RightClickInput()
        self.app.PopupMenu.Wait('ready').Menu().GetMenuPath(
            'Select All')[0].ClickInput()
        self.dlg.Edit.TypeKeys('{DEL}')
        self.assertEquals(self.dlg.Edit.TextBlock(), '')

    def testPressMoveRelease(self):
        self.dlg.NoteEdit.PressMouse(coords=(0, 5))
        self.dlg.NoteEdit.MoveMouse(coords=(65, 5))
        self.dlg.NoteEdit.ReleaseMouse(coords=(65, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (0, 12))

    def testDragMouse(self):
        self.dlg.NoteEdit.DragMouse(press_coords=(0, 5),
                                    release_coords=(65, 5))
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (0, 12))

        # continue selection with pressed Shift key
        self.dlg.NoteEdit.DragMouse(press_coords=(65, 5),
                                    release_coords=(90, 5),
                                    pressed='shift')
        self.assertEquals(self.dlg.Edit.SelectionIndices(), (0, 17))

    def testDebugMessage(self):
        self.dlg.NoteEdit.DebugMessage('Test message')
        # TODO: add screenshots comparison

    #def testDrawOutline(self):
    #    # TODO: add screenshots comparison
    #    self.dlg.DrawOutline()


#    def testSetWindowText(self):
#        pass
#
#    def testTypeKeys(self):
#        pass

    def testSetTransparency(self):
        self.dlg.SetTransparency()
        self.assertRaises(ValueError, self.dlg.SetTransparency, 256)
コード例 #23
0
class HwndWrapperTests(unittest.TestCase):
    "Unit tests for the TreeViewWrapper class"

    def setUp(self):
        """Start the application set some data and ensure the application
        is in the state we want it."""

        self.app = Application().start(os.path.join(mfc_samples_folder, u"CmnCtrl3.exe"))

        self.dlg = self.app.Common_Controls_Sample
        self.dlg.TabControl.Select('CButton (Command Link)')
        self.ctrl = HwndWrapper(self.dlg.Command_button_here.handle)

        #self.dlg = self.app.Calculator
        #self.dlg.MenuSelect('View->Scientific\tAlt+2')
        #self.ctrl = HwndWrapper(self.dlg.Button2.handle) # Backspace

    def tearDown(self):
        "Close the application after tests"
        # close the application
        #self.dlg.type_keys("%{F4}")
        #self.dlg.Close()
        self.app.kill_()

    def testInvalidHandle(self):
        "Test that an exception is raised with an invalid window handle"
        self.assertRaises(InvalidWindowHandle, HwndWrapper, -1)

    def testFriendlyClassName(self):
        "Test getting the friendly classname of the control"
        self.assertEquals(self.ctrl.friendly_class_name(), "Button")


    def testClass(self):
        "Test getting the classname of the control"
        self.assertEquals(self.ctrl.class_name(), "Button")

    def testWindowText(self):
        "Test getting the window Text of the control"
        self.assertEquals(
            HwndWrapper(self.dlg.Set.handle).window_text(), u'Set')

    def testStyle(self):

        self.dlg.Style()

        self.assertEquals(self.ctrl.Style(),
            win32defines.WS_CHILD |
            win32defines.WS_VISIBLE |
            win32defines.WS_TABSTOP |
            win32defines.BS_COMMANDLINK)


    def testExStyle(self):
        self.assertEquals(self.ctrl.ExStyle(),
            win32defines.WS_EX_NOPARENTNOTIFY |
            win32defines.WS_EX_LEFT |
            win32defines.WS_EX_LTRREADING |
            win32defines.WS_EX_RIGHTSCROLLBAR)

        """self.assertEquals(self.dlg.ExStyle(),
            win32defines.WS_EX_WINDOWEDGE |
            win32defines.WS_EX_LEFT |
            win32defines.WS_EX_LTRREADING |
            win32defines.WS_EX_RIGHTSCROLLBAR |
            win32defines.WS_EX_CONTROLPARENT |
            win32defines.WS_EX_APPWINDOW)"""

    def testControlID(self):
        self.assertEquals(self.ctrl.control_id(), 1037)
        self.dlg.control_id()

    def testUserData(self):
        self.ctrl.UserData()
        self.dlg.UserData()

    def testContextHelpID(self):
        self.ctrl.ContextHelpID()
        self.dlg.ContextHelpID()

    def testIsVisible(self):
        self.assertEqual(self.ctrl.is_visible(), True)
        self.assertEqual(self.dlg.is_visible(), True)

    def testIsUnicode(self):
        self.assertEqual(self.ctrl.IsUnicode(), True)
        self.assertEqual(self.dlg.IsUnicode(), True)

    def testIsEnabled(self):
        self.assertEqual(self.ctrl.is_enabled(), True)
        self.assertEqual(self.dlg.is_enabled(), True)
        #self.assertEqual(self.dlg.Note.is_enabled(), False); # Button26 = '%'

    def testRectangle(self):
        "Test getting the rectangle of the dialog"
        rect = self.dlg.rectangle()
        
        self.assertNotEqual(rect.top, None)
        self.assertNotEqual(rect.left, None)
        self.assertNotEqual(rect.bottom, None)
        self.assertNotEqual(rect.right, None)

        if abs(rect.height() - 423) > 5:
            self.assertEqual(rect.height(), 423)
        if abs(rect.width() - 506) > 5:
            self.assertEqual(rect.width(), 506)

    def testClientRect(self):
        rect = self.dlg.rectangle()
        cli = self.dlg.ClientRect()

        self.assertEqual(cli.left , 0)
        self.assertEqual(cli.top , 0)

        assert(cli.width() < rect.width())
        assert(cli.height() < rect.height())

    def testFont(self):
        self.assertNotEqual(self.dlg.Font(), self.ctrl.Font())

    def testProcessID(self):
        self.assertEqual(self.ctrl.process_id(), self.dlg.process_id())
        self.assertNotEqual(self.ctrl.process_id(), 0)

    def testHasStyle(self):
        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_CHILD), True)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_CHILD), False)

        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_SYSMENU), False)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_SYSMENU), True)

    def testHasExStyle(self):
        self.assertEqual(self.ctrl.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), True)
        self.assertEqual(self.dlg.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), False)

        self.assertEqual(self.ctrl.HasExStyle(win32defines.WS_EX_APPWINDOW), False)
        #self.assertEqual(self.dlg.HasExStyle(win32defines.WS_EX_APPWINDOW), True)

    def testIsDialog(self):
        self.assertEqual(self.ctrl.is_dialog(), False)
        self.assertEqual(self.dlg.is_dialog(), True)

    def testParent(self):
        self.assertEqual(self.ctrl.parent().parent(), self.dlg.handle)

    def testTopLevelParent(self):
        self.assertEqual(self.ctrl.top_level_parent(), self.dlg.handle)
        self.assertEqual(self.dlg.top_level_parent(), self.dlg.handle)

    def testTexts(self):
        self.assertEqual(self.dlg.texts(), ['Common Controls Sample'])
        self.assertEqual(HwndWrapper(self.dlg.Show.handle).texts(), [u'Show'])
        self.assertEqual(self.dlg.ChildWindow(class_name='Button', found_index=2).texts(), [u'Elevation Icon'])

    def testFoundIndex(self):
        "test an access to a control by found_index"

        ctl = self.dlg.ChildWindow(class_name='Button', found_index=3)
        self.assertEqual(ctl.texts(), [u'Show'])
        ctl.DrawOutline('blue')  # visualize
        
        # Test an out-of-range access
        # Notice:
        # A ChildWindow call only creates a WindowSpecification object.
        # The exception is raised later when we try to find the window.
        # For this reason we can't use an assertRaises statement here because
        # the exception is raised before actual call to DrawOutline
        ctl = self.dlg.ChildWindow(class_name='Button', found_index=3333)
        self.assertRaises(ElementNotFoundError, ctl.WrapperObject)

    def testSearchWithPredicateFunc(self):
        "test an access to a control by filtering with a predicate function"

        def is_checkbox(elem):
            res = False
            if elem.handle is None:
                return False
            hwwrp = HwndWrapper(elem.handle)
            if hwwrp.friendly_class_name() == u'CheckBox':
                if hwwrp.texts() == [u'Show']:
                    res = True
            return res

        ctl = self.dlg.ChildWindow(predicate_func=is_checkbox)
        self.assertEqual(ctl.texts(), [u'Show'])
        ctl.DrawOutline('red')  # visualize

    def testClientRects(self):
        self.assertEqual(self.ctrl.ClientRects()[0], self.ctrl.ClientRect())
        self.assertEqual(self.dlg.ClientRects()[0], self.dlg.ClientRect())

    def testFonts(self):
        self.assertEqual(self.ctrl.Fonts()[0], self.ctrl.Font())
        self.assertEqual(self.dlg.Fonts()[0], self.dlg.Font())

    def testChildren(self):
        self.assertEqual(self.ctrl.children(), [])
        self.assertNotEqual(self.dlg.children(), [])


    def testIsChild(self):
        self.assertEqual(self.ctrl.is_child(self.dlg.WrapperObject()), True)
        self.assertEqual(self.dlg.is_child(self.ctrl), False)


    def testSendMessage(self):
        vk = self.dlg.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Edit.SendMessage(win32defines.WM_GETDLGCODE)
        # The expected return code is: "Edit" ? # "Button" = 0x2000 and "Radio" = 0x40
        expected = 0x89 # 0x2000 + 0x40
        self.assertEqual(expected, code)


    def testSendMessageTimeout(self):
        default_timeout = Timings.sendmessagetimeout_timeout
        Timings.sendmessagetimeout_timeout = 0.1
        vk = self.dlg.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Show.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        # The expected return code is: "Button" = 0x2000 # and "Radio" = 0x40
        expected = 0x2000 #+ 0x40
        Timings.sendmessagetimeout_timeout = default_timeout
        self.assertEqual(expected, code)

    def testPostMessage(self):
        self.assertNotEquals(0, self.dlg.PostMessage(win32defines.WM_PAINT))
        self.assertNotEquals(0, self.dlg.Show.PostMessage(win32defines.WM_PAINT))

#    def testNotifyMenuSelect(self):
#        "Call NotifyMenuSelect to ensure it does not raise"
#        self.ctrl.NotifyMenuSelect(1234)
#        self.dlg.NotifyMenuSelect(1234)

    def testNotifyParent(self):
        "Call NotifyParent to ensure it does not raise"
        self.ctrl.NotifyParent(1234)
        #self.dlg.NotifyParent(1234)

    def testGetProperties(self):
        "Test getting the properties for the HwndWrapped control"
        props  = self.dlg.GetProperties()

        self.assertEquals(
            self.dlg.friendly_class_name(), props['friendly_class_name'])

        self.assertEquals(
            self.dlg.texts(), props['texts'])

        for prop_name in props:
            self.assertEquals(getattr(self.dlg, prop_name)(), props[prop_name])

#    def testCaptureAsImage(self):
#        pass

    # def testDrawOutline(self):
    #     """Test the outline was drawn."""
    #     # make sure window is ready
    #     self.dlg.Wait('active')
    #     self.dlg.Show.Click()
    #
    #     # not sure why, but this extra call makes the test stable
    #     self.dlg.draw_outline()
    #
    #     # outline control
    #     self.dlg.Show.draw_outline()
    #     img1 = self.dlg.Show.capture_as_image()
    #     self.assertEqual(img1.getpixel((0, 0)), (0, 255, 0))  # green
    #
    #     # outline window
    #     self.dlg.draw_outline(colour="red")
    #     img2 = self.dlg.capture_as_image()
    #     self.assertEqual(img2.getpixel((0, 0)), (255, 0, 0))  # red

    def testEquals(self):
        self.assertNotEqual(self.ctrl, self.dlg.handle)
        self.assertEqual(self.ctrl, self.ctrl.handle)
        self.assertEqual(self.ctrl, self.ctrl)


#    def testVerifyActionable(self):
#        self.assertRaises()

#    def testVerifyEnabled(self):
#        self.assertRaises()

#    def testVerifyVisible(self):
#        self.assertRaises()


    def testMoveWindow_same(self):
        "Test calling movewindow without any parameters"
        prevRect = self.dlg.rectangle()
        self.dlg.MoveWindow()
        self.assertEquals(prevRect, self.dlg.rectangle())

    def testMoveWindow(self):
        "Test moving the window"

        dlgClientRect = self.ctrl.parent().rectangle() # use the parent as a reference

        prev_rect = self.ctrl.rectangle() - dlgClientRect

        new_rect = win32structures.RECT(prev_rect)
        new_rect.left -= 1
        new_rect.top -= 1
        new_rect.right += 2
        new_rect.bottom += 2

        self.ctrl.MoveWindow(
            new_rect.left,
            new_rect.top,
            new_rect.width(),
            new_rect.height(),
            )
        time.sleep(0.1)

        print('prev_rect = ', prev_rect)
        print('new_rect = ', new_rect)
        print('dlgClientRect = ', dlgClientRect)
        print('self.ctrl.rectangle() = ', self.ctrl.rectangle())
        self.assertEquals(
            self.ctrl.rectangle(),
            new_rect + dlgClientRect)

        self.ctrl.MoveWindow(prev_rect)

        self.assertEquals(
            self.ctrl.rectangle(),
            prev_rect + dlgClientRect)


    def testMaximize(self):
        self.dlg.Maximize()

        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWMAXIMIZED)
        self.dlg.Restore()

    def testMinimize(self):
        self.dlg.Minimize()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWMINIMIZED)
        self.dlg.Restore()

    def testRestore(self):
        self.dlg.Maximize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

        self.dlg.Minimize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

    def testGetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), None)
        self.assertEqual(self.dlg.GetFocus(), self.ctrl.GetFocus())

        self.dlg.Set.set_focus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Set.handle)

    def testSetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), self.dlg.Set.handle)
        self.dlg.Set.set_focus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Set.handle)
コード例 #24
0
ファイル: SaveFromFirefox.py プロジェクト: yuyobit/pywinauto
    mozilla = app.window_(title_re=".*Mozilla Firefox")

else:
    app = application.Application().connect_(title_re=".*Mozilla Firefox")
    mozilla = app.window_(title_re=".*Mozilla Firefox")

# ie doesn't define it's menus as Menu's but actually as a toolbar!
print("No Menu's in FireFox:", mozilla.MenuItems())

# File -> Save As
mozilla.TypeKeys("%FA")
#ie.Toolbar3.PressButton("File")
app.SaveAs.Edit.SetEditText(outputfilename)

app.SaveAs.Save.CloseClick()

try:
    # if asked to overwrite say yes
    if app.SaveAs.Yes.Exists():
        app.SaveAs.Yes.CloseClick()
except WindowAmbiguousError as e:
    for w in e.windows:
        w = HwndWrapper(w)
        print(w.WindowText(), w.Class())

print("saved:", outputfilename)

# File close tab or close
#(Firefox makes it easy for us having the same shortcut for both!
mozilla.TypeKeys("%FC")
コード例 #25
0
 def testTexts(self):
     self.assertEqual(self.dlg.texts(), ['Common Controls Sample'])
     self.assertEqual(HwndWrapper(self.dlg.Show.handle).texts(), [u'Show'])
     self.assertEqual(
         self.dlg.ChildWindow(class_name='Button', found_index=2).texts(),
         [u'Elevation Icon'])
コード例 #26
0
class HwndWrapperTests(unittest.TestCase):
    "Unit tests for the TreeViewWrapper 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()
        self.app.start_("calc.exe")

        self.dlg = self.app.Calculator
        self.ctrl = HwndWrapper(self.dlg.Backspace.handle)

    def tearDown(self):
        "Close the application after tests"
        # close the application
        self.dlg.TypeKeys("%{F4}")

    def testInvalidHandle(self):
        "Test that an exception is raised with an invalid window handle"
        self.assertRaises(InvalidWindowHandle, HwndWrapper, -1)

    #def testText(self):
    #    "Test getting the window Text of the dialog"
    #    self.assertEquals(self.dlg.WindowText(), "Untitled - Notepad")

    def testFriendlyClassName(self):
        "Test getting the friendly classname of the dialog"
        self.assertEquals(self.ctrl.FriendlyClassName(), "Button")

    def testClass(self):
        "Test getting the classname of the dialog"
        self.assertEquals(self.ctrl.Class(), "Button")

    def testWindowText(self):
        "Test getting the window Text of the dialog"
        self.assertEquals(self.ctrl.WindowText(), "Backspace")

    def testStyle(self):

        self.dlg.Style()

        self.assertEquals(
            self.ctrl.Style(), win32defines.WS_CHILD | win32defines.WS_VISIBLE
            | win32defines.BS_PUSHBUTTON | win32defines.BS_TEXT)

    def testExStyle(self):
        self.assertEquals(
            self.ctrl.ExStyle(), win32defines.WS_EX_NOPARENTNOTIFY
            | win32defines.WS_EX_LEFT | win32defines.WS_EX_LTRREADING
            | win32defines.WS_EX_RIGHTSCROLLBAR)

        self.assertEquals(
            self.dlg.ExStyle(),
            win32defines.WS_EX_WINDOWEDGE | win32defines.WS_EX_LEFT
            | win32defines.WS_EX_LTRREADING | win32defines.WS_EX_RIGHTSCROLLBAR
            | win32defines.WS_EX_CONTROLPARENT | win32defines.WS_EX_APPWINDOW)

    def testControlID(self):
        self.assertEquals(self.ctrl.ControlID(), 83)
        self.dlg.ControlID()

    def testUserData(self):
        self.ctrl.UserData()
        self.dlg.UserData()

    def testContextHelpID(self):
        self.ctrl.ContextHelpID()
        self.dlg.ContextHelpID()

    def testIsVisible(self):
        self.assertEqual(self.ctrl.IsVisible(), True)
        self.assertEqual(self.dlg.IsVisible(), True)

    def testIsUnicode(self):
        self.assertEqual(self.ctrl.IsUnicode(), True)
        self.assertEqual(self.dlg.IsUnicode(), True)

    def testIsEnabled(self):
        self.assertEqual(self.ctrl.IsEnabled(), True)
        self.assertEqual(self.dlg.IsEnabled(), True)
        self.assertEqual(
            self.dlg.ChildWindow(title='Ave', enabled_only=False).IsEnabled(),
            False)

    def testCloseClick_bug(self):
        self.dlg.Sta.Click()
        Timings.closeclick_dialog_close_wait = .5
        try:
            self.app.StatisticsBox.CAD.CloseClick()
        except timings.TimeoutError:
            pass

        self.app.StatisticsBox.TypeKeys("%{F4}")

        #self.assertEquals(self.app.StatisticsBox.Exists(), False)

    def testRectangle(self):
        "Test getting the rectangle of the dialog"
        rect = self.dlg.Rectangle()
        self.assertNotEqual(rect.top, None)
        self.assertNotEqual(rect.left, None)
        self.assertNotEqual(rect.bottom, None)
        self.assertNotEqual(rect.right, None)

        self.assertEqual(rect.height(), 309)
        self.assertEqual(rect.width(), 480)

    def testClientRect(self):
        rect = self.dlg.Rectangle()
        cli = self.dlg.ClientRect()

        self.assertEqual(cli.left, 0)
        self.assertEqual(cli.top, 0)

        assert (cli.width() < rect.width())
        assert (cli.height() < rect.height())

    def testFont(self):
        self.assertNotEqual(self.dlg.Font(), self.ctrl.Font())

    def ProcessID(self):
        self.assertEqual(self.ctrl.ProcessID(), self.dlg.ProcessID)
        self.assertNotEqual(self.ctrl.ProcessID(), 0)

    def testHasStyle(self):
        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_CHILD), True)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_CHILD), False)

        self.assertEqual(self.ctrl.HasStyle(win32defines.WS_SYSMENU), False)
        self.assertEqual(self.dlg.HasStyle(win32defines.WS_SYSMENU), True)

    def testHasExStyle(self):
        self.assertEqual(
            self.ctrl.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), True)
        self.assertEqual(
            self.dlg.HasExStyle(win32defines.WS_EX_NOPARENTNOTIFY), False)

        self.assertEqual(self.ctrl.HasExStyle(win32defines.WS_EX_APPWINDOW),
                         False)
        self.assertEqual(self.dlg.HasExStyle(win32defines.WS_EX_APPWINDOW),
                         True)

    def testIsDialog(self):
        self.assertEqual(self.ctrl.IsDialog(), False)
        self.assertEqual(self.dlg.IsDialog(), True)

    def testMenuItems(self):
        self.assertEqual(self.ctrl.MenuItems(), [])
        self.assertEqual(self.dlg.MenuItems()[1]['Text'], '&View')

    def testParent(self):
        self.assertEqual(self.ctrl.Parent(), self.dlg.handle)

    def testTopLevelParent(self):
        self.assertEqual(self.ctrl.TopLevelParent(), self.dlg.handle)
        self.assertEqual(self.dlg.TopLevelParent(), self.dlg.handle)

    def testTexts(self):
        self.assertEqual(self.dlg.Texts(), [u'Calculator'])
        self.assertEqual(self.ctrl.Texts(), [u'Backspace'])
        self.assertEqual(self.dlg.Edit.Texts(), ['0. ', "0. "])

    def testClientRects(self):
        self.assertEqual(self.ctrl.ClientRects()[0], self.ctrl.ClientRect())
        self.assertEqual(self.dlg.ClientRects()[0], self.dlg.ClientRect())

    def testFonts(self):
        self.assertEqual(self.ctrl.Fonts()[0], self.ctrl.Font())
        self.assertEqual(self.dlg.Fonts()[0], self.dlg.Font())

    def testChildren(self):
        self.assertEqual(self.ctrl.Children(), [])
        self.assertNotEqual(self.dlg.Children(), [])

    def testIsChild(self):
        self.assertEqual(self.ctrl.IsChild(self.dlg.WrapperObject()), True)
        self.assertEqual(self.dlg.IsChild(self.ctrl), False)

    def testSendMessage(self):
        vk = self.dlg.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Inv.SendMessage(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

    def testSendMessageTimeout(self):

        vk = self.dlg.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

        code = self.dlg.Inv.SendMessageTimeout(win32defines.WM_GETDLGCODE)
        self.assertEqual(0, vk)

    def testPostMessage(self):
        self.assertNotEquals(0, self.dlg.PostMessage(win32defines.WM_PAINT))
        self.assertNotEquals(0,
                             self.dlg.Inv.PostMessage(win32defines.WM_PAINT))

#    def testNotifyMenuSelect(self):
#        "Call NotifyMenuSelect to ensure it does not raise"
#        self.ctrl.NotifyMenuSelect(1234)
#        self.dlg.NotifyMenuSelect(1234)

    def testNotifyParent(self):
        "Call NotifyParent to ensure it does not raise"
        self.ctrl.NotifyParent(1234)
        #self.dlg.NotifyParent(1234)

    def testGetProperties(self):
        "Test getting the properties for the HwndWrapped control"
        props = self.dlg.GetProperties()

        self.assertEquals(self.dlg.FriendlyClassName(),
                          props['FriendlyClassName'])

        self.assertEquals(self.dlg.Texts(), props['Texts'])

        for prop_name in props:
            self.assertEquals(getattr(self.dlg, prop_name)(), props[prop_name])

#    def testCaptureAsImage(self):
#        pass

    def testEquals(self):
        self.assertNotEqual(self.ctrl, self.dlg.handle)
        self.assertEqual(self.ctrl, self.ctrl.handle)
        self.assertEqual(self.ctrl, self.ctrl)


#    def testVerifyActionable(self):
#        self.assertRaises()

#    def testVerifyEnabled(self):
#        self.assertRaises()

#    def testVerifyVisible(self):
#        self.assertRaises()

    def testMoveWindow_same(self):
        "Test calling movewindow without any parameters"
        prevRect = self.dlg.Rectangle()
        self.dlg.MoveWindow()
        self.assertEquals(prevRect, self.dlg.Rectangle())

    def testMoveWindow(self):
        "Test moving the window"

        dlgClientRect = self.dlg.ClientAreaRect()

        prev_rect = self.ctrl.Rectangle() - dlgClientRect

        new_rect = win32structures.RECT(prev_rect)
        new_rect.left -= 1
        new_rect.top -= 1
        new_rect.right += 2
        new_rect.bottom += 2

        self.ctrl.MoveWindow(
            new_rect.left,
            new_rect.top,
            new_rect.width(),
            new_rect.height(),
        )

        self.assertEquals(self.ctrl.Rectangle(), new_rect + dlgClientRect)

        self.ctrl.MoveWindow(prev_rect)

        self.assertEquals(self.ctrl.Rectangle(), prev_rect + dlgClientRect)

    def testMaximize(self):
        self.dlg.Maximize()

        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMAXIMIZED)
        self.dlg.Restore()

    def testMinimize(self):
        self.dlg.Minimize()
        self.assertEquals(self.dlg.GetShowState(),
                          win32defines.SW_SHOWMINIMIZED)
        self.dlg.Restore()

    def testRestore(self):
        self.dlg.Maximize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

        self.dlg.Minimize()
        self.dlg.Restore()
        self.assertEquals(self.dlg.GetShowState(), win32defines.SW_SHOWNORMAL)

    def testGetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), None)
        self.assertEqual(self.dlg.GetFocus(), self.ctrl.GetFocus())

        self.dlg.Hyp.SetFocus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Hyp.handle)

    def testSetFocus(self):
        self.assertNotEqual(self.dlg.GetFocus(), self.dlg.Hyp.handle)
        self.dlg.Hyp.SetFocus()
        self.assertEqual(self.dlg.GetFocus(), self.dlg.Hyp.handle)

    def testMenuSelect(self):
        "Test selecting a menut item"

        if not self.dlg.MenuItem("View -> Digit grouping").IsChecked():
            self.dlg.MenuSelect("View -> Digit grouping")

        self.dlg.TypeKeys("1234567")
        self.dlg.MenuSelect("Edit->Copy")
        self.dlg.CE.Click()
        self.assertEquals(self.dlg.Edit.Texts()[1], "0. ")
        self.dlg.MenuSelect("Edit->Paste")
        self.assertEquals(self.dlg.Edit.Texts()[1], "1,234,567. ")

    def testClose(self):
        "Test the Close() method of windows"
        # open the statistics dialog
        try:
            self.dlg.Sta.CloseClick()
        except timings.TimeoutError:
            pass
        # make sure it is open and visible
        self.assertTrue(self.app.StatisticsBox.IsVisible(), True)

        # close it
        self.app.StatisticsBox.Close()

        # make sure that it is not visible
        self.assertRaises(AttributeError, self.app.StatisticsBox)

        # make sure the main calculator dialog is still open
        self.assertEquals(self.dlg.IsVisible(), True)
コード例 #27
0
 def testWindowText(self):
     "Test getting the window Text of the dialog"
     self.assertEquals(
         HwndWrapper(self.dlg.Degrees.handle).WindowText(), u'Degrees')
コード例 #28
0
 def testWindowText(self):
     "Test getting the window Text of the control"
     self.assertEquals(
         HwndWrapper(self.dlg.Set.handle).window_text(), u'Set')