gpt4 book ai didi

java - 如果它不能为空,但需要初始化,我该怎么办?

转载 作者:行者123 更新时间:2023-11-30 03:13:26 25 4
gpt4 key购买 nike

我在访问某些 xpath 时遇到问题。我们使用混合测试框架。

public class logoutMenu {
public static void run(WebDriver driver) {
Properties prop = new Properties();
InputStream selectproductsidebarobjectrepository;
try {
selectproductsidebarobjectrepository = new FileInputStream(
"C:/thisisthepath/ObjectRepositories/SignInPageObjectRepository");
prop.load(selectproductsidebarobjectrepository);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement logoutNormal = driver.findElement(By.xpath(prop.getProperty("logoutnormal")));
Actions actions = new Actions(driver);
actions.moveToElement(logoutNormal).build().perform();
WebElement logoutHover = driver.findElement(By.xpath(prop.getProperty("logouthover")));
logoutHover.click();
WebElement logoutPushed = driver.findElement(By.xpath(prop.getProperty("logoutpushed")));
logoutPushed.click();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (selectproductsidebarobjectrepository != null) {
try {
selectproductsidebarobjectrepository.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

原始问题:它告诉我,如果 xpath 为空,则无法访问。由于以下行,它为 null:InputStream selectproductsidebarobjectrepository = null;。如果我去掉其中的 = null; 部分,我会得到一个不同的错误:“局部变量 selectproductsidebarobjectrepository 可能尚未初始化”,这也是有道理的。

我怎样才能使 selectproductsidebarobjectrepository 等于 not null?

新问题:清理了代码。摆脱了空。摆脱了额外的支架。仍然出现:

java.lang.IllegalArgumentException: Cannot find elements when the XPath expression is null.
at org.openqa.selenium.By.xpath(By.java:113)
at SelectProductSidebar.logoutMenu.run(logoutMenu.java:29)
at CommonFunctions.FunctionCheck.test(FunctionCheck.java:19)

这是调用此类的脚本:

public class FunctionCheck {
@Test
public void test() throws Exception {
WebDriver driver;
String baseUrl;
driver = new FirefoxDriver();
baseUrl = "http://www.MATTDAMON.com/";
driver.get(baseUrl + "MATT/MATT/MATT/MATT");
Thread.sleep(3000);
LoginPage.enterValidCredentials.run(driver);
SelectProductSidebar.logoutMenu.run(driver);

如果我搞砸的事情比最初多得多(可能是真的),这里是文本文件 selectproductsidebarobjectrepository:

logoutnormal=//img[contains(@src,'log_out_normal')]
logouthover=//img[contains(@src,'log_out_hover')]
logoutpushed=//img[contains(@src,'log_out_pushed')]

我可能正在做一些非常愚蠢的事情,但我对现在的情况完全视而不见。

最佳答案

去掉多余的支架后,这对我来说效果很好。

public class logoutMenu {
public static void run(WebDriver driver) {
Properties prop = new Properties();
InputStream selectproductsidebarobjectrepository;
try {
selectproductsidebarobjectrepository = new FileInputStream(
"C:/thisisthepath/ObjectRepositories/SignInPageObjectRepository");
prop.load(selectproductsidebarobjectrepository);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement logoutNormal = driver.findElement(By.xpath(prop.getProperty("log_out_normal")));
Actions actions = new Actions(driver);
actions.moveToElement(logoutNormal).build().perform();
WebElement logoutHover = driver.findElement(By.xpath(prop.getProperty("log_out_hover")));
logoutHover.click();
WebElement logoutPushed = driver.findElement(By.xpath(prop.getProperty("log_out_pushed")));
logoutPushed.click();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (selectproductsidebarobjectrepository != null) {
try {
selectproductsidebarobjectrepository.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

关于java - 如果它不能为空,但需要初始化,我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com