def ConnectWindow(self, selector): # 目前来说,如下处理,以后添加更多的参数后需修改代码逻辑 argunums = 0 if 'bundleid' in selector: argunums += 1 if 'appname' in selector: argunums += 1 if 'appname_re' in selector: argunums += 1 if argunums == 0: raise ValueError("Expect bundleid or appname, got none") elif argunums != 1: raise ValueError( "Too many arguments, only need bundleid or appname or appname_re" ) winlist = self.EnumWindows(selector) handleSetList = [] if 'windowtitle' in selector: handleSetList.append( self.ConnectWindowsByWindowTitle(selector, winlist)) if 'windowindex' in selector: handleSetList.append(set([selector['windowindex']])) if "windowtitle_re" in selector: handleSetList.append( self.ConnectWindowsByWindowTitleRe(selector, winlist)) while -1 in handleSetList: handleSetList.remove(-1) # 有些参数没有提供会返回-1.把所有的-1去掉 if len(handleSetList) == 0: # 三种方法都找不到窗口 raise InvalidSurfaceException( selector, "Can't find any applications by the given parameter") handleSet = reduce(operator.__and__, handleSetList) # 提供了多个参数来确定唯一一个窗口,所以要做交集,取得唯一匹配的窗口 if len(handleSet) == 0: raise InvalidSurfaceException( selector, "Can't find any applications by the given parameter") elif len(handleSet) != 1: raise NonuniqueSurfaceException(selector) else: hn = handleSet.pop() # 取得该窗口的索引 w = self.app.windows() if len(w) <= hn: raise IndexError( "Unable to find the specified window through the index, you may have closed the specified window during the run" ) self.root = self.app.windows()[hn] self.SetForeground() return True
def ConnectWindow(self, selector): # 目前来说,如下处理,以后添加更多的参数后需修改代码逻辑 argunums = 0 if 'handle' in selector: argunums += 1 if 'title' in selector: argunums += 1 if 'title_re' in selector: argunums += 1 if argunums == 0: raise ValueError("Expect handle or title, got none") elif argunums != 1: raise ValueError( "Too many arguments, only need handle or title or title_re") handleSetList = [] if 'title' in selector: handleSetList.append(self.ConnectWindowsByTitle(selector['title'])) if 'handle' in selector: handleSetList.append( self.ConnectWindowsByHandle(selector['handle'])) if "title_re" in selector: handleSetList.append( self.ConnectWindowsByTitleRe(selector['title_re'])) while -1 in handleSetList: handleSetList.remove(-1) # 有些参数没有提供会返回-1.把所有的-1去掉 if len(handleSetList) is 0: raise InvalidSurfaceException( selector, "Can't find any windows by the given parameter") handleSet = reduce(operator.__and__, handleSetList) # 提供了多个参数来确定唯一一个窗口,所以要做交集,取得唯一匹配的窗口 if len(handleSet) == 0: raise InvalidSurfaceException( selector, "Can't find any windows by the given parameter") elif len(handleSet) != 1: raise NonuniqueSurfaceException(selector) else: hn = handleSet.pop() # 取得那个唯一的窗口 self.root = UIAuto.ControlFromHandle(hn) if self.root is None: raise InvalidSurfaceException( selector, "Can't find any windows by the given parameter") self.SetForeground()
def __init__(self, root): self.RootControl = root self.RootHeight = self.RootControl.AXSize[1] self.RootWidth = self.RootControl.AXSize[0] self.RootLeft = self.RootControl.AXPosition[0] self.RootTop = self.RootControl.AXPosition[1] if self.RootWidth == 0 or self.RootHeight == 0: raise InvalidSurfaceException(self, "You may have minimized your window or the window is too small!")
def JudgeSize(self): self.SetForeground() # 先打开窗口再获取大小,否则最小化的窗口获取到的大小会为0 size = self.GetScreenSize() if size[0] == 0 or size[1] == 0: raise InvalidSurfaceException( self, "You may have minimized or closed your window or the window is too small!" )
def __init__(self, root): self.RootControl = root self.RootHeight = self.RootControl.BoundingRectangle[3] - self.RootControl.BoundingRectangle[1] self.RootWidth = self.RootControl.BoundingRectangle[2] - self.RootControl.BoundingRectangle[0] self.RootLeft = self.RootControl.BoundingRectangle[0] self.RootTop = self.RootControl.BoundingRectangle[1] if self.RootWidth == 0 or self.RootHeight == 0: raise InvalidSurfaceException(self, "You may have minimized or closed your window or the window is too small!")
def EnumWindows(self, selector): names = [] if 'bundleid' in selector: self.app = atomac.getAppRefByBundleId(selector['bundleid']) windows = self.app.windows() for i, w in enumerate(windows): names.append((w.AXTitle, i)) return names if 'appname' in selector: self.app = atomac.getAppRefByLocalizedName(selector['appname']) windows = self.app.windows() for i, w in enumerate(windows): names.append((w.AXTitle, i)) return names if 'appname_re' in selector: # 此方法由于MacOS API,问题较多 apps = atomac.NativeUIElement._getRunningApps() # 获取当前运行的所有应用程序 appset = set() # 应用程序集合 appnameset = set() # 应用程序标题集合 for t in apps: tempapp = atomac.getAppRefByPid(t.processIdentifier()) if str(tempapp) == str(atomac.AXClasses.NativeUIElement() ): # 通过trick判断应用程序是都否为空 continue attrs = tempapp.getAttributes() if 'AXTitle' in attrs: tit = tempapp.AXTitle if re.match(selector['appname_re'], tit): appset.add(tempapp) appnameset.add( tit) # 这里有Bug,可能会获取到进程的不同副本,所以要通过名字去判断是否唯一 if len(appnameset) is 0: raise InvalidSurfaceException( selector, "Can't find any applications by the given parameter") if len(appnameset) != 1: raise NonuniqueSurfaceException(selector) while len(names) is 0: # 有可能有多个副本,但只有一个真的应用程序有窗口,所以要枚举去找 if len(appset) is 0: return names self.app = appset.pop() windows = self.app.windows() # 获取当前应用程序的所有窗口 for i, w in enumerate(windows): names.append((w.AXTitle, i)) return names return names
def JudgeSize(self): size = self.GetScreenSize() if size[0] == 0 or size[1] == 0: raise InvalidSurfaceException(self, "You may have minimized your window or the window is too small!")