def test_02_ScriptVariable(self): """Testing creating and using variables through a Script object.""" value = 5 script = ahk.Script() # Define our good variable script.variable('test', int, value) # Test expected exceptions with self.assertRaises(AttributeError): script.function('_badname') script.function('function') script.function('test') # Test getting variable value self.assertEqual( script.test, value, msg="Variable value {0} doesn't match expected {1}!".format( script.test, value)) # Test setting variable value value = 10 script.test = 10 self.assertEqual( script.test, value, msg="Variable value {0} doesn't match expected {1}!".format( script.test, value)) # Test outside modification ahk.execute("test := test+5\n") self.assertEqual( script.test, value + 5, msg="Variable value {0} doesn't match expected {1}!".format( script.test, value + 5))
def test_01_ScriptFunction(self): """Testing creating and using a Function through a Script object.""" tests = ( (1, 1), ('2', 2), ('3', '4'), (10, 11), (100, 1000), ) script = ahk.Script() # Define our good function add = script.function('add', int, '(x, y)', 'return x + y') self.assertIsInstance(add, ahk.Function, msg="Non-function {0} returned!".format(add)) # Test expected exceptions with self.assertRaises(AttributeError): script.function('_badname') script.function('function') script.function('add') # Test with an assortment of values for x, y in tests: result = script.add(x, y) expect = int(x) + int(y) self.assertEqual(result, expect, msg="Unexpected result {0}, expected {1}!".format( result, expect)) with self.assertRaises(ValueError): # Error during type conversion script.add('abc', 'efg')
def __call__(self, imagePath, evtID, cType, evtF, evtNF): script = "\nSysGet, VirtualWidth, 78\nSysGet, VirtualWidth, 79" script += "\nImageSearch, imageX, imageY, 0, 0" script += ", %A_ScreenWidth%, %A_ScreenHeight%, " + imagePath if cType != 0: script += "\nif ErrorLevel = 0" if cType == 1: script += "\n Click %imageX%, %imageY%, left" if cType == 2: script += "\n Click %imageX%, %imageY%, 2" if cType == 3: script += "\n Click %imageX%, %imageY%, right" script = replaceEgVars(script) ImgScript = ahk.Script() ImgScript.variable("imageX", int) ImgScript.variable("imageY", int) ahk.execute(str(script)) errorLevel = ImgScript.ErrorLevel if evtID: evtID += "." if errorLevel == 0 and evtF == True: eg.TriggerEvent(evtID + "ImageFound", prefix="AHK.ImageSearch", payload=[ImgScript.imageX, ImgScript.imageY]) if errorLevel == 1 and evtNF == True: eg.TriggerEvent(evtID + "ImageNotFound", prefix="AHK.ImageSearch") if errorLevel == 2: eg.PrintError("There was an error. Try another image format.")
def test_04_waitPixel(self): """Testing waiting for a pixel.""" # Test waiting for a specific pixel value with mock.patch.object(ahk.Script, 'getPixel') as getpx: target = (10, 10, 10) threshold = 0.005 # 0.5% threshold interval = 0.01 # no wait since values are canned # Setup getpx with canned return values for our test getpx.side_effect = [ (50, 50, 50), # Far away (20, 20, 20), # Closer (13, 13, 13), # Just outside threshold (11, 11, 11), # Within threshold (10, 10, 10), # Extra equal value ] # Test our object scr = ahk.Script() scr.waitPixel(color=target, threshold=threshold, interval=interval) self.assertTrue(getpx.called, msg="Mock wasn't called?") self.assertEqual( getpx.call_count, 4, msg="getPixel mock called {0} times instead of 4.".format( getpx.call_count)) # Next test waiting for any different pixel value with mock.patch.object(ahk.Script, 'getPixel') as getpx: threshold = 0.008 # 0.8% threshold interval = 0.01 # no wait since values are canned # Setup getpx with canned return values for our test getpx.side_effect = [ (10, 10, 10), # Extra equal value (11, 11, 11), # Within threshold (12, 12, 12), # Within threshold (13, 13, 13), # Just outside threshold (50, 50, 50), # Far away ] # Test our object scr = ahk.Script() scr.waitPixel(threshold=threshold, interval=interval) self.assertTrue(getpx.called, msg="Mock wasn't called?") self.assertEqual( getpx.call_count, 4, msg="getPixel mock called {0} times instead of 4.".format( getpx.call_count))
def test_00_init(self): """Testing that script was initialized correctly.""" scr = ahk.Script() self.assertTrue(ahk.ready(nowait=True), msg="AHK not ready after init?") self.assertIsNotNone(scr.Clipboard, msg="Special Clipboard variable not initialized!") self.assertIsNotNone( scr.ErrorLevel, msg="Special ErrorLevel variable not initialized!")
def test_06_winExist(self): """Testing IfWinExist wrapper.""" script = ahk.Script() # Test that non-existent window isn't found name = "non-existent-{0}".format(time.time()) result = script.winExist(name) self.assertEqual( result, None, msg="Found unexpected window with name \"{0}\"!".format(name)) # Test that an active window is findable result = script.winExist("A") # "A" is the current active window self.assertNotEqual(result, None, msg="Can't find an active window?")
def test_03_convert_color(self): """Testing conversion of hex color.""" scr = ahk.Script() for i in range(10): t = tuple([random.randint(0, 255) for i in range(3)]) h = '0x' + ''.join('{0:02x}'.format(c) for c in t) res = scr.convert_color(h) #print res, t, h self.assertEqual( res, t, msg="Re-converted color {0} doesn't match {1} ({2})!".format( res, t, h))
def __call__(self, outVar, title, prompt, password, width, height, xPos, yPos): script = "\nInputBox" if outVar != "eg." or outVar != "": script += ", output" else: eg.PrintError("No Output Variable Provided") return if title: script += ", " + title else: script += ", " if prompt: script += ", " + prompt else: script += ", " if password: script += ", hide" else: script += ", " script += ", " + width + ", " + height if xPos: script += ", " + xPos else: script += ", " if yPos: script += ", " + yPos else: script += ", " inputScript = ahk.Script() inputScript.variable("output", str) ahk.execute(str(script)) print script print outVar + " = inputScript.output" try: if inputScript.ErrorLevel == 0: exec(outVar + " = inputScript.output") else: exec(outVar + " = ''") except: eg.PrintError("Problem with output variable.")
def __call__(self, msgText, msgID, msgTitle, msgTO, bStyle, iStyle, selButton, onTop): opVal = bStyle + (iStyle * 16) + (selButton * 256) if onTop: opVal += 4096 msgText = replaceEgVars(msgText) msgText = msgText.replace('\n', '`n') script = ahk.Script() script.message(text=msgText, title=msgTitle, options=opVal, timeout=msgTO) buttons = [ "OK", "Cancel", "Yes", "No", "TryAgain", "Continue", "Abort", "Retry", "Ignore" ] for b in buttons: if script.msgResult(name=b): if msgID == "": eg.TriggerEvent(msgTitle + "." + b, prefix="AHK.MsgBox") else: eg.TriggerEvent(msgID + "." + b, prefix="AHK.MsgBox") break
def setUp(self): """Configure test environment.""" self.script = ahk.Script()
import ahk import time script = ahk.Script() script.variable("text", str) #set AHK variable that can be accessed with Python ahk.execute("SetTitleMatchMode 2") currLines = [] prevLines = [] while (True): #gpsStatus = ahk.execute("WinActivate, NovAtel") #if gpsStatus == True: #if GPS Window exists # print "Exists!" # messageStatus = ahk.execute("WinActivate, CONFIG - ASCII") # if messageStatus == True: #if GPS ASCII coordinate window exists ahk.execute("WinGetText, text, CONFIG - ASCII") curr_text = script.text #print curr_text lines = curr_text.split('\n') for line in lines: if line not in prevLines: currLines.append(line) if line.find( "#BESTPOS" ) != -1: #if line has GPS coordinates, splice the string gpsCoords = line[line.find(';'):line.find("WGS")] gpsCoords = gpsCoords[gpsCoords.find("SINGLE") + 7:] gpsCoords = gpsCoords[:gpsCoords.rfind(',')] gpsCoords = gpsCoords[:gpsCoords.rfind(',')] gpsCoords = gpsCoords[:gpsCoords.rfind(',')] gpsCoords = gpsCoords[gpsCoords.find(',') + 1:]