for循环跳过数组

时间:2016-01-14 16:12:06

标签: java arrays sikuli

我目前在我的脚本中遇到一个问题,我使用for循环遍历一系列元素并检查它们是否存在于GUI中。我的问题是for循环总是跳过数组的第一个条目。

我目前的脚本如下:

public class GUIFunctionality {
    static Properties config = Data.getProperties("config");
    static int Pass = 0;
    static Screen s = new Screen();

    @Test(priority = 0)
    public static void loginGUI() {
        WebDriver driver = AutomationWebDriver.getWebDriver("firefox", config.getProperty("url"));

        // Test all GUI elements on login screen.

        String[] login_elements = { "loginbutton.png", "logintitle.png", "smalllogo.png", "remembermechecked.png",
                "signupbutton.png", "signuptitle.png", "changelanguage.png" };
        ArrayList<String> passed = new ArrayList<String>();
        ArrayList<String> failed = new ArrayList<String>();

        for (String base : login_elements) {
            String file = String.format("imagerepo/config/%s", base);
            if (s.exists(file) != null) {
                System.out.println(file + " has been successfully found.");
                passed.add(file);
                Pass++;
            } else {
                System.out.println(file + " has not been found.");
                failed.add(file);
            }
        }

这个脚本完全忽略"loginbutton.png",几乎就像它根本不存在于脚本中一样。我真的很难过为什么。这是控制台输出:

imagerepo/config/logintitle.png has been successfully found.
imagerepo/config/smalllogo.png has been successfully found.
imagerepo/config/remembermechecked.png has been successfully found.
imagerepo/config/signupbutton.png has been successfully found.
imagerepo/config/signuptitle.png has been successfully found.
imagerepo/config/changelanguage.png has been successfully found.
Found elements: [imagerepo/config/logintitle.png, imagerepo/config/smalllogo.png, imagerepo/config/remembermechecked.png, imagerepo/config/signupbutton.png, imagerepo/config/signuptitle.png, imagerepo/config/changelanguage.png]
Missing elements: []

我想知道我需要改变什么,所以String[] login_elements的第一个条目包含在for循环中。有趣的是,在String[] login_elements中添加一个条目将完全解决它。

进行这一小改动:(nobutton.png是存储库中存在的图像,但不在测试页面上)

String[] login_elements = { "nobutton.png", "loginbutton.png", "logintitle.png", "smalllogo.png",
                "remembermechecked.png", "signupbutton.png", "signuptitle.png", "changelanguage.png" };

现在,这一个更改将打印到控制台:

imagerepo/config/nobutton.png has not been found.
imagerepo/config/loginbutton.png has been successfully found.
imagerepo/config/logintitle.png has been successfully found.
imagerepo/config/smalllogo.png has been successfully found.
imagerepo/config/remembermechecked.png has been successfully found.
imagerepo/config/signupbutton.png has been successfully found.
imagerepo/config/signuptitle.png has been successfully found.
imagerepo/config/changelanguage.png has been successfully found.
Found elements: [imagerepo/config/loginbutton.png, imagerepo/config/logintitle.png, imagerepo/config/smalllogo.png, imagerepo/config/remembermechecked.png, imagerepo/config/signupbutton.png, imagerepo/config/signuptitle.png, imagerepo/config/changelanguage.png]
Missing elements: [imagerepo/config/nobutton.png]

此控制台输出包括该阵列中的每个条目。从数组中删除“nobutton.png”将使我们回到原来的问题。

那到底是怎么回事?我唯一能想到的是数组中包含第一个条目的最小字符串数,但这看起来很愚蠢。

修改s.exists(String)Sikuli屏幕的一个实例,使用exists功能检查网页上元素是否存在。我真的不认为这与错误有任何关系。我也可能完全错了。我通过反复试验了解了大部分Sikuli图书馆(关于发布日期的时间紧迫是一件可怕的事情),所以我对“为什么”的无知是相当高的,这就是为什么我在这里。

编辑:请记住,在阵列中再添加一个条目可以完全解决问题。

修改:添加了s的实例。行WebDriver driver = AutomationWebDriver.getWebDriver("firefox", config.getProperty("url"));是我用来启动WebDriver实例的Selenium WebDriver的实例,我必须与Sikuli一起使用,因为我们的Web应用程序是fubar(6年的遗留代码)。

另一个编辑Region.exists()方法和文档的源代码。

Source Code

Documentation

此问题已得到解答。 @Berger和@Andy Thomas也提供了一些关于循环发生情况的见解

  

我想我找到了源代码。 exists使用基于超时值的while循环等,因此使用相同参数的后续调用很可能返回另一个结果,请参阅:https://github.com/RaiMan/SikuliX-2014/blob/master/API/src/main/java/org/sikuli/script/Region.java - @Berger

     

我从another Sikuli source file看到默认的autoWaitTimeout是63秒,这使得比赛条件易于观察。这个问题的两个重要教训是:1)默认情况通常很有用,特别是如果预期不会发生 - 以及2)如果您想要一个返回值,请进行一次调用。 - @Andy Thomas

3 个答案:

答案 0 :(得分:5)

您没有默认案例。您正在使用if-elseif而不是if-else。

for (String base : login_elements) {
  String file = String.format("imagerepo/config/%s", base);
  if (s.exists(file) != null) {
    ...
  } else if (s.exists(file) == null) {
    ...
  }
}

您的第二个条件包括第二次致电s.exists(file)如果未输入分支,则返回的值必须在不同呼叫之间更改。

您可以通过添加默认案例来处理此问题。一个简单的方法是消除第二个条件。

for (String base : login_elements) {
  String file = String.format("imagerepo/config/%s", base);
  if (s.exists(file) != null) {
    ...
  } else {
    ...
  }
}

调试器可以帮助您找到这样的问题。如果在第一个条件下设置断点,您将看到循环正在考虑第一个文件。

答案 1 :(得分:2)

这是因为你没有面对所有可能性:

if (s.exists(file) != null) {
    System.out.println(file + " has been successfully found.");
    passed.add(file);
} else {
    System.out.println(file + " has not been found.");
    failed.add(file);
} 

将使用相同的代码抛出错误...

答案 2 :(得分:2)

我相信Java代码应该是:

String[] login_elements = {
    "loginbutton.png",
    "logintitle.png",
    "smalllogo.png", 
    "remembermechecked.png",
    "signupbutton.png",
    "signuptitle.png",
    "changelanguage.png"
};

ArrayList<String> passed = new ArrayList<String>();
ArrayList<String> failed = new ArrayList<String>();

for (String base : login_elements) {
    String file = String.format("imagerepo/config/%s", base);
    File s = new File(file);
    if (s.exists()) {
        System.out.println(file + " has been successfully found.");
        passed.add(file);
    }
    else {
        System.out.println(file + " has not been found.");
        failed.add(file);
    }
}

System.out.println("Found elements: " + passed);
System.out.println("Missing elements: " + failed);