user2801650
user2801650

Reputation: 11

Verification/Assertion of image in Sikuli: If IMG1 exists OR IMG2 exists

I am really struggling with the line

if mImg1 or mImg2:

I am trying to monitor for a set time if 1 of 2 images appears. If it does then it will perform the desired actions. I just cannot get the right syntax for it to perform the IF statement above.

If either is not null then I want it to break the loop. If not then carry on looping.

weWait = 10
while weWait > 0:
 mImg1 = exists("1379615300466.png",1)
 mImg2 = exists("1379534637993.png",1)
 print mImg1
 print mImg2
  if mImg1 or mImg2:
   print "breaking"
   break
  wait (1)
  weWait = weWait - 1

if not (mImg1 and mImg2):
  print "niether image appeared"
  exit(1)

if mImg2:
  print "img2 appeared"
  exit(1)

if mImg1:
print "img1 appeared"
exit(1)

I am an enthusiast and apologise for incorrect terminology.

Thanks in advance.

Upvotes: 1

Views: 22568

Answers (5)

Mahmud Riad
Mahmud Riad

Reputation: 1165

you can achieve this by using the following method just call this method and send the image name as the parameter .

  /**
  * Check and verify the image using Sikuli-Script
  */
 public boolean  verifyImageExists(String imageName){

     boolean isValid = false;
     try {
            Screen screen = new Screen();
            Pattern image = new Pattern(AppConstant.RESOURCE_DIR+imageName);
            //Wait 10ms for image 

            try 
            {
                screen.wait(image, 10);
            } catch (FindFailed e1)
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(screen.exists(image)!= null)
            {
                isValid = true;
            }
        }
        catch(Exception e){

        }
     return isValid;
 }

Upvotes: 2

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37766

Sikuli Java code:

import org.sikuli.script.FindFailed;
Screen screen = new Screen();
try{
    if(screen.exists("img1.png") != null || screen.exists("img2.png") != null){
        //DO YOUR ACTIONS
        screen.click("img1.png");
        }
    }
catch(FindFailed e){
    e.getStackTrace();
}

Or, you can do assertion by using TestNG or JUnit as below:

Match img1 = screen.exists("img1.png");
assertTrue(img1 != null);

Do same for img2.

Upvotes: 1

Akbar
Akbar

Reputation: 1525

exists() returns a Match object. You should check if it is null or not. Checking whether it is null or not would return true or false that could be used in argument of if statement

Upvotes: 0

Tenzin
Tenzin

Reputation: 2505

I now this topic is kinda old, but I came here because I was looking for the same answer.
Now does the question not realy get answered here, so I am sharing my solution.

What I did was, I used a "while True" to make a loop and look if the images are already visible.
If this is the case we do something with the images and break the while loop.

Example code:
(Sikuli with Python)

Image1 = ("image1.png")
Image2 = ("image2.png")
class Multi():
    def __init__(self):
        self.Search()
    def Search(self):
        # Look when one of the two images appear. 
        while True: 
            print('Searching....')
            if exists(Image1):
                print('Image1 located!')
                click(Image1)
                # Break loop.
                break
            elif exists(Image2):
                print('Image2 located!')
                click(Image2)
                # Break loop.
                break
            else:
                pass
# Run class 
Multi()

Upvotes: 1

Tumit Watcharapol
Tumit Watcharapol

Reputation: 138

if mImg1 or mImg2 != Null: null not defined. Sooooo frustrating. Wish I was better!

In Jython script you should use

if mImg1 != None or mImg2 != None: 

Upvotes: 2

Related Questions