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

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

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&lt;WebDriver&gt; driver = new ThreadLocal&lt;&gt;();

public static void setDriver(@NonNull String browserName) {
    if (browserName.equalsIgnoreCase(&quot;chrome&quot;)) {
       WebDriverManager.chromedriver().setup();
       driver.set(new ChromeDriver());
    } else if (browserName.equalsIgnoreCase(&quot;firefox&quot;)) {
       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&lt;WebDriver&gt; driver = new ThreadLocal&lt;&gt;() {
    @Override protected WebDriver initialValue() {
         return getDriver(&quot;chrome&quot;);
     }
};

private static WebDriver getDriver(@NonNull String browserName) {
    if (browserName.equalsIgnoreCase(&quot;chrome&quot;)) {
        WebDriverManager.chromedriver().setup();
        return new ChromeDriver();
    } else if (browserName.equalsIgnoreCase(&quot;firefox&quot;)) {
        WebDriverManager.firefoxdriver().setup();
        return new FirefoxDriver();
    } else {
        return null;
    }
}

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:

确定