def tearDown(self): for hndle in self.tempFileHndles: if not win32gui.IsWindow(hndle): print("???window does not exist: %s"% hndle) continue if hndle == self.thisHndle: # print('window hndle %s may not match "thisHndle": %s'% (hndle, self.thisHndle)) continue # print('close window with hndle: %s'% hndle) natlinkutilsqh.SetForegroundWindow(hndle) curHndle = natlinkutilsqh.GetForegroundWindow() if hndle == curHndle: if hndle in self.killActions: # natlinkutilsqh.SetForegroundWindow(self.thisHndle) # place to break in debug mode # natlinkutilsqh.SetForegroundWindow(curHndle) action(self.killActions[hndle], modInfo=self.modInfos[hndle]) else: natlinkutils.playString("{alt+f4}") natlinkutilsqh.SetForegroundWindow(self.thisHndle) self.disconnect() # disabled, natConnect notClosedHndles = [] for hndle in self.tempFileHndles: if hndle and win32gui.IsWindow(hndle): notClosedHndles.append(hndle) if self.frescHndle: if self.frescHndle in notClosedHndles: notClosedHndles.remove(self.frescHndle) else: raise TestError('Fresobaldi should not be closed after the test') if notClosedHndles: raise TestError('Hndles not close, but should: %s'% notClosedHndles)
def closeWindow(self, hndle): """close the test files """ curmod = natlinkutilsqh.getCurrentModuleSafe() # try a few times if it fails first if curmod[2] != hndle: natlinkutilsqh.SetForegroundWindow(hndle, debug=True) curmod = natlinkutilsqh.getCurrentModuleSafe() # try a few times if it fails first if curmod[2] == hndle: actions.do_KW()
def tearDown(self): print('----------- tearDown') natlinkutilsqh.SetForegroundWindow(self.thisHndle) # close if they were opened in the start of a test: for hndle in self.allWindows: print('closing: %s (allWindows: %s)'% (hndle, self.allWindows[hndle])) if hndle: self.closeWindow(hndle) else: print('how can this be? hndle %s of window: %s'% (hndle, self.allWindows[hndle])) self.disconnect() pass
def tttestSwitchingWindows(self): """test the opening of more explorer windows, word windows and notepad windows switching back and forth, using natlinkutilsqh.SetForegroundWindow!!! nCycles can be adapted to make test quicker or slower """ ## open the txtFiles (are closed in tearDown) print '------------ testSwitchingWindows----------------------------------' for explDir in explDirectories: hndle = self.openTestFile(explDir) if hndle: self.allWindows[hndle] = explDir else: raise IOError('could not open test directory %s' % explDir) for txtFile in txtFiles: hndle = self.openTestFile( os.path.join(thisDir, 'test_clipboardfiles', txtFile)) if hndle: self.allWindows[hndle] = txtFile else: print 'could not open %s' % txtFile for docxFile in docxFiles: hndle = self.openTestFile(os.path.join(thisDir, 'test_clipboardfiles', docxFile), waitingTime=0.5) if hndle: self.allWindows[hndle] = docxFile else: print 'could not open %s' % docxFile allKeys = sorted(self.allWindows.keys()) print 'go with allKeys: %s' % allKeys nCycles = 20 for i in range(nCycles): for hndle in allKeys: target = self.allWindows[hndle] print 'try to get %s (%s)' % (hndle, target) result = natlinkutilsqh.SetForegroundWindow(hndle, debug=True) if not result: print 'failed to set foreground window %s' % hndle continue curMod = natlinkutilsqh.getCurrentModuleSafe() self.assert_equal( hndle, curMod[2], "hndle not as expected after SetForegroundWindow %s (got: %s) (target is: %s)" % (hndle, curMod[2], target)) time.sleep(1) pass
def setupTextFiles(self): """make some text files for clipboard testing """ textFile0 = "testempty.txt" textPath0 = os.path.join(testFilesDir, textFile0) open(textPath0, 'w') result = actions.AutoHotkeyBringUp(app=None, filepath=textPath0) pPath, wTitle, hndle = result natlinkutilsqh.SetForegroundWindow(self.thisHndle) print('testempty (text0Hndle): %s'% hndle) self.tempFileHndles.append(hndle) self.text0Hndle = hndle textFile1 = "testsmall.txt" textPath1 = os.path.join(testFilesDir, textFile1) self.text1Txt = "small abacadabra\n"*2 open(textPath1, 'w').write(self.text1Txt) result = actions.AutoHotkeyBringUp(app=None, filepath=textPath1) pPath, wTitle, hndle = result self.tempFileHndles.append(hndle) natlinkutilsqh.SetForegroundWindow(self.thisHndle) print('testsmall (text1Hndle): %s'% hndle) self.text1Hndle = hndle self.tempFileHndles.append(hndle) textFile2 = "testlarge.txt" textPath2 = os.path.join(testFilesDir, textFile2) self.text2Txt = "large abacadabra\n"*1000 open(textPath2, 'w').write(self.text2Txt) result = actions.AutoHotkeyBringUp(app=None, filepath=textPath2) pPath, wTitle, hndle = result self.tempFileHndles.append(hndle) natlinkutilsqh.SetForegroundWindow(self.thisHndle) print('testlarge (text2Hndle): %s'% hndle) self.text2Hndle = hndle self.tempFileHndles.append(hndle)
def testCopyClipboardWord(self): """test the copying of the clipboard word documents """ ## open the txtFiles (are closed in tearDown) for docxFile in docxFiles: hndle = self.openTestFile(os.path.join(thisDir, 'test_clipboardfiles', docxFile), waitingTime=0.5) if hndle: self.allWindows[hndle] = docxFile else: print 'could not open test file %s' % docxFile cb = natlinkclipboard.Clipboard(save_clear=True) ## empty file: expTextPrev = "" nCycles = 2 # make it 10 or 50 to do a longer test for i in range(nCycles): for hndle, docxFile in self.allWindows.iteritems(): print 'trying %s (%s) (cycle %s)' % (hndle, docxFile, i + 1) natlinkutilsqh.SetForegroundWindow(hndle) time.sleep( 0.2 ) # word needs a little time before keystrokes are accepted... natlink.playString( "{ctrl+home}{shift+end}{shift+down 3}{ctrl+c}") # cb.copy_from_system(waiting_interval=0.005) # natlinkutilsqh.SetForegroundWindow(self.thisHndle) gotall = cb.get_text(waiting_interval=0.05) lengotall = len(gotall) got = gotall expText = u"A small word document.\n\nWith only a few lines\n\n" if not got: print 'no text, file: %s' % docxFile self.assert_equal( expText, got, "testing docx file %s, result not as expected" % docxFile) # also test the class method (direct call) got = natlinkclipboard.Clipboard.get_system_text() self.assert_equal( expText, got, "testing docx file %s, with class method, result not as expected" % docxFile) pass
def testEmpty(self): """only testing the setup and teardown implicitly testing actions (via autohotkey, AHK) like starting a process and switching to a window handle (this presented a lot of trouble, getting the correct files open with the correct window handles) Now with AHK on, this runs more smooth than ever. With or without Dragon running. """ print("doing an empty test, just to test setUp and tearDown") self.setupTextFiles() # self.setupDocxFile() # self.setupWindows() # self.setupThunderbirdNewWindow() # self.setupFrescobaldiNewPane() natlinkutilsqh.SetForegroundWindow(self.thisHndle)
def setupDocxFile(self): """open Word file and do the testing on it... """ docxFile2 = "natlink.docx" docxPath2 = os.path.normpath( os.path.join(testFilesDir, docxFile2)) if not os.path.isfile(docxPath2): raise IOError('file does not exist: %s'% docxPath2) result = actions.AutoHotkeyBringUp(app=None, filepath=docxPath2) if result: pPath, wTitle, hndle = result else: raise TestError("AutoHotkeyBringUp of Word gives no result: %s"% result) self.tempFileHndles.append(hndle) natlinkutilsqh.SetForegroundWindow(self.thisHndle) print('testempty.docx (docx2Hndle): %s'% result) self.tempFileHndles.append(result) self.docx2Hndle = result
def setupWindows(self): """make several windows to which can be switched, and which can be copied from TODOQH: make bringup of a folder and copy paste of the folder info work... (if somebody wants this) """ return self.thisHndle = natlinkutilsqh.GetForegroundWindow() dirWindows = "C:\\windows" result = actions.AutoHotkeyBringUp(app="explore", filepath=dirWindows) if not result: print('no result for %s'% dirWindows) return pPath, wTitle, hndle = result self.tempFileHndles.append(hndle) self.expl0Hndle = hndle natlinkutilsqh.SetForegroundWindow(self.thisHndle)
def tttestCopyClipboardCautious(self): """test the copying of the clipboard, with waiting times """ ## open the txtFiles (are closed in tearDown) for txtFile in txtFiles: hndle = self.openTestFile( os.path.join(thisDir, 'test_clipboardfiles', txtFile)) if hndle: self.allWindows[hndle] = txtFile else: print 'could not open %s' % txtFile cb = natlinkclipboard.Clipboard(save_clear=True) ## empty file: expTextPrev = "" for hndle, txtFile in self.allWindows.iteritems(): filePath = os.path.join(thisDir, 'test_clipboardfiles', txtFile) encoding, bom, expText = readwritefile.readAnything(filePath) if txtFile == "emptytest.txt": # now the clipboard remains unchanged... expText = expTextPrev else: # now save for empty file: expTextPrev = expText natlinkutilsqh.SetForegroundWindow(hndle) time.sleep(0.5) if txtFile == "emptytest.txt": time.sleep(3) natlink.playString("{ctrl+a}{ctrl+c}") # cb.copy_from_system(waiting_interval=0.05) # natlinkutilsqh.SetForegroundWindow(self.thisHndle) # print 'cb text: %s'% cb got = cb.get_text(waiting_interval=0.05) if txtFile == "emptytest.txt": self.assert_equal( expTextPrev, got, "testing empty txt file %s, result not as expected" % txtFile) else: self.assert_equal( expPrev, got, "testing txt file %s, result not as expected" % txtFile)
def tttestCopyClipboardQuick(self): """test the copying of the clipboard, without waiting times """ ## open the txtFiles (are closed in tearDown) for txtFile in txtFiles: hndle = self.openTestFile( os.path.join(thisDir, 'test_clipboardfiles', txtFile)) if hndle: self.allWindows[hndle] = txtFile else: print 'did not open testfile: %s' % txtFile cb = natlinkclipboard.Clipboard(save_clear=True) ## empty file: expTextPrev = "" for hndle, txtFile in self.allWindows.iteritems(): filePath = os.path.join(thisDir, 'test_clipboardfiles', txtFile) encoding, bom, expText = readwritefile.readAnything(filePath) if txtFile == "emptytest.txt": expText = expTextPrev else: expTextPrev = expText natlinkutilsqh.SetForegroundWindow(hndle) natlink.playString("{ctrl+a}{ctrl+c}") cb.copy_from_system() # natlinkutilsqh.SetForegroundWindow(self.thisHndle) # print 'cb text: %s'% cb got = cb.get_text() self.assert_equal( expText, got, "testing txt file %s, result not as expected" % txtFile) # also test the class method (direct call) got = natlinkclipboard.Clipboard.get_system_text() self.assert_equal( expText, got, "testing txt file %s, with class method, result not as expected" % txtFile) cb.restore()
def tttestSetForegroundWindow(self): """test switching the different windows, including this This is a side test for getting the testing system at work... and should work without problems with the AutoHotkey script... About 0.15 seconds to get a window in the foreground... """ # self.setupDocxFile() self.setupTextFiles() # self.text0Hndle, ..., self.text2Hndle self.setupWindows() self.setupFrescobaldiNewPane() # self.frescHndle self.setupThunderbirdNewWindow() # self.thundHndle thisHndle = natlinkutilsqh.GetForegroundWindow() print("thisHndle: %s"% thisHndle) unknownHndle = 5678659 hndles = [thisHndle, self.docx2Hndle, self.docx1Hndle, self.text0Hndle, self.text1Hndle, self.text2Hndle, self.frescHndle, self.thundHndle, unknownHndle] t0 = time.time() rounds = 10 for i in range(1, rounds+1): print('start round %s'% i) for h in hndles: if not h: continue result = natlinkutilsqh.SetForegroundWindow(h) if result: self.assert_not_equal(unknownHndle, h, "hndle should not be unknown (fake) hndle") else: self.assert_equal(unknownHndle, h, "hndle should be one of the valid hndles") time.sleep(0.1) t1 = time.time() deliberate = rounds*0.1 total = t1 - t0 nettime = t1 - t0 - deliberate nswitches = rounds*len(hndles) timeperswitch = nettime/nswitches print('nswitches: %s, nettime: %.4f, time per switch: %.4f'% ( nswitches, nettime, timeperswitch)) print('total: %s, deliberate: %s'% (total, deliberate))
def gotoTask(self, countOrApp): """go to the specified task, by number or application name, return proginfo, or None if task was not found """ self.lastTaskCount = None if type(countOrApp) in (bytes, str): countBack = self.getNumberFromSpoken( countOrApp, self.taskCounts) # returns a string or None elif isinstance(countOrApp, int): countBack = countOrApp hasMousePos = 0 appList = self.ini.get('application') ## print 'appList: %s'% appList if type(countOrApp) in (bytes, str) and self.hasCommon( countOrApp, 'back'): action('SSK {Alt+Tab}') return 1 elif countOrApp in self.namedtaskDict: hndle = self.namedtaskDict[countOrApp] result = natqh.SetForegroundWindow(hndle) if not result: print('switch to %s failed, delete name: %s' % (hndle, countOrApp)) del self.namedtaskDict[countOrApp] self.setList('namedtask', list(self.namedtaskDict.keys())) return else: return result elif countBack: t = time.time() self.lastTaskCount = countBack if self.doTasksWithWindowsKey: self.goto_task_winkey(countBack) else: action('TASK %s' % countBack) result = natqh.getProgInfo() #print 'after action task %s, time: %.2f'% (countBack, (time.time()-t)) return result elif countOrApp in appList: value = self.ini.getList('application', countOrApp) if len(value) == 1: app = value[0] self.app = app result = action("BRINGUP %s" % app) return result ## print 'after bringup: %s'% app elif len(value) == 2: #application is known, but click by number!! appName = value[0] countBack = value[1] self.lastTaskCount = countBack if self.doTasksWithWindowsKey: self.goto_task_winkey(countBack) else: action('TASK %s' % countBack) for i in range(30): # 40 x 0.1: 4 seconds... prog, title, topchild, classname, hndle = natqh.getProgInfo( ) if prog == appName: break className = natqh.getClassName() if className == "TaskListThumbnailWnd": return 1 # more items already available natqh.Wait() else: print('application not detected in foreground: %s' % appName) return else: print('_tasks, no valid entry for gotoTask: %s' % countOrApp) return result = natqh.getProgInfo()
def tttestCopyingFromExplorerWindows(self): """test the getting of explorer items dialog windows #32770 now also work. een The testing is not stable. But probably these are only testing issues. """ cb = natlinkclipboard.Clipboard() ## open the txtFiles (are closed in tearDown) for explDir in explDirectories: hndle = self.openTestFile(explDir) if hndle: self.allWindows[hndle] = explDir else: raise IOError('could not open test directory %s'% explDir) # for txtFile in txtFiles: # hndle = self.openTestFile(os.path.join(thisDir, 'test_clipboardfiles', txtFile)) # if hndle: # self.allWindows[hndle] = txtFile # else: # print 'did not open testfile: %s'% txtFile nCycles = 2 for i in range(nCycles): for hndle in list(self.allWindows.keys()): steps = (i % 4) + 2 expDir = self.allWindows[hndle] print('try to get %s (%s) (cycle %s)'% (hndle, expDir, i+1)) result = natlinkutilsqh.SetForegroundWindow(hndle, debug=True) if not result: print('could not get %s in the foreground after SetForegroundWindow %s'% (expDir, hndle)) continue curMod = natlinkutilsqh.getCurrentModuleSafe() print('doing %s lines of directory'% (steps+1,)) expDNr = self.allWindows[hndle] if hndle != curMod[2]: pass self.assert_equal(hndle, curMod[2], "hndle not as expected after SetForegroundWindow %s (%s)"% (hndle, expDir)) testChild = (curMod[0].find("notepad") > 0) if testChild: natlink.playString("{ctrl+o}") time.sleep(0.2) natlink.playString("{alt+d}%s{enter}"% testFilesDir) time.sleep(0.2) actions.do_RMP(1, 0.5, 0.5) # click on middle of window time.sleep(1) natlink.playString("{home}{shift+down %s}"% steps, 0x200) time.sleep(1) natlink.playString("{ctrl+c}", 0x200) time.sleep(1) if testChild: time.sleep(0.1) natlink.playString("{esc}") time.sleep(0.1) time.sleep(0.5) if testChild: pass got = cb.get_folderinfo(waiting_interval=0.1) exp = () lenexp = steps if testChild: lenexp = steps + 1 gotclassmethod = natlinkclipboard.Clipboard.Get_folderinfo() print('got: %s'% repr(got)) print('gotclassmethod: %s'% repr(gotclassmethod)) if got is None: pass self.assert_equal(got, gotclassmethod, "getting folderinfo should be the same from class method and instance method") self.assert_equal(type(exp), type(got), "result should be at least a tupl, testChild is: %s"% testChild) self.assert_equal(lenexp, len(got), "expect length of 2, but: %s, testChild is: %s"% (repr(got), testChild)) formats = natlinkclipboard.Clipboard.Get_clipboard_formats() self.assertTrue(type(formats) == list, "list of formats should be a list, not: %s"% repr(formats)) gotclassmethod = natlinkclipboard.Clipboard.Get_folderinfo() self.assert_equal(got, gotclassmethod, "getting folderinfo should be the same from class method and instance method") time.sleep(1) # cb.restore() pass
def tttestCopyClipboardSwitching(self): """test the copying of the clipboard with empty, small and larger .txt file. .docx does not run smooth """ print("testCopyClipboardSwitching") # self.setupDocxFile() # self.setupWindows() # self.setupThunderbirdNewWindow() self.setupFrescobaldiNewPane() self.setupTextFiles() cb = natlinkclipboard.Clipboard(save_clear=True, debug=2) # debug flag can be set or omitted (or set to 0) return ## longer test: for i in range(1, 10, 5): waitTime = 0.001/i print('---- round: %s, waitTime %.4f '% (i, waitTime)) ## first clear the clipboard: cb.clear_clipboard() got = cb.get_text(waitTime) print('after clear_clipboard, text: %s (cb: %s)'% (got, cb)) self.assert_equal("", got, "should have no text now, waitTime: %s (times 4)"% waitTime) print("now testempty.txt -----------------") # empty file: natlinkutilsqh.SetForegroundWindow(self.text0Hndle) natlinkutils.playString("{ctrl+a}{ctrl+c}") got = cb.get_text(waitTime) print('after select all copy of empty file, no change should have happened:') # print('got: %s, (cb: %s)'% (got, cb)) self.assert_equal("", got, "should have no text now, waitTime: %s"% waitTime) natlinkutilsqh.SetForegroundWindow(self.thisHndle) print("now testsmall.txt -----------------") natlinkutilsqh.SetForegroundWindow(self.text1Hndle) natlinkutils.playString("{ctrl+a}{ctrl+c}") got = cb.get_text(waitTime) print('after select all copy of testsmall.txt') # print('got: %s, (cb: %s)'% (got, cb)) exp = self.text1Txt self.assert_equal(exp, got, "testsmall should have two lines of text, waitTime: %s"% waitTime) # test large.txt print("now testlarge.txt -----------------") natlinkutilsqh.SetForegroundWindow(self.text2Hndle) natlinkutils.playString("{ctrl+a}{ctrl+c}") cb.get_text(waiting_interval=waitTime) got = cb.get_text() if got: lengot = len(got) else: print('testlarge, no result from get_text: %s'% cb) exp = len(self.text2Txt) self.assert_equal(exp, lengot, "should have long text now, waitTime: %s"% waitTime) # empty for the next round: # cb.set_text("") time.sleep(waitTime) natlinkutilsqh.SetForegroundWindow(self.thisHndle) # test natlink.docx print("now natlink.docx (%s)-----------------"% self.docx2Hndle) if not self.docx2Hndle: print("word document not available: %s"% (self.docx2Hndle)) continue natlinkutilsqh.SetForegroundWindow(self.docx2Hndle) natlinkutils.playString("{ctrl+a}{ctrl+c}") cb.get_text(waiting_interval=waitTime) got = cb.get_text() if got: lengot = len(got) else: print('testlarge, no result from get_text: %s'% cb) exp = len(self.text2Txt) self.assert_equal(exp, lengot, "should have long text now, waitTime: %s"% waitTime) # empty for the next round: # cb.set_text("") time.sleep(waitTime) del cb got_org_text = natlinkclipboard.Clipboard.get_system_text() self.assert_equal(self.org_text, got_org_text, "restored text from clipboard not as expected")