1

I am only able to execute the 1st test methods. All subsequent test methods fail to execute even though the code is correct. See attached image for error message. Using test.sdk(15.8.0), NUNIT(3.10.1), Selenium.WebDriver(3.13.0), Selenium.IEDriverServer.win64(3.9.0),Selenium.InternetExplorer.WebDriver(3.3.0) enter image description here

using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Tests
{
    public class LandingPage
    {
        IWebDriver driver = new InternetExplorerDriver("C:\\Users\\M\\Desktop\\SL\\SLAutomation\\Core\\CoreLandingPage\\CoreLandingPage\\CoreLandingPage\\Drivers\\");

        [SetUp]
        public void Initialize()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            Console.WriteLine("Opened URL");
        }

        [Test]
        public void TestCase1()
        {
            Assert.That(2+2, Is.EqualTo(4));
            Console.WriteLine("Test case 1");
        }


        [Test]
        public void TestCase2()
        {
            Assert.That(2 * 2, Is.EqualTo(4));
            Console.WriteLine("Test case 2");
        }


        [TearDown]    
        public void CleanUp()
        {
            driver.Close();
            Console.WriteLine("Closed Browser");
        }
    }
}

1 Answer 1

2

You need to instantiate the driver in the method Initialize() tagged with [SetUp]. The error happens because at the end of TestCase1(), CleanUp() is called and the driver is closed. Then TestCase2() comes along and Initialize() is called but the driver no longer exists. You can verify this by commenting out the driver.Close(); line in CleanUp().

Your code should look more like

public class LandingPage
{
    IWebDriver driver;

    [SetUp]
    public void Initialize()
    {
        driver = new InternetExplorerDriver("C:\\Users\\M\\Desktop\\SL\\SLAutomation\\Core\\CoreLandingPage\\CoreLandingPage\\CoreLandingPage\\Drivers\\");
        driver.Navigate().GoToUrl("http://www.google.com");
        Console.WriteLine("Opened URL");
    }
    ...
Sign up to request clarification or add additional context in comments.

3 Comments

Why Initialize() is called after every test cases?I just need an open an instance of the browser and validate all test cases in that session, but the driver keeps closing on me after the first test case and all of my subsequent test cases fail! Could you write a sample where a 2nd test case is validated after the browser is opened by the first test case?
You want each test case to be run on a fresh browser instance. It makes your tests more resilient and allows for parallel execution of tests. Initialize() should be called BEFORE each test and CleanUp() should be called AFTER each test and should close the browser. From what you have posted, you shouldn't need to change anything else. Having said that, you don't have any code referencing Selenium in your tests. You would want your test to start where Initialize() left off.
Appears redundant, but if it's good practice then i will adjust to it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.