I'm using Visual Studio 2012 test framework (I guess that's MSTest). I have some code that I need to run once and only once before the various testmethods run. ClassInitialize seemed perfect until I learned that it must be static.
First, I have an instance variable for ChromeDriver:
private ChromeDriver driver;
[ClassInitialize]
public static void Initialize() {
ChromeOptions options = new ChromeOptions();
options.AddArgument("test-type");
options.AddArgument("start-maximized");
options.LeaveBrowserRunning = true;
driver = new ChromeDriver(@"C:\MyStuff", options);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(4));
}
driver
I'm not sure whether it's really the best approach, but you could make it a static variable - it's fine to access static variables from instance methods (your tests) after all.
Note that this could cause problems if you try to run your tests in parallel, however. It's probably worth investigating what the test instance lifecycle is - where you can use one instance for multiple tests, in which case initializing the instance variable in the constructor might be another reasonable approach.
(Unless it actually takes a long time to initialize the driver, I'd be tempted to create it on a per-test basis anyway...)