Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rupeshtiwari/ad9bea0cfbc8bb6c1db15997c30a025c to your computer and use it in GitHub Desktop.
Save rupeshtiwari/ad9bea0cfbc8bb6c1db15997c30a025c to your computer and use it in GitHub Desktop.
Running Multiple TestFixtures in One Browser Instance ( NUNIT, Visual Studio Test, Selenium, Webdriver )
//C# NUNIT 3.0
/*
* Problem: Running multiple TestFixtures within one browser instance.
* you can create one dedicated setup class say MySetupClass where
* on RunBeforeANyTests method set your broser instance in some shared static class
* Say BrowserProvider class then in all of your TestFixture you can get your current broser instance from
* This BrowserProvider class.
*/
// MySetupClass.cs
using System;
using NUnit.Framework;
namespace NUnit.Tests
{
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
}
[OneTimeTearDown]
public void RunAfterAnyTests()
{
browsers.Close();
}
}
}
}
Ref: https://github.com/nunit/docs/wiki/SetUpFixture-Attribute
// You can drive your browsers from appsettings. And then in each TestFixture you just use this broswer settings.
// One thing to note is dont quit browser on teardown.
// Browsers.cs
public class Browsers
{
private static IWebDriver webDriver;
private static string baseURL = ConfigurationManager.AppSettings["url"];
private static string browser = ConfigurationManager.AppSettings["browser"];
public static void Init()
{
switch (browser)
{
case "Chrome":
webDriver = new ChromeDriver();
break;
case "IE":
webDriver = new InternetExplorerDriver();
break;
case "Firefox":
webDriver = new FirefoxDriver();
break;
}
webDriver.Manage().Window.Maximize();
Goto(baseURL);
}
public static string Title
{
get { return webDriver.Title; }
}
public static IWebDriver getDriver
{
get { return webDriver; }
}
public static void Goto(string url)
{
webDriver.Url = url;
}
public static void Close()
{
webDriver.Quit();
}
}
Ref:https://blog.testproject.io/2017/02/09/cross-browser-testing-selenium-webdriver/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment