def main(): main = threading.currentThread() auto.Logger.WriteLine('This is running in main thread. {} {}'.format(main.ident, main.name), auto.ConsoleColor.Cyan) time.sleep(2) auto.GetConsoleWindow().CaptureToImage('console_mainthread.png') root = auto.GetRootControl() auto.EnumAndLogControl(root, 1) th = threading.Thread(target=threadFunc, args=(root, )) th.start() th.join() auto.Logger.WriteLine('\nMain thread exits. {} {}'.format(main.ident, main.name), auto.ConsoleColor.Cyan)
def threadFunc(root): """ If you want to use functionalities related to Controls and Patterns in a new thread. You must call InitializeUIAutomationInCurrentThread first in the thread and call UninitializeUIAutomationInCurrentThread when the thread exits. But you can't use use a Control or a Pattern created in a different thread. So you can't create a Control or a Pattern in main thread and then pass it to a new thread and use it. """ #print(root)# you cannot use root because it is root control created in main thread th = threading.currentThread() auto.Logger.WriteLine('\nThis is running in a new thread. {} {}'.format(th.ident, th.name), auto.ConsoleColor.Cyan) time.sleep(2) auto.InitializeUIAutomationInCurrentThread() auto.GetConsoleWindow().CaptureToImage('console_newthread.png') newRoot = auto.GetRootControl() #ok, root control created in new thread auto.EnumAndLogControl(newRoot, 1) auto.UninitializeUIAutomationInCurrentThread() auto.Logger.WriteLine('\nThread exits. {} {}'.format(th.ident, th.name), auto.ConsoleColor.Cyan)
def threadFunc(uselessRoot): """ If you want to use UI Controls in a new thread, create an UIAutomationInitializerInThread object first. But you can't use use a Control or a Pattern created in a different thread. So you can't create a Control or a Pattern in main thread and then pass it to a new thread and use it. """ # print(uselessRoot)# you cannot use uselessRoot because it is a control created in a different thread th = threading.currentThread() auto.Logger.WriteLine( '\nThis is running in a new thread. {} {}'.format(th.ident, th.name), auto.ConsoleColor.Cyan) time.sleep(2) with auto.UIAutomationInitializerInThread(debug=True): auto.GetConsoleWindow().CaptureToImage('console_newthread.png') newRoot = auto.GetRootControl( ) # ok, root control created in current thread auto.EnumAndLogControl(newRoot, 1) auto.Logger.WriteLine('\nThread exits. {} {}'.format(th.ident, th.name), auto.ConsoleColor.Cyan)
def firefox(): ancestor = False showAllName = False showMore = False depth = 0xFFFFFFFF wait_time_in_seconds = 5 edgeWindow = automation.WindowControl(searchDepth=1, ClassName='ApplicationFrameWindow') control = edgeWindow if automation.WaitForExist(edgeWindow, wait_time_in_seconds): automation.Logger.WriteLine("There is an Edge window open :D", automation.ConsoleColor.Green) else: automation.Logger.WriteLine("There is no Edge window open :(", automation.ConsoleColor.Red) if ancestor: # control = ControlFromCursor() control = edgeWindow if control: automation.EnumAndLogControlAncestors(control, showAllName, showMore) else: automation.Logger.Write( 'IUIAutomation return null element under cursor\n', ConsoleColor.Yellow) else: if not control: control = edgeWindow controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] automation.EnumAndLogControl(control, depth, showAllName, showMore) automation.Logger.Log('Ends\n')
def main(): import getopt auto.Logger.Write('UIAutomation {} (Python {}.{}.{}, {} bit)\n'.format( auto.VERSION, sys.version_info.major, sys.version_info.minor, sys.version_info.micro, 64 if sys.maxsize > 0xFFFFFFFF else 32)) options, args = getopt.getopt(sys.argv[1:], 'hrfcand:t:', [ 'help', 'root', 'focus', 'cursor', 'ancestor', 'showAllName', 'depth=', 'time=' ]) root = False focus = False cursor = False ancestor = False foreground = True showAllName = False depth = 0xFFFFFFFF seconds = 3 for (o, v) in options: if o in ('-h', '-help'): usage() exit(0) elif o in ('-r', '-root'): root = True foreground = False elif o in ('-f', '-focus'): focus = True foreground = False elif o in ('-c', '-cursor'): cursor = True foreground = False elif o in ('-a', '-ancestor'): ancestor = True foreground = False elif o in ('-n', '-showAllName'): showAllName = True elif o in ('-d', '-depth'): depth = int(v) elif o in ('-t', '-time'): seconds = int(v) if seconds > 0: auto.Logger.Write('please wait for {0} seconds\n\n'.format(seconds), writeToFile=False) time.sleep(seconds) auto.Logger.Log('Starts, Current Cursor Position: {}'.format( auto.GetCursorPos())) control = None if root: control = auto.GetRootControl() if focus: control = auto.GetFocusedControl() if cursor: control = auto.ControlFromCursor() if depth < 0: while depth < 0 and control: control = control.GetParentControl() depth += 1 depth = 0xFFFFFFFF if ancestor: control = auto.ControlFromCursor() if control: auto.EnumAndLogControlAncestors(control, showAllName) else: auto.Logger.Write( 'IUIAutomation returns null element under cursor\n', auto.ConsoleColor.Yellow) else: indent = 0 if not control: control = auto.GetFocusedControl() controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] if foreground: indent = 1 auto.LogControl(controlList[0], 0, showAllName) auto.EnumAndLogControl(control, depth, showAllName, startDepth=indent) auto.Logger.Log('Ends\n')