Example #1
0
 def testWaitCPUUsageLower(self):
     if is_x64_Python() != is_x64_OS():
         return None
     
     app = Application().Start(r'explorer.exe')
     WaitUntil(30, 0.5, lambda: len(findwindows.find_windows(active_only=True, class_name='CabinetWClass')) > 0)
     handle = findwindows.find_windows(active_only=True, class_name='CabinetWClass')[-1]
     window = WindowSpecification({'handle': handle, })
     explorer = Application().Connect(process=window.ProcessID())
     
     try:
         window.AddressBandRoot.ClickInput(double=True)
         window.Edit.SetEditText(r'Control Panel\Programs\Programs and Features')
         window.TypeKeys(r'{ENTER 2}', set_foreground=False)
         WaitUntil(30, 0.5, lambda: len(findwindows.find_windows(active_only=True, title='Programs and Features', class_name='CabinetWClass')) > 0)
         explorer.WaitCPUUsageLower(threshold=2.5, timeout=40, usage_interval=2)
         installed_programs = window.FolderView.Texts()[1:]
         programs_list = ','.join(installed_programs)
         if ('Microsoft' not in programs_list) and ('Python' not in programs_list):
             HwndWrapper.ImageGrab.grab().save(r'explorer_screenshot.jpg')
         HwndWrapper.ActionLogger().log('\ninstalled_programs:\n')
         for prog in installed_programs:
             HwndWrapper.ActionLogger().log(prog)
         self.assertEqual(('Microsoft' in programs_list) or ('Python' in programs_list), True)
     finally:
         window.Close(2.0)
Example #2
0
def _toggle_notification_area_icons(show_all=True, debug_img=None):
    """
    A helper function to change 'Show All Icons' settings.
    On a succesful execution the function returns an original
    state of 'Show All Icons' checkbox.

    The helper works only for an "English" version of Windows,
    on non-english versions of Windows the 'Notification Area Icons'
    window should be accessed with a localized title"
    """

    app = Application()
    starter = app.start(r'explorer.exe')
    class_name = 'CabinetWClass'

    def _cabinetwclass_exist():
        "Verify if at least one active 'CabinetWClass' window is created"
        l = findwindows.find_windows(active_only=True, class_name=class_name)
        return (len(l) > 0)

    WaitUntil(_ready_timeout, _retry_interval, _cabinetwclass_exist)
    handle = findwindows.find_windows(active_only=True,
                                      class_name=class_name)[-1]
    window = WindowSpecification({'handle': handle, })
    explorer = Application().Connect(process=window.ProcessID())
    cur_state = None

    try:
        # Go to "Control Panel -> Notification Area Icons"
        window.Wait("ready", timeout=_ready_timeout)
        window.AddressBandRoot.ClickInput()
        window.TypeKeys(
                    r'control /name Microsoft.NotificationAreaIcons',
                    with_spaces=True,
                    set_foreground=True)
        # Send 'ENTER' separately, this is to make sure 
        # the window focus hasn't accidentally been lost
        window.TypeKeys(
                    '{ENTER}',
                    with_spaces=True,
                    set_foreground=True)
        explorer.WaitCPUUsageLower(threshold=5, timeout=_ready_timeout)

        # Get the new opened applet
        notif_area = explorer.Window_(title="Notification Area Icons",
                                      class_name=class_name)
        notif_area.Wait("ready", timeout=_ready_timeout)
        cur_state = notif_area.CheckBox.GetCheckState()

        # toggle the checkbox if it differs and close the applet
        if bool(cur_state) != show_all:
            notif_area.CheckBox.ClickInput()
        notif_area.Ok.ClickInput()
        explorer.WaitCPUUsageLower(threshold=5, timeout=_ready_timeout)

    except Exception as e:
        if debug_img:
            from PIL import ImageGrab
            ImageGrab.grab().save("%s.jpg" % (debug_img), "JPEG")
        l = pywinauto.actionlogger.ActionLogger()
        l.log("RuntimeError in _toggle_notification_area_icons")
        raise e

    finally:
        # close the explorer window
        window.Close()

    return cur_state