Unable to click in random location based on image search.

Asked by Betty20921

Hello,

I am helping a friend with android prototyping, long story short I am trying to simulate random input (clicks in this case) around a specific location (a pattern).

To accomplish this I've tried all sorts of random algorithms provided by jython and both java. The end result is the same,
TypeError: offset(): 1st arg can't be coerced to int

I have even looked up the questions and examples of others and used alternative modifiers and I still end up with the issue of "can't be coerced to int"
(function I've used, .targetOffset() .offset(), .left() .up(), etc)

I've even played with the various forms of random within Jyton, random.random(), .randrange(), .choice(), etc. I've also explored Math.random() within java. The result is always the same.

#-------------------
import java.util.Random
import random

def x():
    random.randint(0,10)

b = wait("1340345219668.png")
click(b.getCenter().offset(x,x))
#-------------------

If anyone has any suggestions I'd greatly appreciate any help offered.'

edit: forgot to add
Windows 7 64bit
r903 of Sikuli
Java 7 (update 5) 32bit

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Roman Podolyan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Since you seem to have checked all possibilities and the usage is correct (it works on my Mac OSX Lion, which is 64Bit allover the place including Java 6), the problem might be, that you are using Java 7.

Sikuli is not yet tested with Java 7 and has some issues.
So you should try on a system with latest Java 6 installed.

Revision history for this message
Betty20921 (betty203154) said :
#2

Thanks for the quick response,

I went ahead and moved back to Java 6 (update 33) and still the same issue. I tried Java6 on both r903 and r930 with the same result "can't be coerced to int".

I decided to see if it was a windows issue and tried it out on OSX 10.7.4. With both r903 & r930 I still received the "can't be coerced to int".

Is your OSX build using Jython 2.5.2 (modded in) or the stock Jython with r930?

Again thanks for all your help.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

This is what I used to test:

import random
x = random.randint(1,10)
print Region(0,0,10,10).getCenter().offset(x, x)

prints:
(9,9)
which is a Location object.

Sikuli rc3 generally comes with Jython 2.5.2

you might paste your code here.

Revision history for this message
Best Roman Podolyan (podolyan-roman) said :
#4

Betty20921 ,

Here is peace of my working code for random clicks:
===
  x = random.randint(0,max_x)
  y = random.randint(0,max_y)
  click(mouseloc.right(x).below(y))
===
where mouseloc is of type Location.

In my current script that location is device emulator top left corner.

Initialization of mouseloc:
===
hover(Pattern("Carrier.png").targetOffset(-36,10))
mouseloc = Env.getMouseLocation()
===

Hope that these ideas will help you.

Revision history for this message
Roman Podolyan (podolyan-roman) said :
#5

#-------------------
import java.util.Random
import random

def x():
    random.randint(0,10)

b = wait("1340345219668.png")
click(b.getCenter().offset(x,x))
#-------------------

I don't see any assignment of "x" variable. So the Jython gives you right error: "x" should be initialized by integer number.

You may try it this way (two variables, X and Y):
#------------
x = random.randint(0,10)
y = random.randint(0,10)
b = wait("1340345219668.png")
click(b.getCenter().offset(x,y))
#-----------

This code works for me in Sikuli.

Revision history for this message
Roman Podolyan (podolyan-roman) said :
#6

Or you should correct you function and call it right way:

#-----------
import random

def x():
    return random.randint(0,10)

b = wait("1340794873108.png")
hover(b.getCenter().offset(x(),x()))
#-----------

This works too.

Notice "return" keyword (missing in your code) and mandatory "()" after "x" when calling it as function.

Revision history for this message
Roman Podolyan (podolyan-roman) said :
#7

Hover instead of a Click in last variant (for my testing purposes)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

@ Roman
once again: great finding !!!

I have simply overseen this obvious programming error:
x returns the function object, which actually cannot be coerced to int, LOL

Sometimes the world can be so easy.

Revision history for this message
Betty20921 (betty203154) said :
#9

Thanks Roman Podolyan, that solved my question.

Revision history for this message
Betty20921 (betty203154) said :
#10

Defining the random.randomint as " x = " rather than "def x()".

Thank you both so much!