2

At the moment my tests are running only in Firefox, because that is the driver which initialize at last. I need to run my tests on both Firefox and Chrome. Parallel or serially. But it should be automatic.

Below is my example test-code:.

namespace TestOne
{
    [TestClass]
    public class UnitTest1
    {
        public static IWebDriver driver;

        [TestInitialize]
        public void BeforeEveryTest()
        {
            driver = new ChromeDriver();

            driver = new FirefoxDriver();
        }

        [TestMethod]
        public void NavigateToGoogle()
        {
            driver.Navigate().GoToUrl("http://google.com");
        }

        [TestCleanup]
        public void RunAfterEveryTest()
        {
            driver.Quit();
        }
    }
}

What are the options to run this test for both Chrome and Firefox? When using the C# testing tools (e.g. mstest.exe or vstest.console.exe)

  • 1
    Changed the question a bit to make it a bit more clear, not sure why this is being voted as unclear. The question is simple, how to run C# Selenium tests against multiple browsers. – Niels van Reijmersdal Mar 30 '17 at 9:12
  • 1
    It is almost funny that question from member of "closing mafia" is almost closed by "closing mafia". – Peter M. Mar 30 '17 at 12:55
  • I got the error NullReferenceException when using TestContext.Properties["browser"].ToString() on BaseClass. Could you have any oter solution? – Vinh Oct 17 '17 at 10:43
2

I think you can use a .RunSettings as described in the MSDN documentation.

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <TestRunParameters>
    <Parameter name="browser" value="chrome" />
  </TestRunParameters>
</RunSettings>

If you make a file for Firefox and Chrome you can run the tests from the command-line with VStest.Console like this:

vstest.console myTestDll.dll /Settings:Chrome.RunSettings /Logger:trx
vstest.console myTestDll.dll /Settings:Firefox.RunSettings /Logger:trx

Your test would look like this:

    [TestInitialize]
    public void BeforeEveryTest()
    {
        if ( TestContext.Properties["browser"].ToString() == "chrome" ) {
          driver = new ChromeDriver();
        } else {
          driver = new FirefoxDriver();
        }
    }
  • in here every time i have to change the browser name by editing the .RunSettings file.But i want it to be get automatically .One browser after another. – ChathuD Mar 30 '17 at 16:10
  • 1
    You make two run setting files. Add the two commands to a batch or powershell script. Then execute that one script to run them after each other. – Niels van Reijmersdal Mar 30 '17 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.