简体   繁体   中英

How do i use more than one browser in Nunit/Selenium GRID/C# Setup

I have a Selenium GRID Setup with the various browsers on it (IE6, 7, 8, FF 3.5.6) written in C# and individually they work fine. I also have a set of Selenium Tests setup and they also work fine with the envirments that i pass to them. What i am asking for is a way to prgrammatically set the differant unit tests to cycle through all of the browsers available to it on the Selenium GRID.

There are not that many browsers, so things like a list or array of browsers is fine but i can't figure a way to have the Setup and TearDown cycle through the browsers. I am using C# with NUnit along with Selenium Grid and 3 Selenium RCs hooked upto it.

I Don't even mind changing to something like MbUnit if it meant I could cycle through the browsers.

Many thanks

一个(相当丑陋)的选项可能是在目标浏览器中传递的测试方法上使用RowTest扩展 - 以设置污染实际测试方法并且可能减慢整个测试套件的速度。

If you are using MbUnit , you can bind a the Factory attribute to a variable. Then have the Data Factory return once of each type of browser you want to automate with. It will execute the tests once per browser.

http://www.gallio.org/wiki/doku.php?id=mbunit:data-driven_testing:factory

If you use NUnit, you can specify parameterized TextFixtures with all browsers you want at base test class:

namespace Tests
{
    [TestFixture("*firefox")]
    [TestFixture("*iexplore")]
    public abstract class Test
    {
        private static string _browser;

        protected Test()
        {
        }

        protected Test(string browser)
        {
            SetBrowser(browser);
        }        

        public static void SetBrowser(string browser)
        {
            _browser = browser;
        }

        [SetUp]
        public virtual void Setup()
        {
            Selenium = new DefaultSelenium(localhost, 5555, _browser, "http://www.google.com/");
            Selenium.Start();
        }

        [TearDown]
        public virtual void TearDown()
        {
            Selenium.Stop();
        }
    }
}

And tests itself will be something like that:

namespace Tests
{
    [TestFixture]
    public class Test1 : Test
    {
        public Test1(string browser)
        {
            SetBrowser(browser);
        }

        [Test]
        public void FirstTest()
        {
            ...
        }
   }
}

2) You can specify browsers via PNunit . Cons: each test should be mentioned in test.conf file. Pros: all specified browsers will be run in parallel. Example of test.conf file with one test specified for two browsers:

<TestGroup>
  <ParallelTests>  
    <ParallelTest>
      <Name>Tests</Name>
        <Tests>

          <TestConf>
            <Name>Test1FF</Name>
            <Assembly>Test.dll</Assembly>
            <TestToRun>Test.Tests.Test1</TestToRun>
            <Machine>localhost:8080</Machine>
            <TestParams>
              <string>*firefox</string>
            </TestParams>
          </TestConf>

          <TestConf>
            <Name>Test1IE</Name>
            <Assembly>Test.dll</Assembly>
            <TestToRun>Test.Tests.Test1</TestToRun>
            <Machine>localhost:8080</Machine>
            <TestParams>
              <string>*iexplore</string>
            </TestParams>
          </TestConf>

        </Tests>
      </ParallelTest>
    </ParallelTests>
</TestGroup>

And base test class will be something like that:

using NUnit.Framework;
using PNUnit.Framework;

namespace Tests
{
    [TestFixture]
    public class Test
    {
        private string browser;

        protected Test()
        {
        }     

        [SetUp]
        public virtual void Setup()
        {
            browser = PNUnitServices.Get().GetTestParams();
            Selenium = new DefaultSelenium(localhost, 5555, browser, "http://www.google.com/");
            Selenium.Start();
        }

        [TearDown]
        public virtual void TearDown()
        {
            Selenium.Stop();
        }
    }
}

3) You can specify browsers in app.config and change it via TeamCity. Didn't investigate this solution, so can't give you an example. Hopes first two solutions will help.

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