如何创建静态类的实例,以便可以从另一个类中调用?

huangapple go评论107阅读模式
英文:

How to create an instance of a static class so it can be called from a separate class?

问题

抱歉,这是一个非常基础的问题,但我在这个概念的基础上确实遇到了困难。

我创建了一个公共的静态类来设置浏览器:

  1. private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();
  2. public static void setDriver(@NonNull String browserName) {
  3. if (browserName.equalsIgnoreCase("chrome")) {
  4. WebDriverManager.chromedriver().setup();
  5. driver.set(new ChromeDriver());
  6. } else if (browserName.equalsIgnoreCase("firefox")) {
  7. WebDriverManager.firefoxdriver().setup();
  8. driver.set(new FirefoxDriver());
  9. }
  10. }

在这之下,我使用以下代码获取浏览器:

  1. public static WebDriver getDriver() {
  2. return driver.get();
  3. }

这在生成单个实例时效果很好,但我需要并行运行,使用多个浏览器实例。

问题是,我不能只是将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

  1. private static ThreadLocal&lt;WebDriver&gt; driver = new ThreadLocal&lt;&gt;();
  2. public static void setDriver(@NonNull String browserName) {
  3. if (browserName.equalsIgnoreCase(&quot;chrome&quot;)) {
  4. WebDriverManager.chromedriver().setup();
  5. driver.set(new ChromeDriver());
  6. } else if (browserName.equalsIgnoreCase(&quot;firefox&quot;)) {
  7. WebDriverManager.firefoxdriver().setup();
  8. driver.set(new FirefoxDriver());
  9. }
  10. }

below this, I get the browser using

  1. public static driver getDriver() {
  2. return driver.get();
  3. }

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()方法。类似于:

  1. private static ThreadLocal<WebDriver> driver = new ThreadLocal<>() {
  2. @Override protected WebDriver initialValue() {
  3. return getDriver("chrome");
  4. }
  5. };
  6. private static WebDriver getDriver(@NonNull String browserName) {
  7. if (browserName.equalsIgnoreCase("chrome")) {
  8. WebDriverManager.chromedriver().setup();
  9. return new ChromeDriver();
  10. } else if (browserName.equalsIgnoreCase("firefox")) {
  11. WebDriverManager.firefoxdriver().setup();
  12. return new FirefoxDriver();
  13. } else {
  14. return null;
  15. }
  16. }
英文:

You need to override the initialValue() method. Something like:

  1. private static ThreadLocal&lt;WebDriver&gt; driver = new ThreadLocal&lt;&gt;() {
  2. @Override protected WebDriver initialValue() {
  3. return getDriver(&quot;chrome&quot;);
  4. }
  5. };
  6. private static WebDriver getDriver(@NonNull String browserName) {
  7. if (browserName.equalsIgnoreCase(&quot;chrome&quot;)) {
  8. WebDriverManager.chromedriver().setup();
  9. return new ChromeDriver();
  10. } else if (browserName.equalsIgnoreCase(&quot;firefox&quot;)) {
  11. WebDriverManager.firefoxdriver().setup();
  12. return new FirefoxDriver();
  13. } else {
  14. return null;
  15. }
  16. }

huangapple
  • 本文由 发表于 2020年9月23日 02:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64015846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定