def open_url(self, url, page_cls=None): import win32process, win32con while is_port_occupied(self._port): # 如果端口被占用,则查找下一个可用端口 self._port += 1 if not self._port in self._port_list: self._port_list.append(self._port) temp_dir = self._temp_dir % self._port exe_path = ChromeBrowser.get_browser_path() params = "--remote-debugging-port=%d --disable-session-crashed-bubble --disable-features=TranslateUI --disable-breakpad --no-first-run --new-window --disable-desktop-notifications --user-data-dir=%s" % ( self._port, temp_dir) cmd = ' '.join([exe_path, params, url]) logging.debug('chrome: %s' % cmd) _, _, pid, _ = win32process.CreateProcess(None, cmd, None, None, 0, 0, None, None, win32process.STARTUPINFO()) handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, pid) win32event.WaitForInputIdle(handle, 10000) self._pid = pid logging.info('chrome进程为%d' % pid) # 加上此句话查看chrome是否成功打开了 timeout = 10 time0 = time.time() while time.time() - time0 < timeout: win_list = self.get_chrome_window_list(pid) if len(win_list) > 0: break time.sleep(1) else: raise RuntimeError('find chrome browser window failed') assert (len(win_list) == 1) page_wnd = win_list[0] self._webview = ChromeWebView(page_wnd, url, self._pid, self._port) return self.get_page_cls(self._webview, page_cls)
def AppIdle(): "Return true when the application is ready to start" result = win32event.WaitForInputIdle(hProcess, int(timeout * 1000)) # wait completed successfully if result == 0: return True # the wait returned because it timed out if result == win32con.WAIT_TIMEOUT: return False return bool(self.windows_())
def Process(self, btn, idx, split_line): hwnd = self.Get_param(btn, 1) # get the window tid, pid = win32process.GetWindowThreadProcessId(hwnd) # find the pid hproc = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, pid) # find the process id res = win32con.WAIT_TIMEOUT # set the failure mode to timeout while res == win32con.WAIT_TIMEOUT: # while we're still timing out res = win32event.WaitForInputIdle( hproc, 20) # wait a little while for window to become idle if btn.Check_kill(): # check if we've been killed return False # and die
def open_url(self, url, page_cls=None): '''打开一个url,返回对应的webpage实例类 :params url: url :type url: str :params page_cls: page实例类 :type page_cls: class ''' ie_path = IEBrowser.get_path() # -e可以实现以新进程的方式打开ie pid = win32process.CreateProcess(None, '%s -e %s ' % (ie_path, url), None, None, 0, win32con.CREATE_NEW_CONSOLE, None, None, win32process.STARTUPINFO())[2] if not pid in self._pid_list: self._pid_list.append(pid) handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, pid) win32event.WaitForInputIdle(handle, 10000) return self._get_page_cls(pid, page_cls)
def setUp(self): self.processHandle = None shutil.copyfile(os.path.join(self.basepath, 'test.tsk'), os.path.join(self.basepath, 'testfile.tsk')) path = os.path.join('..', 'build') for name in os.listdir(path): dirname = os.path.join(path, name) filename = os.path.join(dirname, 'taskcoach.exe') if os.path.isdir(dirname) and os.path.exists(filename): break else: self.fail('Could not find Task Coach executable.') self.logfilename = filename + '.log' cmd = [filename, '-i', 'test.ini', '-l', 'en'] + self.args sinfo = win32process.STARTUPINFO() sinfo.dwFlags = 0 hProcess = win32process.CreateProcess(None, ' '.join(cmd), None, None, False, 0, None, os.getcwd(), sinfo)[0] self.processHandle = hProcess if win32event.WaitForInputIdle(hProcess, 120000) == win32event.WAIT_TIMEOUT: self.fail('Could not launch Task Coach.') window = self.findWindow(r'^Errors occurred', tries=3) if window is not None: window.close() self.fail("Errors occurred. The log content was:\n" + file(self.logfilename, 'rb').read()) window = self.findWindow(r'^Tip of the day$') if window is None: self.fail("Tip window didn't appear") window.close() window = self.findWindow(r'^New version of Task Coach available$', tries=3) if window: window.close()
def enable_remote_debug2(self): '''由于使用weinre不稳定,增加另一种调试方法 ''' import os, sys import win32process, win32event from tuia._autoweb.webkit import chrome html_path = os.path.join(os.environ['temp'], 'page.html') f = open(html_path, 'w') html = self.execScript('document.documentElement.outerHTML;') f.write(html) f.close() chrome_path = chrome.WebPage._get_browser_path() cmdline = [chrome_path, html_path] try: processinfo = win32process.CreateProcess( None, ' '.join(cmdline), None, None, 0, 0, None, None, win32process.STARTUPINFO()) win32event.WaitForInputIdle(processinfo[0], 10000) except: print >> sys.stderr, '请在Chrome浏览器中打开:%s' % html_path
def enable_remote_debug(self): '''启用远程调试 ''' import os, sys import win32process, win32event from tuia._autoweb.webkit import chrome weinre_domain = 'weinre.qq.com' # gz.zat.cc:8081 已挂 computer = os.popen("hostname").read().strip() debug_id = '%s_%s' % (computer, self.__class__.__name__) self._driver.enable_remote_debug(debug_id, weinre_domain) chrome_path = chrome.WebPage._get_browser_path() url = 'http://%s/client/#%s' % (weinre_domain, debug_id) cmdline = ['"%s"' % chrome_path, url] try: processinfo = win32process.CreateProcess( None, ' '.join(cmdline), None, None, 0, 0, None, None, win32process.STARTUPINFO()) win32event.WaitForInputIdle(processinfo[0], 10000) except: print >> sys.stderr, '请在Chrome浏览器中打开:%s' % url
def open_url(url): '''打开URL ''' import time chrome_path = Chrome._get_browser_path() if sys.platform == 'win32': import win32process, win32event, win32gui Log.i('Chrome', chrome_path) cmdline = [ '"%s"' % chrome_path, url, "--user-data-dir=remote-profile_1113" ] # , '--disk-cache-dir="%sqt4a_cache"' % chrome_path[:-10] processinfo = win32process.CreateProcess( None, ' '.join(cmdline), None, None, 0, 0, None, None, win32process.STARTUPINFO()) win32event.WaitForInputIdle(processinfo[0], 10000) time.sleep(2) return Chrome(win32gui.GetForegroundWindow()) elif sys.platform == 'darwin': import subprocess subprocess.Popen([chrome_path, url])