2017-09-13 45 views
1

我是C#的新手,我无法使驱动程序线程安全。一旦第二个浏览器打开,我就可以打开这两个浏览器,第一个驱动程序将失去它的引用。如何使用Nunit,C#-ThreadSafe驱动程序在单个类中运行并行测试方法

下面

是我的代码有三个类

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); 

    } 
}} 

设置类

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

我期待像ThreadLocal的injava一个解决方案,我可以得到和在设置方法中为每个线程设置驱动程序

回答

0

你做两件矛盾的东西:

  1. 使用每个测试一个新的浏览器。
  2. 在测试之间共享浏览器属性。

你应该做一个或另一个。如果您想为每个测试创建一个新的浏览器,请不要在其他测试也访问它的地方存储引用。

或者,也可以使用OneTimeSetUpOneTimeTearDown并且只创建一次浏览器。但是,在这种情况下,您无法并行运行测试。

+0

真的,但我要运行的测试并行这是我的项目需求。所以不可能并行运行测试用例?使所有测试用例平行运行并且驱动程序线程安全的其他选择? – user1

+0

然后使用我的第一个建议。由于您有两个不同的“当前”驱动程序,请停止使用公共属性来存储当前驱动程序。 – Charlie

0

删除SetUp &方法的TearDown属性并显式调用它们。当您使用这些方法属性时,它会开始在同一个类或继承类中的测试之间共享资源。

下面的解决方案工作得很好。我开发了一个项目,您可以在其中并行执行浏览器测试(方法级并行)。您可以根据需要修改项目。

项目链接: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. 
     } 
} 
相关问题