def getEditControl(self): # get application, if not found, set ctrl to None self.getAppWindow() if not self.app: self.__class__.ctrl = None return currentSel = None if self.ctrl: currentSel = mess.getSelection(self.ctrl) if currentSel and currentSel != (0, 0): return ctrls = mess.findControls(self.app, wantedClass=self.editcontrol) if len(ctrls): editHndle = ctrls[0] #print 'editHndle set to: %s'% editHndle if len(ctrls) > 1: for hndle in ctrls: id = win32gui.GetDlgCtrlID(hndle) if id == 0xd6: editHndle = hndle break else: print('did not get valid id for aligen window') return else: raise ValueError( "could not find the editHndle of the control: %s in application: %s" % (self.editcontrol, self.apppath)) self.__class__.ctrl = editHndle
def getEditControl(self, prog, appHndle): """get the control (self.ctrl) of the text window in the foreground prog = name of application (to be defined in self.WindowsParameters) appHndle = hndle of the application """ # get application, if not found, set ctrl to None currentSel = None if self.ctrl: currentSel = mess.getSelection(self.ctrl) if currentSel and currentSel != (0, 0): return # get controls via messages functions, elaborate here if we need client-server trick: W = self.WindowsParameters[prog] # test case with fixed handle, set in windowparameters.py: if W["controlhandle"]: self.ctrl = W["controlhandle"] return wantedText, wantedClass, selectionFunction = W["edittext"], W[ "editcontrol"], W["selectionfunction"] print('wantedText: "%s", wantedClass: "%s", selectionFunction: %s' % (wantedText, wantedClass, selectionFunction)) ctrls = mess.findControls( appHndle, wantedText, wantedClass, selectionFunction) # pass the relevant windows parameters, as dict print('ctrls for "%s": %s' % (prog, ctrls)) # some special triggering in a difficult case: if ctrls: editHndle = ctrls[0] self.ctrl = editHndle else: self.ctrl = None
def getEditControl(self): # get application, if not found, set ctrl to None self.getAppWindow() if not self.app: self.__class__.ctrl = None return if tester == 'pythonwin': wTitle = win32gui.GetWindowText(self.app) filename = wTitle.split( '[')[-1][:-1] # remove [ and ], leaving only the filenam def selectionFunction(hndle, gotTitle, gotClass): """special for selecting the Afx class with the same title as the complete window title bar being the filename in question special for pythonwin and only for the FIRST search action for child windows. After the correct Afx window has been identified, the Scintilla child window is the correct one. """ if gotTitle == filename: #print 'got afx with title: %s'% gotTitle return True else: selectionFunction = None currentSel = None if self.ctrl: currentSel = mess.getSelection(self.ctrl) if currentSel and currentSel != (0, 0): return wC, wT = W["editcontrol"], W["edittext"] choiceControl = 0 if type(wT) in [type(None), bytes, int]: wT = [wT] if type(wC) in [type(None), bytes, int]: wC = [wC] ctrl = self.app for wtext, wclass in zip(wT, wC): ctrls = mess.findControls(ctrl, wantedText=wtext, wantedClass=wclass, selectionFunction=selectionFunction) if selectionFunction: selectionFunction = None # execute only for first findControls action pythonwin if tester == 'pythonwin': choiceControl = -1 if len(ctrls): ctrl = ctrls[choiceControl] #print 'editHndle set to: %s'% editHndle if len(ctrls) > 1: for hndle in ctrls: id = win32gui.GetDlgCtrlID(hndle) if id == 0xd6: ctrl = hndle break else: pprint.pprint(mess.dumpWindow(self.app)) raise ValueError( "could not find the editHndle of the control: %s in application: %s" % (self.editcontrol, self.apppath)) self.__class__.ctrl = ctrl self.__class__.classname = win32gui.GetClassName(ctrl)
def getSelection(self, handle=None): """get the selection of the edit control return a 2 tuple """ handle = handle or self.handle if not handle: return return mess.getSelection(handle)
def gotResults_testrule(self, words, fullResults): """special for testing the synchronisation between dictObj and real window one: just dictate a few words two: dictate on two lines (with empty in between) three: select the word "second" on the second line of text four: put a larger text in the window (so it scrolls) and select text. """ dct = self.dictobj dct.clearBoth() test = words[-1] if test == 'all': # get list of keywords in the inifile: tests = self.ini.get('global_tests') if tests: print('testwords: %s' % tests) for lastWord in tests: natlink.recognitionMimic( ["global", "dictation", "test", lastWord]) return else: print('found no "global dictations tests"') return if test == "one": # test just one line of text, to be found in dictobj and in actual window (through messagefunctions) natlink.recognitionMimic(["hello", "test", "one"]) time.sleep(0.5) beforeLength = dct.dictObj.getLength() beforeText = dct.dictObj.getText(0, beforeLength) messText = dct.getWindowText() dct.updateState() afterLength = dct.dictObj.getLength() afterText = dct.dictObj.getText(0, beforeLength) expected = [ "Hello test one " + dct.aftertext, "Hello test one" + dct.aftertext ] # 0: other window, 1: window has focus if not self.assert_equal_strings( expected, beforeText, "test %s; beforeText (dctobj) matches expected??" % test): return if not self.assert_equal_strings( beforeText, messText, "test %s; beforeText, messText equal test" % test): return if not self.assert_equal_strings( beforeText, afterText, "test %s; beforeText, afterText equal test" % test): return print('test %s OK--' % test) return 1 # OK elif test in ('two', 'three'): thirdWord = "second" if test == 'three': thirdWord = "third" # send a three line (two paragraph) text, and test the result: natlink.recognitionMimic( ["hello", "\\New-Paragraph", thirdWord, "test"]) time.sleep(0.5) beforeLength = dct.dictObj.getLength() beforeText = dct.dictObj.getText(0, beforeLength) messText = dct.getWindowText() dct.updateState() if test == ('two', 'three'): afterLength = dct.dictObj.getLength() afterText = dct.dictObj.getText(0, beforeLength) expected = [ "Hello\r\%s test %s" (thirdWord.capitalize(), dct.aftertext), "Hello\r\r%s test" % (thirdWord.capitalize(), dct.aftertext) ] # 0: other window, 1: window has focus if not self.assert_equal_strings( expected, beforeText, "test %s; beforeText (dctobj) matches expected??" % test): return if not self.assert_equal_strings( beforeText, messText, "test %s; beforeText, messText equal test" % test): return if not self.assert_equal_strings( beforeText, afterText, "test %s; beforeText, afterText equal test" % test): return if test == 'two': print('test %s OK--' % test) return 1 if test == 'three': # select a word on the third line (second paragraph) natlink.recognitionMimic(["select", "third"]) time.sleep(0.5) beforeSel = dct.dictObj.getTextSel() dctSelection = beforeText[beforeSel[0]:beforeSel[1]] messSel = mess.getSelection(dct.ctrl) messSelection = messText[messSel[0]:messSel[1]] expected = "Third " if not self.assert_equal_strings( expected, dctSelection, "test %s; selection from dctobj " % test): return if not self.assert_equal_strings( expected, messSelection, "test %s; selection from message window " % test): return print('test %s OK--' % test) return 1 # OK if test == "four": natlink.recognitionMimic( ["test", "four", "\\New-Paragraph", "there", "we", "go"]) natlink.recognitionMimic(["insert", "test", "fracture"]) natlink.recognitionMimic(["hello", "after", "fracture"]) natlink.recognitionMimic(["select", "hello", "after"]) time.sleep(0.5) beforeLength = dct.dictObj.getLength() beforeText = dct.dictObj.getText(0, beforeLength) beforeSel = dct.dictObj.getTextSel() dctSelection = beforeText[beforeSel[0]:beforeSel[1]] messText = dct.getWindowText() messSel = mess.getSelection(dct.ctrl) messSelection = messText[messSel[0]:messSel[1]] expected = "Hello after " if not self.assert_equal_strings( expected, dctSelection, "test %s; selection from dctobj " % test): return if not self.assert_equal_strings( expected, messSelection, "test %s; selection from message window " % test): return print('test %s OK--' % test) return 1 # OK if test == 'five': natlink.recognitionMimic(["test", ",\\comma", "five"]) natlink.recognitionMimic(["select", "test"]) natlink.recognitionMimic(["hello", "again", "five"]) self.wait() beforeLength = dct.dictObj.getLength() beforeText = dct.dictObj.getText(0, beforeLength) beforeSel = dct.dictObj.getTextSel() messText = dct.getWindowText() messSel = mess.getSelection(dct.ctrl) expected = "Hello again, five" if not self.assert_equal_strings( expected, beforeText, "test %s; text from dctobj " % test): return if not self.assert_equal_strings( expected, messText, "test %s; text from window " % test): return print('test %s OK--' % test) return 1 # OK if test == 'six': # try to make a field, and see if next dictate removes the field and caps the first char natlink.recognitionMimic(["insert", "impression"]) dct.sendKey(" ") dct.sendKey("[") dct.sendKey("x") dct.sendKey("]") natlink.recognitionMimic(["select", "x\\xray"]) self.wait() beforeLength = dct.dictObj.getLength() beforeText = dct.dictObj.getText(0, beforeLength) beforeSel = dct.dictObj.getTextSel() messText = dct.getWindowText() messSel = mess.getSelection(dct.ctrl) expected = '\r\rImpression: [x]\r' if not self.assert_equal_strings( expected, beforeText, "test %s; text from dctobj " % test): return if not self.assert_equal_strings( expected, messText, "test %s; text from window " % test): return dctSelection = beforeText[beforeSel[0]:beforeSel[1]] messSelection = messText[messSel[0]:messSel[1]] expected = "[x]" if not self.assert_equal_strings( expected, dctSelection, "test %s; selection from dctobj (selected field) " % test): return if not self.assert_equal_strings( expected, messSelection, "test %s; selection from message window (selected field) " % test): return print('test %s OK--' % test) return 1 # OK if test == 'seven': # try to make a field, and see if next dictate removes the field and caps the first char natlink.recognitionMimic(["insert", "findings"]) dct.sendKey(" ") dct.sendKey("[") dct.sendKey("x") dct.sendKey("]") natlink.recognitionMimic(["select", "x\\xray"]) self.wait() natlink.recognitionMimic(["insert", "test", "bone"]) beforeLength = dct.dictObj.getLength() beforeText = dct.dictObj.getText(0, beforeLength) beforeSel = dct.dictObj.getTextSel() messText = dct.getWindowText() messSel = mess.getSelection(dct.ctrl) expected = '\r\rImpression:\r\r1. Text in field \r' if not self.assert_equal_strings( expected, beforeText, "test %s; text from dctobj " % test): return if not self.assert_equal_strings( expected, messText, "test %s; text from window " % test): return print('test %s OK--' % test) return 1 # OK
def getSelection(self): """get the selection of the edit control return a 2 tuple """ return mess.getSelection(self.ctrl)