for循环跳过数组中的第一个字符串
for loop skipping first string in array
我目前在我的脚本中遇到一个问题,我使用 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”将使我们回到原来的问题。
那到底是怎么回事?我唯一可能想到的是数组中包含第一个条目的最少字符串数,但这看起来很愚蠢。
Edit: s.exists(String)
是 Sikuli
屏幕的一个实例,使用 exists
函数来检查屏幕上的元素是否存在页。我真的不认为这与错误有任何关系。我也可能完全错了。我通过反复试验了解了大部分 Sikuli 库(发布日期前后的时间紧缩是一件可怕的事情),所以我对“为什么”的无知非常高,这就是我来这里的原因。
编辑:记住,在数组中再添加一项可以完全解决问题。
编辑:添加了s
的实例。 WebDriver driver = AutomationWebDriver.getWebDriver("firefox", config.getProperty("url"));
行是我用来启动 WebDriver 实例的 Selenium WebDriver 实例,我必须将其与 Sikuli 一起使用,因为我们的 Web 应用程序是 fubar(6 年的遗留代码)。
另一个编辑:Region.exists()
方法和文档的源代码。
此问题已得到解答。 @Berger 和@Andy Thomas 也对循环:
发生了什么提供了一些见解
I think I have found the source code. exists uses a while loop based on a timeout value, among other things, so a subsequent call with the same parameter, could well return another result, see : https://github.com/RaiMan/SikuliX-2014/blob/master/API/src/main/java/org/sikuli/script/Region.java - @Berger
I see from another Sikuli source file that the default autoWaitTimeout is 63 seconds, making the race condition easy to observe. Two important lessons from this question are: 1) A default case is frequently useful, especially if it's not expected to occur -- and 2) If you want a single return value, make a single call. - @Andy Thomas
您没有默认案例。您使用的是 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 {
...
}
}
调试器可以帮助您发现此类问题。如果您在第一个条件下设置断点,您会看到循环正在考虑第一个文件。
这是因为你没有面对所有的可能性:
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);
}
使用相同的代码会抛出错误...
我认为 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);
我目前在我的脚本中遇到一个问题,我使用 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”将使我们回到原来的问题。
那到底是怎么回事?我唯一可能想到的是数组中包含第一个条目的最少字符串数,但这看起来很愚蠢。
Edit: s.exists(String)
是 Sikuli
屏幕的一个实例,使用 exists
函数来检查屏幕上的元素是否存在页。我真的不认为这与错误有任何关系。我也可能完全错了。我通过反复试验了解了大部分 Sikuli 库(发布日期前后的时间紧缩是一件可怕的事情),所以我对“为什么”的无知非常高,这就是我来这里的原因。
编辑:记住,在数组中再添加一项可以完全解决问题。
编辑:添加了s
的实例。 WebDriver driver = AutomationWebDriver.getWebDriver("firefox", config.getProperty("url"));
行是我用来启动 WebDriver 实例的 Selenium WebDriver 实例,我必须将其与 Sikuli 一起使用,因为我们的 Web 应用程序是 fubar(6 年的遗留代码)。
另一个编辑:Region.exists()
方法和文档的源代码。
此问题已得到解答。 @Berger 和@Andy Thomas 也对循环:
发生了什么提供了一些见解I think I have found the source code. exists uses a while loop based on a timeout value, among other things, so a subsequent call with the same parameter, could well return another result, see : https://github.com/RaiMan/SikuliX-2014/blob/master/API/src/main/java/org/sikuli/script/Region.java - @Berger
I see from another Sikuli source file that the default autoWaitTimeout is 63 seconds, making the race condition easy to observe. Two important lessons from this question are: 1) A default case is frequently useful, especially if it's not expected to occur -- and 2) If you want a single return value, make a single call. - @Andy Thomas
您没有默认案例。您使用的是 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 {
...
}
}
调试器可以帮助您发现此类问题。如果您在第一个条件下设置断点,您会看到循环正在考虑第一个文件。
这是因为你没有面对所有的可能性:
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);
}
使用相同的代码会抛出错误...
我认为 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);