📜 ⬆️ ⬇️

Read more about Sikuli in test automation

Introduction


Sikuli is an API that allows writing automation scripts on Jython based on the visual component of any program / site, etc. Especially nice for automating Flash.
About Sikuli few articles are written and most of them are overview. Even less Russian-language help, and even fewer code examples. And the absence of the latter is perhaps the most tragic for a software tester who is faced with the need to automate any flash. It was precisely this that prompted me to write a more capacious article on Sikuli and describe in some detail some features of use.


Installation


To install and properly operate Sikuli, you will need:
1. Java of the sixth version and necessarily 32 bit.
2. Distribution
3. Service Pack.

Due to the fact that Sikuli comes only in 32-bit configuration, in order to work correctly on 64-bit systems, we must install Java 6u38 32x (with the 7th version does not work).
It is also necessary to put the update package. Installs easily. The contents of the Sikuli-IDE folder are simply copied from the archive (point 3) into the folder with the installed program C: \ Program Files (x86) \ Sikuli X, by the way, do not change the default installation folder.
If suddenly something is wrong, here is a reference to read.
')

Functionality and features.


Screenshot image
It is used to highlight a specific area of ​​the screen and then create a pattern. image
When you click on the pattern, the "Template Setup" window appears. image
Tabbed everything is primitive and clear. The only thing I will note is that on the “target offset” tab, the goal is where to move the cursor and make a click / enter. And the place of the click can lie far beyond the pattern.

Now about the features.
*****. png is the name of the desired picture that should be in the folder of a previously saved project. image
similar = similarity, where 1 = 100% (it is better not to use because it does not always find it), for example, 0.85 (through a dot) = 85%, which in my opinion is the most optimal value.
targetOffset (x, y)) and here it is important that the coordinates "x" and "y" is the offset of the target from the center of the pattern.

Area selection / wait () / Click ()

image
Great function and most importantly, extremely useful.
Many say that Sikuli has a bad understanding of work and is still mistaken great. I completely disagree with this, and I believe that a competent approach significantly increases the performance of the program compared to similar IDEs.

This function sets the active screen area.
image
in which the search for the desired pattern will be performed.
You can also like this:
Region(x,y,w,h).find(Pattern("*****.png".similar(0.85)targetOffset(x,y))) 

It is extremely useful if we need the script to react to changes promptly, not to slow down the system.
As an example:
from all of our large screen, we select only a small area where every time a cherished button appears. image
Next, we describe what we need to look for in this area and then what to do with it.
image
In order to optimize the process, we need to run this script and make it click for us 1 time. After we go to the tab "Message" where we see the coordinates of the click.
image
Then do a cycle and click on the coordinates (this is provided that the button will always appear in the same place).
Similarity in the search do about 70 percent, will find faster, the expectation of 999 ...
image
As a result, we have almost instantaneous triggering.


Find ()

The find () function searches for a pattern. But why? It's simple. Once found, and then he will know where the found object is always. How to put into practice:
We have a lot of things either ... image
And the program will have to press this “something” long and hard ...
In order for the program to know where to click, you need to set targetOffset ()
image
Repeat for each goal, assign variables, optimize if the field with the set is in the same place.
deploy here
image

Further we can use the found zones as we want. And the click will occur without delay in searching, etc.
image

The rest of the functionality will not be considered it is primitive and simple.

Examples


The script should be sane!
image

Creating the function yan (). The first call to the function will call the find () command, subsequent calls will call click ().
image

Through the dialog box, set the number of cycles.
image

the try / except function and how good it is.
image

And at the end of the bot to send gifts to friends (of which there may be 5k) in the game DOTD
 Settings.MoveMouseDelay = 0.02 def f5(): while "f5": click(Pattern("KyddinsmapsD-1.png").targetOffset(-41,0)) sleep(5) if exists(Pattern("1344731699750.png").similar(0.90),10): continue else: pass try: wait(Pattern("CLOSElah.png").similar(0.85),25) except: continue try: click(Pattern("CLOSElah.png").similar(0.85).targetOffset(1,-6)) dragDrop(Location(1430,388), Location(1431,435)) # 435 break except: pass def send_frend(): click(Location(849,495)) sleep(1) click(Location(578,695)) sleep(1) def pre_click_frend(): click(Location(713,300)) click(Location(713,315)) def click_frend(): click(Location(713,332)) click(Location(714,348)) click(Location(714,364)) click(Location(715,380)) click(Location(714,396)) click(Location(713,412)) click(Location(713,427)) click(Location(713,444)) click(Location(713,460)) click(Location(713,474)) click(Location(713,490)) click(Location(715,506)) click(Location(714,522)) click(Location(714,537)) click(Location(713,554)) click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)) sleep(1) click(Location(713,288)) click(Location(713,304)) click(Location(713,320)) click(Location(713,336)) click(Location(713,352)) click(Location(713,368)) click(Location(713,384)) click(Location(713,400)) click(Location(713,416)) click(Location(713,432)) click(Location(713,448)) click(Location(713,464)) click(Location(713,480)) click(Location(713,496)) click(Location(713,512)) click(Location(713,528)) click(Location(713,544)) click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)), click(Location(923,541)) sleep(1) click(Location(713,300)), click(Location(713,315)) click(Location(713,332)) click(Location(714,348)) click(Location(714,364)) click(Location(715,380)) click(Location(714,396)) click(Location(713,412)) click(Location(713,427)) click(Location(713,444)) click(Location(713,460)) click(Location(713,474)) click(Location(713,490)) click(Location(715,506)) click(Location(714,522)) click(Location(714,537)) sleep(1) def send_fb(): click(Location(756,760)) sleep(2) try: click(Pattern("0TIIp3BHTb33.png").targetOffset(-2,1)) except: pass def off (event): popup("The program is completed.\nCreator of this miracle: Grumo Van Blum.\nIf you have questions about using this program, please contact us by e-mail: grumovanblum@gmail.com\nGood Luck 8c)") exit() popup("If you want to stop the script, press: 'Ctrl' + 'Alt' + 'Space'") Env.addHotkey(Key.SPACE, KeyModifier.ALT+KeyModifier.CTRL, off) click(Pattern("1344836792562.png").similar(0.88)) n = 0 n = int(input("How many cycles you want to run?")) while n > 0: f5() while "cycle": if n == 0: break else: send_frend() if exists(Pattern("Jd.png").similar(0.96),1): popup("Friends come to an end :)") n=0 continue else: pre_click_frend() sleep(1) if exists(Pattern("Youcanontysd.png").similar(0.95),1): f5() send_frend() pre_click_frend() else: pass click_frend() send_fb() n -= 1 if n == 0: break else: sleep(3) 

And a video about how it works (shot in debug mode).


Utilities Used


Alt + Shift + c stops the script execution and returns the IDE.

Set the cursor movement time:
 Settings.MoveMouseDelay = 0.02 

Assign an event to key combinations:
 def off (event): exit() Env.addHotkey(Key.SPACE, KeyModifier.ALT+KeyModifier.CTRL, off) 

Calling the dialog box (integer):
 n = int(input("    ?")) 

Inaction in 1 sec:
 sleep(1) 

If after inserting the copied text you have replaced the code of a pattern or region with a picture, simply press 1 or 2 times the key combination Ctrl + z.
image

This is not all, and there will be more. But at this stage I think that I expressed the main idea.
I would be happy to feedback.
If you have questions about Sikuli, then write.

Next time I’ll probably tell you how to teach Sikuli to recognize the text and write the error log to a separate file.

Thanks for attention.

Source: https://habr.com/ru/post/163883/


All Articles