问题
I'm currently building an automation project using C#, nunit
and Selenium and I'm trying to have a setup class that will initialize the selenium webdriver before any TestFixture
will run and after all of them have ended and trying to get that webdriver from the TestFixture
in each TestFixture
OneTimeSetUp
attribute, can it be done or should I change the selenium class to static in order to be able to get the driver field in each TestFixture
setup?
so the basic structure is this:
[SetUpFixture]
public class Test
{
[OneTimeSetUp]
public void Init()
{
_driver = new Driver();
}
}
[TestFixture]
public class FirstTest
{
[OneTimeSetUp]
public void Init()
{
xxxxxxxxxx - here I need to initialize a class with the driver from the setup class
}
}
回答1:
As things currently stand, the only way NUnit can do this for you is if you use a static. But this won't work well if you are testing multiple drivers in parallel.
A true solution (requiring a new NUnit feature) would be to allow setup fixtures to save info in the TestContext, which your individual fixture could then access.
A workaround would be to create a new driver in the fixture onetimesetup, but only if it had not already been created. That too would require a static, but you could set it up so that there was a separate static for each driver type you support.
回答2:
It could probably be done by Re-using a remote web driver. You will have to fetch session id of the session and extend a class to Reuse Remote Webdriver. Then you can use the URL and session id to connect to an existing session for every test class.
回答3:
I have a similar requirement to this using Java and TestNG but the general idea structure might help.
What I did was to extend each of my test classes from a setup driver type class. So the setup driver class would choose the browser and options, navigate to the site (or first page of what you need to do) and then the test class take over. However I can get away with this by using @beforesuite and @aftersuite keywords to keep it at a "higher level" than the tests so it keeps the same instance.
来源:https://stackoverflow.com/questions/42689275/passing-an-argument-to-testfixture-onetimesetup-from-setupfixture