[英]How do I declare a driver as global?
Here is how I declare firefox driver: 这是我声明firefox驱动程序的方式:
public static WebDriver driver = new FirefoxDriver();
I place the code above outside main and within my class (global) 我将代码放在main之外和我的课程中(全局)
Here is how I declare chrome driver: 这是我声明Chrome驱动程序的方式:
System.setProperty("webdriver.chrome.driver", "/path/xxx/xxx/xx");
WebDriver driver = new ChromeDriver();
I place the code above in main 我将上面的代码放在main中
Here is the issue: 这是问题所在:
I want to make the ChromeDriver as a global but I NEED to set the property before doing so. 我想将ChromeDriver设置为全局属性,但需要设置此属性。 But I place the
System.setProperty("xx","xx");
但是我放置了
System.setProperty("xx","xx");
within the main body. 在主体内。 Cuz it gives error when placed outside.
因为放在外面时会产生错误。
Here is a user trying to do the same thing as me. 这是一个用户尝试与我做相同的事情。 Trying to run different browsers using the same driver : How to run Selenium tests in multiple browsers for cross-browser testing using Java?
尝试使用同一驱动程序运行不同的浏览器: 如何在多个浏览器中运行Selenium测试,以使用Java进行跨浏览器测试?
The answer is involves declaring the driver in the main body and not as a constant before. 答案是在主体中声明驱动程序,而不是将其声明为常量。
My issue: All functions need driver declaration from before. 我的问题:所有功能都需要从前声明驱动程序。 Calling functions which use
driver
. 调用使用
driver
函数。 If I declare driver
in main, I need to continuously pass it as a parameter to all the functions. 如果我在main中声明
driver
,则需要将其作为参数连续传递给所有函数。 I do not wish to do that. 我不想这样做。 Here is an example function
这是一个示例功能
public static void a(){
driver.findElement(By.id("hi"));
}
How about something like: 怎么样:
class SomeTest {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("key", "value");
driver = new ChromeDriver();
}
public static void a() {
driver.findElement(By.id("hi"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.