For loop not executing if statement in Java

Asked by Ali Balkot

I have created an infinite loop which clicks a "spin" button in an online game and I am waiting for the balance to be too low for the user to spin, in which the spin button becomes inaccessible. Everything is working smoothly until the spin button becomes inaccessible for the application to click, in which the code does not access the if statement but I'm not too sure why.

Here is my code:

   for(;;) {

   screen.click(screen.wait(spinButton, 20), 0);

   if (screen.exists(spinButton, 1) == null) {
   driver.navigate().refresh();
   }
}

So the screen click on the spinButton is looping here, and then I've added an if statement for when it becomes un-clickable (spinButton == null) - but however the if statement is not executed, the loop just keeps trying to look for the spinButton then eventually fails.

Could anyone possibly assist me with this and guide me in the right direction? Would be much appreciated.

Question information

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

read
http://sikulix-2014.readthedocs.io/en/latest/basicinfo.html#sikulix-how-does-it-find-images-on-the-screen
to understand the magic:

the "inaccessible" button is still visible for SikuliX, only with a lower score, which is still above 0.7 as standard value and hence the exists does not return null.

The easiest way to adjust the minimum score is to use the Sikuli IDE for some testing/evaluation about the scores (use the highlight feature to track and get the values or use the Preview feature)

Revision history for this message
Ali Balkot (alibalkot3) said :
#2

Hi RaiMan - thank you for responding so soon.

However when I change the if statement condition to be something else for example a display message which always appears when the balance is too low - it still fails.

The loop ignores it and keeps executing the "click spin button" and throws an error saying "can not find "spinButton" S: 0.7 on the screen" - which is confusing me as even when the if statement condition is definitely true it still keeps trying to click the spinButton.

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

It is always difficult to "debug" these situations here.

How do you verify that the if statement is not executed?
insert at least some print statements.

What I see is that you seem to refresh the screen with the webdriver feature.

May be it is a timing problem and you should raise the wait time here:
screen.exists(spinButton, 1)

Revision history for this message
Ali Balkot (alibalkot3) said :
#4

Thanks a lot for your help - I did as you suggested and raised the wait time and it has worked successfully.

Revision history for this message
Ali Balkot (alibalkot3) said :
#5

Thanks RaiMan, that solved my question.