【发布时间】:2019-06-02 22:43:25
【问题描述】:
我正在自动进行位置选择测试。选项将在下拉菜单中。下拉菜单中有三个选项(位置)。根据所选位置,页面上的数据将相应更改。我正在尝试将位置存储在属性中并从中检索。属性文件中的位置如下所示:
location=UK
获取位置属性的代码:
Properties prop = new Properties();
prop.load(f);
setLocation(prop.getProperty("location"));
当我尝试打印 location 属性时,会显示正确的值。
System.out.println(prop.getProperty("location")); //The value UK is displayed
setLocation()方法代码为:
wait.until(ExpectedConditions.visibilityOf(selectLocation));
selectLocation.click(); //now the dropdown will be displayed
Actions action = new Actions(driver);
if(location == "UK") {
wait.until(ExpectedConditions.visibilityOf(ukLocation));
action.moveToElement(ukLocation).click().build().perform();
}
else if(location == "US") {
wait.until(ExpectedConditions.visibilityOf(usLocation));
action.moveToElement(usLocation).click().build().perform();
}else {
System.out.println("didn't get the location");
}
当我运行代码时
"didn't get the location"
正在显示。 我已经实现了 URL 的属性并且它有效。在这里,我可以获取 location 属性并将其显示在控制台上,但问题出现在字符串比较中。如果我将字符串作为位置传递,则 setLocation() 方法有效:
setLocation("UK");
【问题讨论】:
-
为
setLocation()发布您的代码。我的猜测是您没有设置在您测试if(location == "UK")的范围内可访问的变量location。要查看这一点,请在if之前添加System.out.println(location);... 可能是null或“”。顺便说一句,当您发布问题时,您总是希望为您使用的编程语言添加标签。 -
我发布的代码是 setLocation() 的。是的,我确实在 if 之前放置了 'System.out.println(location) 并且它正在打印我在描述中也提到过的值。
-
代码在使用 equals() 方法时有效。
标签: selenium selenium-webdriver properties-file