I’ve brought this up before on this forum before where I use a
java.awt.Robot to try to test a GUI, run db queries before and after test to make sure that every executed as expected, stuff like that which we currently do completely manually to make sure all our intermediary scripts are working right.
Now, I am trying to make this a little bit more developer friendly so that other people in my company can use this for testing out other projects.
Here’s a little proof of concept I have before I ask my questions -
# Needed for moving Stuff around import java.awt.Robot from java.awt.event import InputEvent, KeyEvent from java.lang import String import java.lang.IllegalArgumentException from java.awt import EventQueue, AWTEvent # For handling lists of GUIActions appropriately import Queue DELAY = 1000 rob = java.awt.Robot() class GUIAction(): def __init__(self): self.completed = False def _execute(self): system.util.invokeLater(self.execute, DELAY) def execute(self): # Required to be filled in by subclasses pass class MouseAction(GUIAction): def __init__(self, x, y): GUIAction.__init__(self) self.x = x self.y = y class LeftClick(MouseAction): def __init__(self, x, y): MouseAction.__init__(self, x, y) def execute(self): rob.mouseMove(self.x, self.y) rob.mousePress(InputEvent.BUTTON1_MASK) rob.mouseRelease(InputEvent.BUTTON1_MASK) self.completed = True class RightClick(MouseAction): def __init__(self, x, y): MouseAction.__init__(self, x, y) def execute(self): rob.mouseMove(self.x, self.y) rob.mousePress(InputEvent.BUTTON3_MASK) rob.mouseRelease(InputEvent.BUTTON3_MASK) self.completed = True class GUITest(Queue.Queue): def __init__(self, *args, **kwargs): Queue.Queue.__init__(self, *args, **kwargs) def runNextBlock(self): nextStep = self.get() nextStep._execute() while nextStep.completed == False: # Please don't judge this too harshly lol for i in range(1, 200000): pass def run(self): while self.qsize() > 0: self.runNextBlock() def runSampleTest(): sampleTest = GUITest() sampleTest.put(LeftClick(100,100)) sampleTest.put(RightClick(200,200)) sampleTest.run()
And this does work reasonably well, I don’t need to keep track of end times of system.util.invokes as long as the
execute function ends in a
self.completed = True then the Queue works correctly, I see the mouse moves, waits, moves, as expected, but I can’t help but feel that this in particular
while nextStep.completed == False: # Please don't judge this too harshly lol for i in range(1, 200000): pass
is very hacky.
So I have three questions
- If I continue to use the built-in python Queue, is there a better way to keep track of the instructions being completed and for the queue to know this?
- I was told in an earlier post about java.awt.EventQueue that takes in java.awt.AWTEvent’s that I am under the impression can do this sort of thing but I can’t seem to get it to work. For example It would require my
LeftClickclass to be a sublcass of java.awt.AWTEvent for it to be able to be entered into the EventQueue - I’m not sure how my LeftClick class would have to look like to satisfy that. Post I am referring to that I could not get to work - Have Robot type any arbitrary message via keyboard in vision? - #14 by PGriffith
- In terms of writing tests, right now I just put one together quickly by manually creating and adding to a queue and then calling my custom run function, but would this be the way to go going forward? Or would it be better to sublcass my GUITest class for each individual test, and I suppose in the
__init__I would add all my instructions?
Apologies for all the questions, in the winter break I’ve lost about 50% of my programming brain cells.