5

I have a method that creates 2 remote web drivers. one with chrome and another with firefox:

Driver.cs

 public class Driver
{

    public static IWebDriver Instance { get; set; }

    public static void Initialize()
    {
        DesiredCapabilities[] browsers = {DesiredCapabilities.Firefox(),DesiredCapabilities.Chrome()};
       foreach (DesiredCapabilities browser in browsers)
        {
            if (browser == DesiredCapabilities.Chrome()) 
                {
                var browser = DesiredCapabilities.Chrome();
                System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/Documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe");
                ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" };
                browser.SetCapability(ChromeOptions.Capability, options);
                Console.Write("Testing in Browser: " + browser.BrowserName);

                Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);

            } else {
               Console.Write("Testing in Browser: "+ browser.BrowserName);
               Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);
           }   
        }
        Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
    }

Then I have a Test class:

[TestClass]
public class LoginTests
{
    [TestInitialize]
    public void Init()
    {
       Driver.Initialize();
    }

    [TestMethod]
    public void Failed_login()
    {
        LoginPage.GoTo();
        LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

        Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect");
    }


    [TestMethod]
    public void Admin_User_Can_Login()
    {
        LoginPage.GoTo();
        LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

        Assert.IsTrue(HomePage.IsAt, "Failed to login.");
    }

    [TestCleanup]
    public void Cleanup()
    {
      Driver.Close();

    }
}

}

The problem is when Driver.Intialize gets called it doesn't run both chrome and firefox. What I want to happen is that when the Init method gets called it starts both browsers and runs the Test Methods in each browser.

3 Answers 3

13

The way I am currently doing this is with NUnit. I had the same problem and could not find a good way to do it with MSTest.

What I am doing would be:

As you can see I just create a new TestFixture for each browser.

[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(FirefoxDriver))]

public class LoginTests<TWebDriver> where TWebDriver : IWebDriver, new()
{


[SetUp]
public void Init()
{
   Driver.Initialize<TWebDriver>();
}

[Test]
public void Failed_login()
{
    LoginPage.GoTo();
    LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

    Assert.IsTrue(LoginFail.IsAt, "Login failure is incorrect");
}


[Test]
public void Admin_User_Can_Login()
{
    LoginPage.GoTo();
    LoginPage.LoginAs("user").WithPassword("pass").WithDatasource("db").Login();

    Assert.IsTrue(HomePage.IsAt, "Failed to login.");
}

[TearDown]
public void Cleanup()
{
  Driver.Close();

}
}
}

Driver Class

 public class Driver<TWebDriver> where TWebDriver : IWebDriver, new()
 {

    public static IWebDriver Instance { get; set; }

    public static void Initialize()
    {
        if (typeof(TWebDriver) == typeof(ChromeDriver))
        {


         var browser = DesiredCapabilities.Chrome();
                System.Environment.SetEnvironmentVariable("webdriver.chrome.driver", "C:/Users/jm/Documents/Visual Studio 2013/Projects/VNextAutomation - Copy - Copy (3)/packages/WebDriverChromeDriver.2.10/tools/chromedriver.exe");
                ChromeOptions options = new ChromeOptions() { BinaryLocation = "C:/Users/jm/AppData/Local/Google/Chrome/Application/chrome.exe" };
                browser.SetCapability(ChromeOptions.Capability, options);
                Console.Write("Testing in Browser: " + browser.BrowserName);



                Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);

            } else {
               Console.Write("Testing in Browser: "+ browser.BrowserName);
               Instance = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), browser);
           }   
        }
        Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
    }
}

I have tried to fit it around your code.

Sign up to request clarification or add additional context in comments.

4 Comments

This is amazing! thanks going to give it a try. Ill post back in a bit. Thanks Tidus.
Thats fine, If i have helped please feel free to upvote. If you have any problems please let me know so I can assist.
@JamieRees Does it allow you to do parallel execution or it's sequential only? Just for my clarification
@JamieRees thank you Jamie this solved my problem im up and running. I made this the accepted answer. I couldnt upvote didnt have enough reputation
3

If you wanted to be able to specify a browser to run a test on an adhoc basis rather than all of them every time using TestFixtures, Richard Bradshaw has an excellent tutorial here.

The idea is to use an app config file (and Factory Pattern) that houses values such as the browser, version, platform, selenium hub and port information (as well as any other data you might require in your Hub/Node implementation on Grid) and pull it in at the time of testing to create a WebDriver instance. You can then modify this file in between tests to spin up a WebDriver of a different type if necessary.

We use this to run tests sequentially with NUnit and it has proven quite effective.

2 Comments

This is a great response and exactly what i"m looking to do. -- I'm just curious if you have en example of you you have used this in your sequential NUnit tests?
Sure, In the article, there's a link to the github repo with some examples. Here's one: github.com/FriendlyTester/WebDriverFactoryExample/blob/master/…
0

an easier and a little bit more straight forward solution using NUnit:

namespace Test
{
    //add all browser you want to test here
    [TestFixture(typeof(FirefoxDriver))]
    [TestFixture(typeof(ChromeDriver))]
    public class SkillTest<TWebDriver> where TWebDriver : IWebDriver, new()
    {
        private IWebDriver _driver;
        private string driverPath;
        
        [SetUp]
        public void Init()
        {
            _driver = new TWebDriver();
            _driver.Navigate().GoToUrl("https://localhost:5001/");
        }

        [Test]
        public void your_test_case()
        { 
        //some test logic
        } 


        [TearDown]
        public void CleanUp()
        {
            _driver.Quit();
            _driver.Dispose();
        }
    }
}

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.