简体   繁体   中英

Using ChromeDriver across SpecFlow Tests

So as we know when you use SpecFlow if you reuse a step from another test it automatically pulls it in and reuses it... however, I have the issue whereby Test A logs me in and test B logs in and confirms the home page is correct but as test A is initialising ChromeDriver when I come to use Test B my Driver wants to open another webpage causing the test to fail as its open an empty webpage.

My question is - How do I use the driver without it opening another instance of Chrome. Here is what I have code wise for my 'generic login:'

        private LandingPageCode landingPage;
        private HomePageCode HomePage;

        [Given(@"I have entered my username, password selected login")]
        public void GivenIHaveEnteredMyUsernamePasswordSelectedLogin()
        {
            driver = new ChromeDriver();
            driver.Url = ("my URL");
            landingPage = new LandingPageCode(driver);
            HomePage = new HomePageCode(driver); 

The code I have on test B which validates the homepage once logged in:

    {
        private ChromeDriver driver;
        private HomePageCode HomePage;
        private LandingPageCode landingPage;


        [Given(@"Successfully log into Cal's website (.*)")]
        public void GivenSuccessfullyLogIntoOptix(Decimal p0)
        {
            driver = new ChromeDriver();
            HomePage = new HomePageCode(driver);
            landingPage = new LandingPageCode(driver);

            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            Assert.AreEqual("HomePage", driver.Title);

You could remove your driver code from your tests and set up a framework for your code to run on. Using NUnit, you could develop a framework for yourself to run the tests in parallel. There are tones of online tutorials for this. [ https://nunit.org/][1]

You could create a driver.cs class that looks like this which pulls the base URL from a config class.:

    public static class Driver
{
    public static IWebDriver driver = new ChromeDriver();

   public static void InitializedDriver()
    {
        Driver.driver.Navigate().GoToUrl(Config.BaseURL);
        Driver.driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    }
}

Then in your test class, you can use OneTimeSetUp to initialise your web driver:

    [OneTimeSetUp]
   public void Initialize()
    {
        Driver.InitializedDriver();
    }

After your test codes, you can then tear down using:

        [OneTimeTearDown]
    public void CleanUp()
    {
        Driver.driver.Quit();

    }

This would allow your tests to run on the same Chrome instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM