1

I am new to C#, i am not able to make driver thread safe. I am able to open the two browser as soon as second browser opens, first driver lose its references.

below is my code i have three class

namespace TestAutomation{
[TestFixture]
[Parallelizable(ParallelScope.Children)]
public class UnitTest1 : Setup
{
    [Test, Property("TestCaseID","123")]
    public void TestMethod1(this IWebDriver driver1)
    {
        driver1.Navigate().GoToUrl("https://www.google.com");
        driver1.FindElement(By.Name("q")).SendKeys("test1");
        Thread.Sleep(10000);
    }


    [Test, Property("TestCaseID", "234")]
    public void TestMethod2()
    {
        driver.Navigate().GoToUrl("https://www.google.com");
        driver.FindElement(By.Name("q")).SendKeys("test2");
        Thread.Sleep(15000);

    }
}}

Setup Class

namespace TestAutomation{
public class Setup:WebDriverManager
{


    [SetUp]
    public void  setupBrowser()
    {

        driver = new ChromeDriver("C:\\Users\\Downloads\\chromedriver_win32");


    }

    [TearDown]
    public  void CloseBrowser()
    {
        driver.Close();
        driver.Quit();
       // driver.Close();
        //driver.Quit;
    }
}}

Webdrivermanager

namespace TestAutomation{
 public class WebDriverManager
{
    public  IWebDriver driver { get; set; }
}
}

i am looking for a solution like ThreadLocal injava where i can get and set the driver for each thread in the setup method

2 Answers 2

1

Remove the SetUp & TearDown Attributes for the methods and call them explicitly. When you use these method attributes, it starts sharing resources across tests in the same class or inherited classes.

The below solution works perfectly fine. I have developed a project in which you can execute browser tests in parallel (method level parallelization). You can modify the project as per your needs.

Project Link: www.github.com/atmakur

[TestFixture]
class Tests
{
        [Test]
        public void Test1
        {
            using(var testInst = new TestCase())
            {
            testInst
               .Init()
               .NavigateToHomePage();
            }
        }
}

public class TestBase:IDisposable
{
        private IWebDriver BaseWebDriver;
        private TestContext _testContext;
        public NavigatePage Init()
        {
            _testContext = TestContext.CurrentTestContext;
            BaseWebDriver = new ChromeDriver();
            .
            .
            .
        }

        public override void Dispose()
        {
            //Kill Driver here
            //TestContext instance will have the AssertCounts
            //But The Testcontext instance will have the result as Inconclusive.
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this should be accepted answer, it actually solves the posted issue- running several test methods of the same class in parallel. You have made a slight error: "using(var testInst = new TestCase())" should be "= new TestBase()". This little code snippet was enough for me, but i still wanted to see the whole project, but there is nothing at linked git repo that looks like c# nunit parallel library.
0

You are doing two contradictory things:

  1. Using a new browser for each test.
  2. Sharing the browser property between the tests.

You should do one or the other. If you want to create a new browser for each test, don't store a reference to it where the other test also accesses it.

Alternatively, use OneTimeSetUp and OneTimeTearDown and only create the browser once. However, in that case, you can't run the tests in parallel.

2 Comments

True, but i want to run test parallel that is my project requirement. so it is not possible to run the test cases parallel? Any other alternative to make all test cases run parallel and driver thread safe?
Then use my first suggestion. Stop using a common property to store the current driver, since you have two different "current" drivers.

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.