C#中的Selenium Grid

时间:2016-07-07 00:04:14

标签: c# selenium-webdriver selenium-grid

我正在尝试在另一台本地计算机上运行我的测试,但我总是最终失败!我已经看过在JAVA中成功实现的视频,但我试图通过c#来实现。

任何想法都非常感谢!

public class Driver
{ 
    public static IWebDriver Instance { get; set; }

    public static void Initialize()
    {
        IWebDriver driver;
        driver = new ChromeDriver();

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities = DesiredCapabilities.Chrome();
        capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
        capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));


        driver = new RemoteWebDriver(new Uri("http://localhost:4446/wd/hub"), capabilities);
    }
    public static void Close()
    {
        Instance.Dispose();
        Instance = null;
    }

1 个答案:

答案 0 :(得分:0)

  1. 您应该使用网格实例的远程uri而不是本地,并确保至少在其中一个节点上安装了chrome。如果要在本地使用selenium grid,请首先使用selenium-server-standalone.jar启动本地集线器。您应该使用here

  2. 中的信息
  3. 此外,您不需要此代码:

    driver = new ChromeDriver();` - you need RemoteWebDriver directly
    
  4. 对我来说,这段代码完美无缺:

    var uri = 'uri_to_your_grid_hub';
    var capabilities =  new ChromeOptions().ToCapabilities();
    var commandTimeout = TimeSpan.FromMinutes(5);
    var driver = new RemoteWebDriver(new Uri(uri),capabilities,commandTimeout)