在尝试并行运行测试时,如何正确传递webdriver?

时间:2018-06-09 13:27:10

标签: c# selenium selenium-webdriver nunit-3.0

我的工作要求我们编写一个自动化框架,其中包含带有c#/ nunit的selenium webdriver。我们目前正在使用nunit 3.0。我最初的想法是提取足够的框架内容,以便使用框架的团队不必担心设置/拆除webdriver等内容。

我们决定采用具有设置/拆卸的测试助手类的路线,该设置/拆卸将基于配置设置(铬,等等)实例化驱动程序,然后在每次测试后将其拆除。我们还添加了一个受保护的类驱动程序变量,只要调用setup,它就会被设置这样,每个测试类都可以扩展TestHelper,并且可以免费获得驱动程序。

当我们没有并行运行测试时,这非常有效但是一旦我们开始进行并行测试,驱动程序每次都会被覆盖(这是有道理的)。

我的问题是,有没有更好的方法可以让我们保持最初的想法,即在TestHelper中提供驱动程序并保持大量驱动程序操作不受测试本身的影响。我真的想在每个测试中都没有驱动程序声明/初始化,只是让测试套件测试特定的代码。

以下是一些类似于发生的测试类(我实际上无法发布生产代码,因为工作很有趣):

这是我的测试辅助类:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace AutomationTest
{
    class TestHelper
    {

        protected IWebDriver driver;

        [SetUp]
        public void startBrowser()
        {
            driver = new ChromeDriver("C:\\Users\\james\\Desktop");
        }

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

这是我的测试类(8个测试仅用于测试并行测试):

using NUnit.Framework;

namespace AutomationTest 
{
    [TestFixture]
    [Parallelizable(ParallelScope.All)]
    class ParallelTest : TestHelper
    {
        [Test]
        public void test1()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        }

        [Test]
        public void test2()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        }

        [Test]
        public void test3()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        }

        [Test]
        public void test4()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        }

        [Test]
        public void test5()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        }

        [Test]
        public void test6()
        {           
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        } 

        [Test]
        public void test7()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        }

        [Test]
        public void test8()
        {            
            driver.Navigate().GoToUrl("http://www.google.com");
            Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");   
        }
    }
}

1 个答案:

答案 0 :(得分:0)

using NUnit.Framework;

namespace AutomationTest 
{
class TestHelper
{

    //protected IWebDriver driver;
    protected void Browse(Action<IWebDriver> action)
    {
      var driver = new ChromeDriver("C:\\Users\\james\\Desktop");
      action(driver);
      driver.Close();
      driver.Quit();
    }
    [SetUp]
    public void startBrowser()
    {
        // driver = new ChromeDriver("C:\\Users\\james\\Desktop");
    }

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

[TestFixture]
[Parallelizable(ParallelScope.All)]
class ParallelTest : TestHelper
{
    [Test]
    public void test1()
    {   
        Browse( (driver) => {         
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    }

    [Test]
    public void test2()
    {   
        Browse( (driver) => {                  
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    }

    [Test]
    public void test3()
    {     
        Browse( (driver) => {                
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    }

    [Test]
    public void test4()
    {     
        Browse( (driver) => {                
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    }

    [Test]
    public void test5()
    {       
        Browse( (driver) => {              
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    }

    [Test]
    public void test6()
    {     
        Browse( (driver) => {               
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    } 

    [Test]
    public void test7()
    {     
        Browse( (driver) => {                
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");
        });
    }

    [Test]
    public void test8()
    {     
        Browse( (driver) => {                
          driver.Navigate().GoToUrl("http://www.google.com");
          Assert.AreEqual(driver.Title, "Google", "Stuff didn't work");   
        });
    }
}
}

这将为每个测试实例化一个新的驱动程序,因为您需要单独的浏览器驱动程序和浏览器实例,以便您的测试并行运行而不会干扰。