简体   繁体   中英

How to Run parallel Test Method in single class using Nunit, C# -ThreadSafe Driver

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

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.

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.
        }
}

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