BrowserStackLocal - 无法连接到日志文件

时间:2017-11-22 05:31:25

标签: c# selenium browserstack

我正在尝试使用浏览器堆栈本地与localhost进行交互来远程运行自动化selenium测试。我收到以下错误:

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

然后我指定一个日志文件,然后说:

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

它挂在这一行没有进一步的进展:

browserStackLocal.start(localArgs);

我的代码如下:

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);
            }

        }

我的配置如下:

  <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>  

如何通过浏览器堆栈本地挂起并使用本地服务器启动远程测试?

1 个答案:

答案 0 :(得分:1)

错误“无法连接到-logFile!”是由于参数“logFile”不正确造成的。正确的参数是“ logfile ”(全部为小写)。你应该在这里查看BrowserStack的文档 - https://github.com/browserstack/browserstack-local-csharp#logfile

这是Browserstack本地C#绑定的工作代码段:

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();
    }
}
}