Running Selenium tests in multiple browsers one by one with C # NUnit

I am looking for a recommended / best way for Selenium tests to run in multiple browsers one by one. The website I'm testing is small, so I don't need a parallel solution yet.

I have the usual test setup methods with [SetUp] , [TearDown] and [Test] . Of course, SetUp creates an instance of the new ISelenium object with any browser that I want to test with.

So what I want to do is programmatically say: this test will run in Chrome, IE and Firefox browsers. How to do it?

EDIT:

This might help a bit. We use CruiseControl.NET to run NUnit tests after a successful build. Is there a way to pass the parameter to the NUnit executable, and then use this parameter in the test? Thus, we could start NUnit several times with different browser options.

+29
c # selenium nunit
Feb 17 '11 at 12:30
source share
8 answers

NUnit 2.5+ now supports Generic Test Fixtures, which makes testing across multiple browsers very easy. http://www.nunit.org/index.php?p=testFixture&r=2.5

Running the following example will run GoogleTest twice, once in Firefox and once in IE.

 using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.IE; using System.Threading; namespace SeleniumTests { [TestFixture(typeof(FirefoxDriver))] [TestFixture(typeof(InternetExplorerDriver))] public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new() { private IWebDriver driver; [SetUp] public void CreateDriver () { this.driver = new TWebDriver(); } [Test] public void GoogleTest() { driver.Navigate().GoToUrl("http://www.google.com/"); IWebElement query = driver.FindElement(By.Name("q")); query.SendKeys("Bread" + Keys.Enter); Thread.Sleep(2000); Assert.AreEqual("bread - Google Search", driver.Title); driver.Quit(); } } } 
+50
Oct 21 '11 at 20:20
source share

This is a recurring question and is addressed in several ways:

  • The Factory method creates your ISelenium object. You have a helper class with the static getSelenium method. This method reads in some external configuration, which has a property that defines the browser that you want as a string. In your getSelenium, you then configure your browser accordingly. here is a handy entry on using configuration files from NUnit http://blog.coryfoy.com/2005/08/nunit-app-config-files-its-all-about-the-nunit-file/

  • Others have success with introducing a browser through an IoC container. I really like this because TestNG works great with Guice in Java, but I'm not sure how easy it is to mix NUnit and Ninject, MEF, etc.

+5
Feb 18 '11 at 18:19
source share

This is basically just an extension of the response to alaning (October 21, 2011 at 20:20). My case was similar, I just didn’t want to start with a parameterless constructor (and thus use the default path to the driver executable files). I had a separate folder with drivers that I wanted to test, and this seems to work well:

 [TestFixture(typeof(ChromeDriver))] [TestFixture(typeof(InternetExplorerDriver))] public class BrowserTests<TWebDriver> where TWebDriver : IWebDriver, new() { private IWebDriver _webDriver; [SetUp] public void SetUp() { string driversPath = Environment.CurrentDirectory + @"\..\..\..\WebDrivers\"; _webDriver = Activator.CreateInstance(typeof (TWebDriver), new object[] { driversPath }) as IWebDriver; } [TearDown] public void TearDown() { _webDriver.Dispose(); // Actively dispose it, doesn't seem to do so itself } [Test] public void Tests() { //TestCode } } 

}

+5
Oct 02 '13 at 14:47
source share

I use the list of IWeb drivers to run tests in all browsers, line by line:

 [ClassInitialize] public static void ClassInitialize(TestContext context) { drivers = new List<IWebDriver>(); firefoxDriver = new FirefoxDriver(); chromeDriver = new ChromeDriver(path); ieDriver = new InternetExplorerDriver(path); drivers.Add(firefoxDriver); drivers.Add(chromeDriver); drivers.Add(ieDriver); baseURL = "http://localhost:4444/"; } [ClassCleanup] public static void ClassCleanup() { drivers.ForEach(x => x.Quit()); } ..and then am able to write tests like this: [TestMethod] public void LinkClick() { WaitForElementByLinkText("Link"); drivers.ForEach(x => x.FindElement(By.LinkText("Link")).Click()); AssertIsAllTrue(x => x.PageSource.Contains("test link")); } 

.. where I write my own methods WaitForElementByLinkText and AssertIsAllTrue to perform an operation for each driver and where something fails, I get a message that helps me determine which browsers may crash:

  public void WaitForElementByLinkText(string linkText) { List<string> failedBrowsers = new List<string>(); foreach (IWebDriver driver in drivers) { try { WebDriverWait wait = new WebDriverWait(clock, driver, TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(250)); wait.Until((d) => { return d.FindElement(By.LinkText(linkText)).Displayed; }); } catch (TimeoutException) { failedBrowsers.Add(driver.GetType().Name + " Link text: " + linkText); } } Assert.IsTrue(failedBrowsers.Count == 0, "Failed browsers: " + string.Join(", ", failedBrowsers)); } 

IEDriver is painfully slow, but it will have 3 major browsers that run side-by-side tests

+4
Jul 19 '12 at 10:22
source share

Well, one solution is to have shell tests that install the ISelenium object with different browsers. Then they pass this object to all other tests that use it instead of creating a new one, as it was before.

The downside is that I end up with one big test for each browser. Not the best solution. Still looking ...

EDIT:

Spent some more time on this. The solution I came up with is to have a text file in the solution that indicates the browser that will be used for testing. NUnit selects an option when creating a Selenium object.

I use CruiseControl.NET to run automated builds and tests. And instead of just running the test once, I configured it to run them twice. But before each test, I run a command line command that changes the browser in the text configuration file.

 <exec> <executable>cmd</executable> <buildArgs>/C echo firefox C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe > F:\...\selenium_browser.txt</buildArgs> </exec> <exec> <executable>F:\...\NUnit 2.5.7\bin\net-2.0\nunit-console.exe</executable> <baseDirectory>F:\...\bin\Debug</baseDirectory> <buildArgs>F:\...\...nunit /xml:"F:\CCXmlLog\Project\nunit-results.xml" /noshadow</buildArgs> <successExitCodes>0</successExitCodes> <buildTimeoutSeconds>1200</buildTimeoutSeconds> </exec> <exec> <executable>cmd</executable> <buildArgs>/C echo googlechrome C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe > F:\...\selenium_browser.txt</buildArgs> </exec> <exec> <executable>F:\...\NUnit 2.5.7\bin\net-2.0\nunit-console.exe</executable> <baseDirectory>F:\...\bin\Debug</baseDirectory> <buildArgs>F:\...\...nunit /xml:"F:\CCXmlLog\Project\nunit-results.xml" /noshadow</buildArgs> <successExitCodes>0</successExitCodes> <buildTimeoutSeconds>1200</buildTimeoutSeconds> </exec> 
+1
Feb 18 2018-11-12T00:
source share

This helped me solve a similar problem. How to run the nUnit test suite with two different settings?

Just install the various browsers in the setup method:]

+1
Jun 22 '11 at 9:45 a.m.
source share

I was interested to know about the same problem and finally I got a solution

Therefore, when you install the plugin, you can control in which browser the script should be tested.

Function example:

 @Browser:IE @Browser:Chrome Scenario Outline: Add Two Numbers Given I navigated to / And I have entered <SummandOne> into summandOne calculator And I have entered <SummandTwo> into summandTwo calculator When I press add Then the result should be <Result> on the screen Scenarios: | SummandOne | SummandTwo | Result | | 50 | 70 | 120 | | 1 | 10 | 11 | 

Implementation

 [Given(@"I have entered '(.*)' into the commentbox")] public void GivenIHaveEnteredIntoTheCommentbox(string p0) { Browser.Current.FindElement(By.Id("comments")).SendKeys(p0); } 

Additional Information

+1
Mar 11 '14 at 8:11
source share

There should be a better way, but you can use T4 templates to create repeating test classes for each browser - essentially copy automation and - debugging tests for each browser.

0
Feb 17 '11 at 12:41
source share



All Articles