【问题标题】:Verification/Assertion of image in Sikuli: If IMG1 exists OR IMG2 existsSikuli 中图像的验证/断言:如果 IMG1 存在或 IMG2 存在
【发布时间】:2013-09-26 15:35:54
【问题描述】:

我真的很纠结这条线

if mImg1 or mImg2:

如果出现 2 张图像中的 1 张,我正在尝试监控设定的时间。如果确实如此,那么它将执行所需的操作。我只是无法获得正确的语法来执行上面的 IF 语句。

如果其中一个不为空,那么我希望它打破循环。如果没有则继续循环。

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)

我是一名爱好者,对于不正确的术语表示歉意。

提前致谢。

【问题讨论】:

  • 请更新标题给我的描述性更强,不要添加标签。

标签: image if-statement exists sikuli


【解决方案1】:

如果 mImg1 或 mImg2 != Null:未定义 null。太令人沮丧了。希望 我更好!

在 Jython 脚本中你应该使用

if mImg1 != None or mImg2 != None: 

【讨论】:

    【解决方案2】:

    您可以使用以下方法来实现这一点,只需调用此方法并将图像名称作为参数发送即可。

      /**
      * 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;
     }
    

    【讨论】:

      【解决方案3】:

      Sikuli Java 代码:

      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();
      }
      

      或者,您可以使用 TestNG 或 JUnit 进行断言,如下所示:

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

      对 img2 做同样的事情。

      【讨论】:

        【解决方案4】:

        我现在这个话题有点老了,但我来到这里是因为我正在寻找相同的答案。
        现在这个问题在这里没有得到真正的回答,所以我分享我的解决方案。

        我所做的是,我使用“while True”来创建一个循环并查看图像是否已经可见。
        如果是这种情况,我们对图像做一些事情并打破 while 循环。

        示例代码:
        (Sikuli 与 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()
        

        【讨论】:

          【解决方案5】:

          exists() 返回一个 Match 对象。您应该检查它是否为空。检查它是否为 null 将返回 true 或 false 可用于 if 语句的参数

          【讨论】:

          • 嗯,我确实读过这篇文章,但是当我使用 != null 时,它似乎仍然无法正常工作并发现某些东西是匹配的。非常感谢。
          • 如果 mImg1 或 mImg2 != Null:
          • null 未定义。太令人沮丧了。希望我变得更好!
          • 在 Java 中是 if(mlmg1!=null || mlmg2!=null)
          猜你喜欢
          • 2023-03-11
          • 2020-05-13
          • 2021-03-05
          • 2022-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多