【发布时间】:2020-09-15 06:12:19
【问题描述】:
目标:当调用“initializeDriver”时,我不想在另一个类中继续抛出 IOexception。
如何正确地在我的代码中实现“try”和“catch”?这是我的尝试,但是,它无法正常工作。我试过环顾四周,但我可能没有正确理解它。
这是我的 gitHub 的链接,以防有人想查看:https://github.com/intuitive86/Sample_Driver_Test
package resources;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Base {
public WebDriver driver;
protected Properties dataProperties;
public WebDriver initializeDriver() throws IOException {
// Create global property file
dataProperties = new Properties();
InputStream dataPropertiesInputStream = null;
try{
InputStream = getClass().getClassLoader().getResourceAsStream("data.properties");
dataProperties.load(dataPropertiesInputStream);
} catch (IOException e) {
e.printStackTrace();
}
String browserName = dataProperties.getProperty("browser");
System.out.println(browserName);
if (browserName.equals("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
} else if (browserName.equals("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
【问题讨论】:
标签: java selenium intellij-idea webdriver browser-automation