【问题标题】:Selenium RemoteWebDriver c# - System.InvalidOperationExceptionSelenium RemoteWebDriver c# - System.InvalidOperationException
【发布时间】:2017-05-03 16:16:33
【问题描述】:

我有一个使用 Selenium.WebDriver v3.4.0 的示例 UI 测试项目。

当我对本地驱动程序运行测试时,一切正常,但我想使用 Selenium Grid 2 让事情正常运行。

当我尝试实例化一个新的 RemoteWebDriver 时,我得到了一个几乎没有细节的异常。

Driver = new RemoteWebDriver(new Uri(GridUrl), Capabilities);     

注意:GridUrl 是“http://localhost:4444/wd/hub

使用 StackTrace 引发 System.InvalidOperationException,如下所示:

   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities)
   at xxxx.Ui.Tests.SeleniumTests.TestInitialize() in C:\Users\xxxx\Documents\Visual Studio 2015\Projects\xxxx.Ui.Tests\xxxx.Tests\PersonTests.cs:line 38

集线器配置

我有 v3.4.0 的集线器在本地运行,配置如下:

{
  "port": 4444,
  "newSessionWaitTimeout": -1,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {},
  "capabilityMatcher":"org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
  "throwOnCapabilityNotPresent": true,
  "cleanUpCycle": 5000,
  "role": "hub",
  "debug": false,
  "browserTimeout": 0,
  "timeout": 1800
}

中心开始于:

java -jar selenium-server-standalone-3.4.0.jar -role hub

这一切正常,看起来正在运行。

节点配置

我尝试了许多节点(chromedriver.exe、IEDriverServer.exe 和 geckodrvier.exe)。这些都不适用于 RemoteWebDriver。它们都位于已添加到我的系统 PATH 变量中的目录中。

Chrome 配置

{
  "capabilities":
  [
    {
      "browserName": "chrome",
      "maxInstances": 5,
      "seleniumProtocol": "WebDriver"
    }
  ],
  "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
  "maxSession": 5,
  "port": 5556,
  "register": true,
  "registerCycle": 5000,
  "hub": "http://localhost:4444",
  "nodeStatusCheckTimeout": 5000,
  "nodePolling": 5000,
  "role": "node",
  "unregisterIfStillDownAfter": 60000,
  "downPollingLimit": 2,
  "debug": false,
  "servlets" : [],
  "withoutServlets": [],
  "custom": {}
}

节点开始于:

java -jar selenium-server-standalone-3.4.0.jar -role node -nodeConfig chromeNodeConfig.json

除了浏览器名称和端口不同之外,其他节点配置基本相同。

所有节点启动后,控制台如下所示:

我无法从异常中得到很多。这是我拥有的驱动程序的版本问题吗?我尝试自定义我的 DesiredCapabilities 以确保我匹配节点配置中的那些。所有这些看起来都很好。


更新

根据要求,我将添加更多关于我如何尝试启动浏览器的详细信息。没有一个浏览器与 RemoteWebDriver 一起使用,而它们与本地驱动程序一起使用。显示 Chrome 示例 - 每个之间的唯一区别在于我传递给基类构造函数的功能。

在我的测试课中

[TestClass]
public class PersonTests : PersonTestBase
{
    public PersonTests() 
        : base(DesiredCapabilities.Chrome())
    {
    }

    [TestCategory("Chrome")]
    [TestMethod]
    public void Chrome_ShouldCreatePlacement()
    {
        this.ShouldCreatePerson();
    }        
}

在我的基类中,我正在执行以下操作

public abstract class PersonTestBase
{
    protected IWebDriver Driver;
    protected ICapabilities Capabilities;
    protected string TargetUrl;
    protected string GridUrl;

    protected PersonTests(ICapabilities capabilities)
    {
        this.Capabilities = capabilities;
    }

    [TestInitialize]
    public void TestInitialize()
    {
        TargetUrl = "http://urlOfMyWebsite";
        GridUrl = "http://localhost:4444/wd/hub"

        Driver = new RemoteWebDriver(new Uri(GridUrl), Capabilities);            
    }

    [TestCleanup]
    public void TestCleanup()
    {
        Driver.Quit();
    }

    protected void ShouldCreatePerson()
    {
        Driver.Navigate().GoToUrl(TargetUrl);

        //rest of test code ommitted
    }
}

【问题讨论】:

  • 可能要检查任务管理器并确保您没有一堆仍在运行的流氓浏览器驱动程序实例。在本地调试时可能会发生这种情况。这给我们带来了许多问题。
  • 感谢您的建议。我明天去看看!
  • 您要启动什么浏览器?你能提供更多关于你是如何做到这一点的代码吗?
  • 我已按要求添加了更新。 @GrayDwarf - 感谢您的建议。我有许多 ChromeDriver 进程正在运行,但重新启动了我的机器并重新运行了测试,但仍然是同样的异常。

标签: c# selenium selenium-grid remotewebdriver


【解决方案1】:

降级到 3.3.0 直到 this issue 得到解决并且新版本的 Selenium Standalone Server 可用(推荐的解决方案)

或者

  1. 下载Solution
  2. 评论this line
  3. Build dotnet language bindings
    • 在根目录打开命令窗口
    • 运行go //dotnet:release
    • 并引用 {root}/build/dotnet/dist 中内置的二进制文件

注意:此解决方法不会解决任何问题!它会忽略导致失败的那段 selenium 网格代码。

另一个注意事项:请注意,升级到 Selenium 3.4 可能还需要升级网络驱动程序

【讨论】:

  • 优秀。我已将 Selenium.WebDriver 和 Selenium.Support 降级到 v3.3.0。这现在有效。非常感谢 - 搜索了一段时间没有任何进展!
  • @StephenCavender 我不确定 Selenium Grid 3.4.0 的 C# 绑定,但对于我的 Java 绑定,它可以使用默认设置完美执行。
【解决方案2】:

V3.5.1 修复了这个问题。

使用 NuGET 管理器和您的 selenium 独立 jar 升级您的 Selenium NuGET 包。

【讨论】:

    【解决方案3】:

    按照 Stephen 的建议降级到 3.3.0 可能会导致已知 issue。请尝试降级到 v3.3.1。

    您可以从这里获取 v3.3.1: http://selenium-release.storage.googleapis.com/index.html?path=3.3/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多