2

I've set up NUnit tests that runs on BrowserStack (set up from this example https://github.com/browserstack/nunit-browserstack )

Base class:

namespace Bloc.TestProject
{

    public class BrowserStackNUnitTest
    {
        protected IWebDriver driver;
        protected string profile;
        protected string environment;
        private Local browserStackLocal;

        public BrowserStackNUnitTest(string profile, string environment)
        {
            this.profile = profile;
            this.environment = environment;
        }

        [SetUp]
        public void Init()
        {
        ...

Browserstack tests:

namespace Bloc.TestProject
{
    [TestFixture("parallel", "chrome")]
    [TestFixture("parallel", "ie11")]
    [TestFixture("parallel", "iphoneX")]
    [TestFixture("parallel", "ipad")]
    [TestFixture("parallel", "samsungGalaxyS8")]
    [Parallelizable(ParallelScope.Fixtures)]
    public class OnTimeOnlineBooking : BrowserStackNUnitTest
    {
        WebDriverWait wait;
        public OnTimeOnlineBooking(string profile, string environment) : base(profile, environment)
        {

        }
... my tests ...

Local tests:

namespace Bloc.TestProject
{
    [TestFixture(typeof(PhantomJSDriver))]
    public class LocalBrowserTest<TWebDriver> where TWebDriver : IWebDriver, new()
{
    private IWebDriver driver;

    [SetUp]
    public void CreateDriver()
    {
        this.driver = new TWebDriver();
    }
    [TearDown]
    public void Cleanup()
    {
        driver.Quit();
    }


    ... my tests ... 

Is there any way I can structure my tests so that I can run a test and it'll run both locally and on browserstack without having to duplicate the test code?

olemarius
  • 1,124
  • 4
  • 17
  • 27

2 Answers2

0

I can Suggest a workaround for this case in Java, needs to change in C#.

write the code of browser-stack setup

public static browserstack_setup()  {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browserName", "chrome");
    caps.setCapability("version", "");
    caps.setCapability("platform", "windows");
    caps.setCapability("os_version", "8.1");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.get("http://www.google.com");
  }

then write code for launch browser

public void openbrowser(String browsername, String URL){
        if(browsername.equalsIgnoreCase("broserstack")){

        browserstack_setup();
        }else if(browsername.equalsIgnoreCase("CH")){
            System.setProperty("webdriver.chrome.driver",chromedriverpath);
            driver=new ChromeDriver();
            driver.get(URL);
        }
}
Kuldeep Yadav
  • 107
  • 1
  • 14
0

You may fetch the name from test context and based on that information, launch the local driver or remote driver. For the repo and you example, I assume the below code should work. You may also want to look at other APIs available under TestContext.CurrentContext.Test for your comparison operation

[SetUp]
        public void Init()
        {
         if(TestContext.CurrentContext.Test.Name == "MyTestName"){
             this.driver = new TWebDriver();
         }
         else{
          NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection;
          NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection;

          DesiredCapabilities capability = new DesiredCapabilities();

          foreach (string key in caps.AllKeys)
          {
            capability.SetCapability(key, caps[key]);
          }

          foreach (string key in settings.AllKeys)
          {
            capability.SetCapability(key, settings[key]);
          }

          String username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
          if(username == null)
          {
            username = ConfigurationManager.AppSettings.Get("user");
          }

          String accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
          if (accesskey == null)
          {
            accesskey = ConfigurationManager.AppSettings.Get("key");
          }

          capability.SetCapability("browserstack.user", username);
          capability.SetCapability("browserstack.key", accesskey);

          if (capability.GetCapability("browserstack.local") != null && capability.GetCapability("browserstack.local").ToString() == "true")
          {
            browserStackLocal = new Local();
            List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>();
            bsLocalArgs.Add(new KeyValuePair<string, string>("key", accesskey));
            browserStackLocal.start(bsLocalArgs);
          }

          driver = new RemoteWebDriver(new Uri("http://"+ ConfigurationManager.AppSettings.Get("server") +"/wd/hub/"), capability);
          }
        }

You need to ensure you have specified test fixtures for running on Local and Grid. For example, if test A on Safari on Grid and test A on local browser

BountyHunter
  • 1,413
  • 21
  • 35