英文:
How to create an instance of a static class so it can be called from a separate class?
问题
抱歉,这是一个非常基础的问题,但我在这个概念的基础上确实遇到了困难。
我创建了一个公共的静态类来设置浏览器:
private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
public static void setDriver(@NonNull String browserName) {
if (browserName.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driver.set(new ChromeDriver());
} else if (browserName.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver.set(new FirefoxDriver());
}
}
在这之下,我使用以下代码获取浏览器:
public static WebDriver getDriver() {
return driver.get();
}
这在生成单个实例时效果很好,但我需要并行运行,使用多个浏览器实例。
问题是,我不能只是将public static WebDriver getDriver()
更改为public WebDriver getDriver()
,因为这会影响到后续的所有内容。所以我需要实例化这个类。不幸的是,我尝试过的一切都或多或少地失败了。
在我的项目中,我如何实例化这个类以在其他类中使用?
英文:
apologies as this is a really basic problem, but Im really struggling with the basics of this concept.
I have created a public static class to set a browser
private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
public static void setDriver(@NonNull String browserName) {
if (browserName.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driver.set(new ChromeDriver());
} else if (browserName.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
driver.set(new FirefoxDriver());
}
}
below this, I get the browser using
public static driver getDriver() {
return driver.get();
}
This works fine when generating a single instance, but I need to run this in parallel using multiple instances of the browser.
The problem is, I cant just change public static driver getDriver()
to public driver getDriver()
because it breaks everything else down the line. So I need to instantiate an instance of this class. Unfortunately, everything I have tried completely fails one way or another.
How can I instantiate this class to use in other classes throughout my project?
答案1
得分: 1
你需要重写initialValue()
方法。类似于:
private static ThreadLocal<WebDriver> driver = new ThreadLocal<>() {
@Override protected WebDriver initialValue() {
return getDriver("chrome");
}
};
private static WebDriver getDriver(@NonNull String browserName) {
if (browserName.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
return new ChromeDriver();
} else if (browserName.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
return new FirefoxDriver();
} else {
return null;
}
}
英文:
You need to override the initialValue()
method. Something like:
private static ThreadLocal<WebDriver> driver = new ThreadLocal<>() {
@Override protected WebDriver initialValue() {
return getDriver("chrome");
}
};
private static WebDriver getDriver(@NonNull String browserName) {
if (browserName.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
return new ChromeDriver();
} else if (browserName.equalsIgnoreCase("firefox")) {
WebDriverManager.firefoxdriver().setup();
return new FirefoxDriver();
} else {
return null;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论