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)

asked Mar 30, 2017 at 7:27
3
  • 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. Commented Mar 30, 2017 at 9:12
  • 1
    It is almost funny that question from member of "closing mafia" is almost closed by "closing mafia". Commented Mar 30, 2017 at 12:55
  • I got the error NullReferenceException when using TestContext.Properties["browser"].ToString() on BaseClass. Could you have any oter solution? Commented Oct 17, 2017 at 10:43

1 Answer 1

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();
 }
 }
answered Mar 30, 2017 at 9:21
2
  • 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. Commented Mar 30, 2017 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. Commented Mar 30, 2017 at 18:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.