Exemplo n.º 1
0
    def ConnectWindow(self, selector, foreground=False):
        if foreground:
            time.sleep(1)
            self.root = UIAuto.GetForegroundControl()
            return True

        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)

        if len(handleSetList) == 0:
            return False

        handleSet = handleSetList[0]
        for s in handleSetList:
            handleSet = s & handleSet

        if len(handleSet) == 0:
            return False
        elif len(handleSet) != 1:
            raise NonuniqueSurfaceException(selector)
        else:
            hn = handleSet.pop()
            if hn:
                self.root = UIAuto.ControlFromHandle(hn)
                return True
            else:
                return False
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
Arquivo: OSXUI.py Projeto: adolli/Poco
    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