java 如何使用 System.set... 在类级别初始化 chrome 驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15757674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Initialise the chrome driver at the class level, using System.set...?
提问by user2172723
I have been using Firefox to run my test cases. But now I want to use Chrome. I want to initialize chrome at the class level, just like I was using Firefox. But setting system property at class level is giving error, what can I do? Using properties file would work, if yes, how??
我一直在使用 Firefox 来运行我的测试用例。但现在我想使用 Chrome。我想在类级别初始化 chrome,就像我使用 Firefox 一样。但是在类级别设置系统属性会出错,我该怎么办?使用属性文件会起作用,如果是,如何?
public class BaseClass {
System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");
public static WebDriver driver = new ChromeDriver();
public void test(){
driver.get("http://asdf.com");
----
---
}
}
回答by jabbrwcky
You cold do it with a static initializer block like this:
您可以使用像这样的静态初始化程序块来完成它:
public class BaseClass {
static {
System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");
}
protected WebDriver driver = new ChromeDriver();
@Test
public void test(){
driver.get("http://asdf.com");
}
}
As you have not stated which test framework you are using you might do it like this in TestNG (which I would recommend anyway):
由于您没有说明您使用的是哪个测试框架,您可以在 TestNG 中这样做(无论如何我都会推荐):
public class BaseClass {
@BeforeSuite
public void setupChromeDriver() {
System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");
}
public static WebDriver driver = new ChromeDriver();
public void test(){
driver.get("http://asdf.com");
}
}
The @BeforeSuite annotation ensures that the method is executed before the first test of a test suite is run, so this should be early enough anyway.
@BeforeSuite 注释确保该方法在运行测试套件的第一个测试之前执行,因此无论如何这应该足够早。
回答by Sriram
Please declare it this way.. it should work
请以这种方式声明..它应该可以工作
public class abcd {
public static WebDriver driver;
@BeforeMethod
public static void start()
{
File file = new File("D:/abcd/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
}
}
It should work.. I had the same error. but when u initialize this way it works. Please try and let us know.
它应该工作..我有同样的错误。但是当你以这种方式初始化时,它就起作用了。请尝试让我们知道。
And also if u dont want to close the browser session try using @BeforeClass and @AfterClass. It runs once before the entire test once
而且,如果您不想关闭浏览器会话,请尝试使用@BeforeClass 和@AfterClass。它在整个测试之前运行一次
回答by Hussain Akhtar Wahid 'Ghouri'
System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");
this line should come inside a method , you cant use it directly inside your class body
这一行应该出现在一个方法中,你不能直接在你的类体内使用它
回答by Abhijeet Vaikar
Why not try initializing Chrome Driver in a @BeforeTest method in your base class. What I have done is like this:
为什么不尝试在基类中的 @BeforeTest 方法中初始化 Chrome 驱动程序。我所做的是这样的:
public class BaseTest {
/*
*
* This is a base class for all Test classes that we'll create to write tests in.
* A test-data set will belong to one/set of tests.
*/
protected WebDriver driver;
protected CustomLogger logger;
protected DependencyChecker dcheck;
protected TestDataReader td;
protected PropReader p;
protected HashMap<String, String> testDataMap;
private String testDataFilePath;
protected BaseTest(String testDataFilePath)
{
this.testDataFilePath = testDataFilePath;
p = new PropReader("environmentConfig.properties");
}
@BeforeTest(description="Preparing environment for the test..")
public void prepareTest()
{
//other code
System.setProperty(p.get("chromeDriverName"),p.get("chromeDriverPath"));
File chrome = new File("/usr/bin/google-chrome");
ChromeOptions options = new ChromeOptions();
options.setBinary(chrome);
logger.log("Launching browser..");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//other code
}
}
I don't know why would you want to initialise it at the class level. The above code works perfectly fine.
我不知道你为什么要在类级别初始化它。上面的代码工作得很好。