Here is how I declare firefox driver:
public static WebDriver driver = new FirefoxDriver();
I place the code above outside main and within my class (global)
Here is how I declare chrome driver:
System.setProperty("webdriver.chrome.driver", "/path/xxx/xxx/xx");
WebDriver driver = new ChromeDriver();
I place the code above in main
Here is the issue:
I want to make the ChromeDriver as a global but I NEED to set the property before doing so. But I place the 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?
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
. If I declare driver
in main, I need to continuously pass it as a parameter to all the functions. 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"));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句