def testOpenApp(self): """ This looks for the specified Notepad icon on the desktop. This test will probably fail if you don't have the same setup I do. """ def test_observer(appear_event): assert (appear_event.isAppear()) img = appear_event.getImage() region = appear_event.getRegion() region.TestFlag = True region.stopObserver() r = lackey.Screen(0) if sys.platform.startswith("win"): r.doubleClick("notepad.png") elif sys.platform == "darwin": r.doubleClick("textedit.png") r.wait("preview_open_2.png") r.type("n", lackey.KeyModifier.CMD) time.sleep(2) r.type("This is a test") if sys.platform.startswith("win"): r.onAppear( lackey.Pattern("test_text.png").similar(0.6), test_observer) elif sys.platform == "darwin": r.onAppear( lackey.Pattern("mac_test_text.png").similar(0.6), test_observer) r.observe(30) self.assertTrue(r.TestFlag) self.assertGreater(r.getTime(), 0) # OCR if sys.platform.startswith("win"): a = r.find("notepad_header.png").below(400) elif sys.platform == "darwin": a = r.find("textedit_header.png").below(400) #a.highlight() self.assertIsNotNone(a.findText("This is a test")) self.assertGreater(len([a.findAllText("This is a test")]), 0) # if sys.platform.startswith("win"): r.rightClick(r.getLastMatch()) r.click("select_all.png") r.type("c", lackey.Key.CTRL) # Copy elif sys.platform == "darwin": r.type("a", lackey.KeyModifier.CMD) r.type("c", lackey.KeyModifier.CMD) self.assertEqual(r.getClipboard(), "This is a test") r.type("{DELETE}") self.assertTrue(r.waitVanishText("This is a test")) if sys.platform.startswith("win"): r.type("{F4}", lackey.Key.ALT) elif sys.platform == "darwin": r.type("w", lackey.KeyModifier.CMD) r.wait(lackey.Pattern("textedit_save_2.png")) r.click( lackey.Pattern("textedit_save_2.png").targetOffset(-126, 20)) lackey.sleep(0.5) r.type("q", lackey.KeyModifier.CMD)
def test_movement(self): self.mouse.move(lackey.Location(10, 10)) lackey.sleep(0.01) self.assertEqual(self.mouse.getPos().getTuple(), (10, 10)) self.mouse.moveSpeed(lackey.Location(100, 200), 0.5) self.assertEqual(self.mouse.getPos().getTuple(), (100, 200)) lackey.wheel(self.mouse.getPos(), 0, 3) # Mostly just verifying it doesn't crash
def test_getters(self): if sys.platform.startswith("win"): app = lackey.App("notepad.exe tests\\test_cases.py") app2 = lackey.App("notepad.exe tests\\test_cases.py") #app.setUsing("test_cases.py") app.open() app2.open() lackey.sleep(1) app2.close() else: raise NotImplementedError("Platforms supported include: Windows") app.focus() self.assertEqual(app.getName(), "notepad.exe") self.assertTrue(app.isRunning()) self.assertEqual(app.getWindow(), "test_cases.py - Notepad") self.assertNotEqual(app.getPID(), -1) region = app.window() self.assertIsInstance(region, lackey.Region) self.assertGreater(region.getW(), 0) self.assertGreater(region.getH(), 0) if sys.platform.startswith("win"): app.close() lackey.sleep(1.0)
def main(): lackey.Settings.InfoLogs = False lackey.Settings.ActionLogs = False r = start_game() board = r.offset(lackey.Location(336, 80)) board.setW(660) board.setH(660) cell_size = board.getW() / 8 while True: grid = find_gems(board) #print("\n".join([str(r) for r in grid])) moves = calculate_move(grid) if moves is None: print("No matches found, waiting...") continue square1, square2 = moves print(square1, square2) square1_click = lackey.Location( board.getX() + (square1[1] * cell_size + (cell_size / 2)), board.getY() + (square1[0] * cell_size + (cell_size / 2))) square2_click = lackey.Location( board.getX() + (square2[1] * cell_size + (cell_size / 2)), board.getY() + (square2[0] * cell_size + (cell_size / 2))) board.click(square1_click) lackey.sleep(0.3) board.click(square2_click)
def testTypeCopyPaste(self): """ Also tests the log file """ lackey.Debug.setLogFile("logfile.txt") r = lackey.Screen(0) if sys.platform.startswith("win"): app = lackey.App("notepad.exe").open() time.sleep(1) r.type("This is a Test") r.type("a", lackey.Key.CTRL) # Select all r.type("c", lackey.Key.CTRL) # Copy self.assertEqual(r.getClipboard(), "This is a Test") r.type("{DELETE}") # Clear the selected text r.paste( "This, on the other hand, is a {SHIFT}broken {SHIFT}record." ) # Paste should ignore special characters and insert the string as is r.type("a", lackey.Key.CTRL) # Select all r.type("c", lackey.Key.CTRL) # Copy self.assertEqual( r.getClipboard(), "This, on the other hand, is a {SHIFT}broken {SHIFT}record.") elif sys.platform == "darwin": app = lackey.App("+open -e") lackey.sleep(2) #r.debugPreview() r.wait(lackey.Pattern("preview_open_2.png")) r.click(lackey.Pattern("preview_open_2.png")) lackey.type("n", lackey.KeyModifier.CMD) time.sleep(1) app = lackey.App("Untitled") r.type("This is a test") r.type("a", lackey.KeyModifier.CMD) # Select all r.type("c", lackey.KeyModifier.CMD) # Copy self.assertEqual(r.getClipboard(), "This is a test") r.type("{DELETE}") # Clear the selected text r.paste( "This, on the other hand, is a {SHIFT}broken {SHIFT}record." ) # Paste should ignore special characters and insert the string as is r.type("a", lackey.KeyModifier.CMD) # Select all r.type("c", lackey.KeyModifier.CMD) # Copy self.assertEqual( r.getClipboard(), "This, on the other hand, is a {SHIFT}broken {SHIFT}record.") else: raise NotImplementedError( "Platforms supported include: Windows, OS X") app.close() lackey.Debug.setLogFile(None) self.assertTrue(os.path.exists("logfile.txt"))
def testOpenApp(self): """ This looks for the specified Notepad icon on the desktop. This test will probably fail if you don't have the same setup I do. """ def test_observer(appear_event): assert(appear_event.isAppear()) img = appear_event.getImage() region = appear_event.getRegion() region.TestFlag = True region.stopObserver() r = lackey.Screen(0) if sys.platform.startswith("win"): r.doubleClick("notepad.png") elif sys.platform == "darwin": r.doubleClick("textedit.png") r.wait("preview_open_2.png") r.type("n", lackey.KeyModifier.CMD) time.sleep(2) r.type("This is a test") if sys.platform.startswith("win"): r.onAppear(lackey.Pattern("test_text.png").similar(0.6), test_observer) elif sys.platform == "darwin": r.onAppear(lackey.Pattern("mac_test_text.png").similar(0.6), test_observer) r.observe(30) self.assertTrue(r.TestFlag) self.assertGreater(r.getTime(), 0) if sys.platform.startswith("win"): r.rightClick(r.getLastMatch()) r.click("select_all.png") r.type("c", lackey.Key.CTRL) # Copy elif sys.platform == "darwin": r.type("a", lackey.KeyModifier.CMD) r.type("c", lackey.KeyModifier.CMD) self.assertEqual(r.getClipboard(), "This is a test") r.type("{DELETE}") if sys.platform.startswith("win"): r.type("{F4}", lackey.Key.ALT) elif sys.platform == "darwin": r.type("w", lackey.KeyModifier.CMD) r.click(lackey.Pattern("textedit_save_2.png").targetOffset(-86, 25)) lackey.sleep(0.5) r.type("q", lackey.KeyModifier.CMD)
def testTypeCopyPaste(self): """ Also tests the log file """ lackey.Debug.setLogFile("logfile.txt") r = lackey.Screen(0) if sys.platform.startswith("win"): app = lackey.App("notepad.exe").open() time.sleep(1) r.type("This is a Test") r.type("a", lackey.Key.CTRL) # Select all r.type("c", lackey.Key.CTRL) # Copy self.assertEqual(r.getClipboard(), "This is a Test") r.type("{DELETE}") # Clear the selected text r.paste("This, on the other hand, is a {SHIFT}broken {SHIFT}record.") # Paste should ignore special characters and insert the string as is r.type("a", lackey.Key.CTRL) # Select all r.type("c", lackey.Key.CTRL) # Copy self.assertEqual(r.getClipboard(), "This, on the other hand, is a {SHIFT}broken {SHIFT}record.") elif sys.platform == "darwin": app = lackey.App("+open -e") lackey.sleep(2) #r.debugPreview() r.wait(lackey.Pattern("preview_open_2.png")) r.click(lackey.Pattern("preview_open_2.png")) lackey.type("n", lackey.KeyModifier.CMD) time.sleep(1) app = lackey.App("Untitled") r.type("This is a Test") r.type("a", lackey.KeyModifier.CMD) # Select all r.type("c", lackey.KeyModifier.CMD) # Copy self.assertEqual(r.getClipboard(), "This is a Test") r.type("{DELETE}") # Clear the selected text r.paste("This, on the other hand, is a {SHIFT}broken {SHIFT}record.") # Paste should ignore special characters and insert the string as is r.type("a", lackey.KeyModifier.CMD) # Select all r.type("c", lackey.KeyModifier.CMD) # Copy self.assertEqual(r.getClipboard(), "This, on the other hand, is a {SHIFT}broken {SHIFT}record.") else: raise NotImplementedError("Platforms supported include: Windows, OS X") app.close() lackey.Debug.setLogFile(None) self.assertTrue(os.path.exists("logfile.txt"))
def test_getters(self): if sys.platform.startswith("win"): app = lackey.App("notepad.exe tests\\test_cases.py") app2 = lackey.App("notepad.exe tests\\test_cases.py") #app.setUsing("test_cases.py") app.open() app2.open() lackey.sleep(1) app2.close() app.focus() self.assertEqual(app.getName(), "notepad.exe") self.assertTrue(app.isRunning()) self.assertRegex(app.getWindow(), "test_cases(.py)? - Notepad") self.assertNotEqual(app.getPID(), -1) region = app.window() self.assertIsInstance(region, lackey.Region) self.assertGreater(region.getW(), 0) self.assertGreater(region.getH(), 0) app.close() elif sys.platform == "darwin": if "TRAVIS" in os.environ: return # Skip these tests in travis build environment a = lackey.App("+open -a TextEdit tests/test_cases.py") a2 = lackey.App("open -a TextEdit tests/appveyor_test_cases.py") lackey.sleep(1) app = lackey.App("test_cases.py") app2 = lackey.App("appveyor_test_cases.py") #app.setUsing("test_cases.py") lackey.sleep(1) app2.close() app.focus() print(app.getPID()) self.assertTrue(app.isRunning()) self.assertEqual(app.getName()[-len("TextEdit"):], "TextEdit") #self.assertEqual(app.getWindow(), "test_cases.py") # Doesn't work on `open`-triggered apps self.assertNotEqual(app.getPID(), -1) region = app.window() self.assertIsInstance(region, lackey.Region) self.assertGreater(region.getW(), 0) self.assertGreater(region.getH(), 0) app.close() else: raise NotImplementedError( "Platforms supported include: Windows, OS X")
def test_getters(self): if sys.platform.startswith("win"): app = lackey.App("notepad.exe tests\\test_cases.py") app2 = lackey.App("notepad.exe tests\\test_cases.py") #app.setUsing("test_cases.py") app.open() app2.open() lackey.sleep(1) app2.close() app.focus() self.assertEqual(app.getName(), "notepad.exe") self.assertTrue(app.isRunning()) self.assertRegex(app.getWindow(), "test_cases(.py)? - Notepad") self.assertNotEqual(app.getPID(), -1) region = app.window() self.assertIsInstance(region, lackey.Region) self.assertGreater(region.getW(), 0) self.assertGreater(region.getH(), 0) app.close() elif sys.platform == "darwin": if "TRAVIS" in os.environ: return # Skip these tests in travis build environment a = lackey.App("+open -a TextEdit tests/test_cases.py") a2 = lackey.App("open -a TextEdit tests/appveyor_test_cases.py") lackey.sleep(1) app = lackey.App("test_cases.py") app2 = lackey.App("appveyor_test_cases.py") #app.setUsing("test_cases.py") lackey.sleep(1) app2.close() app.focus() print(app.getPID()) self.assertTrue(app.isRunning()) self.assertEqual(app.getName()[-len("TextEdit"):], "TextEdit") #self.assertEqual(app.getWindow(), "test_cases.py") # Doesn't work on `open`-triggered apps self.assertNotEqual(app.getPID(), -1) region = app.window() self.assertIsInstance(region, lackey.Region) self.assertGreater(region.getW(), 0) self.assertGreater(region.getH(), 0) app.close() else: raise NotImplementedError("Platforms supported include: Windows, OS X")
import lackey from WindowMgr import * # turn on the monitor win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, -1) lackey.sleep(1) lackey.Keyboard().keyDown(lackey.Key.PRINTSCREEN) lackey.Keyboard().keyUp(lackey.Key.PRINTSCREEN) # call the wechat window to foreground w = WindowMgr() w.find_window("WeChatMainWndForPC", "微信") w.set_foreground() lackey.Keyboard().keyDown(lackey.Key.PRINTSCREEN) lackey.Keyboard().keyUp(lackey.Key.PRINTSCREEN) lackey.sleep(1) lackey.click(r"D:\Code\DAD\image\chat.jpg") lackey.Mouse().move(60, 0) lackey.sleep(1) lackey.Mouse().wheel(1, 200) # enter the subscription list while True: try: lackey.click(r"D:\Code\DAD\image\subscription.jpg") break except: