3

I would like to be able to run my unit tests across different browsers (FF/IE/Chrome) without having to modify the code to change what webdriver I am using. I am new to selenium testing and would appreciate any recommendations.

I would like to be able to do the following:

  1. run a particular test against a particular browser
  2. run all tests against a particular browser
  3. run all tests against all browsers

Here are some options I have considered but they don't meet my all my needs.

  1. Ask the user (via a Dialog window) which browser to run the test against
    • This approach meets condition #1 listed above but not 2 and 3.
    • This approach would cause the user to be prompted for each test so it does not meet condition #2
  2. Store the default browser in a config file.
    • The config file can be easily edited with a text editor
    • This approach meets condition #1 and #2 but requires manually editing the config file before running the tests.
4
  • Are you using NUnit? Commented Feb 18, 2015 at 22:39
  • No but I can look into using it if it's a better alternative. I am currently using Visual Studio Unit Testing to run my tests. Commented Feb 18, 2015 at 22:47
  • 1
    See this. However, this is a way to sequential execution not parallel and does not meet your requirement necessarily Commented Feb 18, 2015 at 22:50
  • I don't see where he wanted to run the test in parallel, but if he does, he can find my solution here: blog.dmbcllc.com/… Commented Feb 20, 2015 at 23:01

2 Answers 2

4

Here is one easy solution for your problem. You can use NUnit to execute selenium tests on multiple browsers. All you need to do is download Nunit from NuGet and reference it in your project. Here is a sample code which works just fine.

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

namespace MultipleBrowserTesting
{
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class BlogTest<TWebDriver> where TWebDriver : IWebDriver, new()
{
    private IWebDriver _driver;

    [Test]
    public void Can_Visit_Google()
    {
        _driver = new TWebDriver();

        // Navigate
        _driver.Manage().Window.Maximize();
        _driver.Navigate().GoToUrl("http://www.google.com/");
    }

    [TestFixtureTearDown]
    public void FixtureTearDown()
    {
        if (_driver != null) 
            _driver.Close();
    }
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using Visual Studio, you can make use of ordered tests. What I did, I have created three separate test cases namely SetIE, SetChrome and SetFireFox.

 [TestMethod]
    public void SetIE()
    {
        Browser.Type = "IE";

    }


    [TestMethod]
    public void SetFireFox()
    {
        Browser.Type = "FF";

    }


    [TestMethod]
    public void SetChrome()
    {

        Browser.Type = "CR";
    }

These methods just set a string in a class variable and do nothing else. Create a method to initialize your webDriver

   IWebDriver WebDriver = null;

public static void InitializeDriver(TestContext t)
    {
      if (WebDriver == null)
            {

                string DRIVER_PATH = @"C:\automation\driversFolder\";

                switch (Browser.Type)
                {
                    case "IE":

                        WebDriver = new InternetExplorerDriver(DRIVER_PATH);                         

                        break;

                    case "FF": 

                        WebDriver = new FirefoxDriver();
                        break;


                    case "CR":

                        WebDriver = new ChromeDriver(DRIVER_PATH);
                        break;

                    default:
                        WebDriver = new FirefoxDriver();                           
                        break;

                }

            }

    }

See this blog post which more or less describes this solution.

Now what you have to do is to create an ordered test for IE. Put the first test case SetIE there. and below that put your other test case like login etc. Now you have one suite ready to execute your test cases in IE. Similary create ordered tests for Chrome and FireFox. After that , create a fourth orderedtest named "All Browsers". Inside it ,place all of your 3 ordered tests.

After that , here is what you can do now. A) If you want to run a single test case against a specific browser, just change the browser name in your class and run that test case.

B) if you want to run all test against a particular browser, just execute the ordered test of that particular broser .

C) If you want to run all tests on all browsers, run your fourth ordered test.

I hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.