java - 为什么不能在类级别使用 System.setProperty?

标签 java selenium testng

我曾尝试在主方法中使用System.setProperty,没有任何问题,但是当我切换到TestNG作为我的Selenium学习的一部分时,我意识到我们不能编写System.setProperty 在类级别。它应该位于方法级别或位于static block 中。 我只是想了解 Java 的哪些特性迫使我们这样做。

public class NewTest {
    public String baseUrl = "http://newtours.demoaut.com/";
    static {
        System.setProperty("webdriver.chrome.driver","D:\\paths\\chromedriver.exe");    
    }

    WebDriver driver = new ChromeDriver();

    @Test
     public void f1() {
      ...}
   }

在静态 block 之外写入此内容会显示编译错误,例如 “此行有多个标记,语法错误”

最佳答案

I just want to understand what is the feature of Java that is compelling us to do this.

“Java 的特性”是您只能在类级别编写方法和声明,而 System.setProperty() 两者都不是:它是方法调用。

关于java - 为什么不能在类级别使用 System.setProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647826/

相关文章:

java - 如何让h的required属性为:inputText depend on certain set of values from h:selectOneMenu?

java - Android ActionBar 找不到资源

java - 我该如何运行我的 GUI?或者更准确地说,我在 main 中放了什么?

testing - 如何在 Selenium IDE 中创建跨域测试?

python - 如何使用 Selenium 和 Python 在网站 https ://www. virustotal.com 中找到 shadow-root(打开)中的名字字段

java - 运行 selenium 脚本时无法从弹出框中加载扩展

java - TestNG - 如果测试花费太长时间,则执行方法或失败?

java - 更新 Intelij 后在线程 "main"java.lang.NoClassDefFoundError : org/testng/TestNG 中获取异常

java - 使用 TestNG 调用 JUnit 测试

java - Windows 上的 Java 准确 sleep