Search code examples
c#seleniumbrowserstack

BrowserStackLocal - could not connect to log files


I am trying to run automated selenium tests remotely using Browser stack local interacting with localhost. I get the following error:

Eror while executing BrowserStackLocal start {"state":"disconnected","pid":7512,"message":{"genre":"error","message":"Could not connect to Files!"}}

then I specify a log file, it then says:

BrowserStackLocal start {"state":"disconnected","pid":18308,"message":{"genre":"error","message":"Could not connect to -logFile!"}}

It hangs on this line with no further progress:

browserStackLocal.start(localArgs);

My code is as follows:

var capability = GetBrowserStackCapabilities();
SetBrowserStackLocal(capability);
webDriver = new RemoteWebDriver(new Uri("http://" + ConfigurationManager.AppSettings.Get("BrowserStackServer") + "/wd/hub/"), capability);

private static DesiredCapabilities GetBrowserStackCapabilities()
        {
            var profile = ConfigurationManager.AppSettings.Get("BrowserStackProfile");
            NameValueCollection caps = ConfigurationManager.GetSection(string.Format("capabilities/{0}", profile)) as NameValueCollection;
            var environment = ConfigurationManager.AppSettings.Get("BrowserStackEnvironment");
            NameValueCollection settings = ConfigurationManager.GetSection(string.Format("environments/{0}", environment)) as NameValueCollection;

            DesiredCapabilities capability = new DesiredCapabilities();

            if (caps != null)
            {
                foreach (string key in caps.AllKeys)
                {
                    capability.SetCapability(key, caps[key]);
                }
            }
            if (settings != null)
            {
                foreach (string key in settings.AllKeys)
                {
                    capability.SetCapability(key, settings[key]);
                }
            }

            string username = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME") ?? ConfigurationManager.AppSettings.Get("BrowserStackUser");
            string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY") ?? ConfigurationManager.AppSettings.Get("BrowserStackKey");
            capability.SetCapability("browserstack.user", username);
            capability.SetCapability("browserstack.key", accesskey);            
            return capability;
        }

        private static void SetBrowserStackLocal(DesiredCapabilities capability)
        {
            if (capability.GetCapability("browserstack.local") != null && capability.GetCapability("browserstack.local").ToString() == "true")
            {
                var browserStackLocal = new Local();
                string accesskey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY") ?? ConfigurationManager.AppSettings.Get("BrowserStackKey");
                List<KeyValuePair<string, string>> localArgs = new List<KeyValuePair<string, string>> {
                    new KeyValuePair<string, string>("key", accesskey),
                    new KeyValuePair<string, string>("logFile", "C:/Temp/logfile.txt"),
                    new KeyValuePair<string, string>("force", "true")
                };
                browserStackLocal.start(localArgs);
                FeatureContext.Current.SetBrowserStackLocal(browserStackLocal);
            }

        }

My config is as follows:

  <appSettings>
    <add key="BrowserStackUser" value="<redacted>" />
    <add key="BrowserStackKey" value="<redacted>/>
    <add key="BrowserStackServer" value="hub-cloud.browserstack.com" />
    <add key="BrowserStackProfile" value="single" />
    <add key="BrowserStackEnvironment" value="ie11" />
  </appSettings>
  <capabilities>
    <single>
      <add key="browserstack.debug" value="true" />
      <add key="browserstack.local" value="true" />
    </single>
  </capabilities>
  <environments>
    <ie11>
      <add key="os" value="Windows" />
      <add key="os_version" value="8.1" />
      <add key="browser" value="IE" />
      <add key="browser_version" value="11.0" />
      <add key="resolution" value="1024x768" />
    </ie11>
  </environments>  

How can I get past browser stack local hanging and start remote testing with a local server?


Solution

  • The error "Could not connect to -logFile!" is due to incorrect parameter "logFile". The correct parameter is "logfile" (all in lower case). You should check BrowserStack's documentation here - https://github.com/browserstack/browserstack-local-csharp#logfile.

    Here is a working snippet for Browserstack local C# bindings:

    using System;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Remote;
    using BrowserStack;
    using System.Collections.Generic;
    using OpenQA.Selenium.Chrome;
    
    using System.Threading;
    
    namespace BrowserStackLocalSample
    {
        class Program
        {
        static void Main(string[] args)
        {
            IWebDriver driver;
            var browserStackLocal = new Local();
            List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
                new KeyValuePair<string, string>("key", "<access_key>"),
                new KeyValuePair<string, string>("logfile", "path_To_log/log.txt"),
                //new KeyValuePair<string, string>("binarypath", "/Tools/local/BrowserStackLocal"),
                new KeyValuePair<string, string>("force", "true"),
            };
    
            browserStackLocal.start(bsLocalArgs);
            Thread.Sleep(15000);
    
            DesiredCapabilities capability = new DesiredCapabilities();
            capability.SetCapability("browser", "Chrome");
            capability.SetCapability("browser_version", "62.0");
            capability.SetCapability("os", "Windows");
            capability.SetCapability("os_version", "7");
            capability.SetCapability("resolution", "1024x768");
            capability.SetCapability("browserstack.local", "true");
            capability.SetCapability("browserstack.user", "<username>");
            capability.SetCapability("browserstack.key", "<access_key>");
    
            driver = new RemoteWebDriver(
              new Uri("http://hub.browserstack.com/wd/hub/"), capability
            );
            driver.Navigate().GoToUrl("http://localhost:45691/check");
    
            Console.WriteLine(driver.Title);
    
            driver.Quit();
            browserStackLocal.stop();
        }
    }
    }