Selenium Webdriver获得可用的集线器端口

时间:2012-12-10 03:06:59

标签: selenium webdriver selenium-grid

我正在使用selenium网格在远程计算机上进行分布式测试。这台机器上运行了两个项目,我想分别为每个项目设置selenium hub和节点。但是,如果其中一个集线器不可用,我想将测试传递给可用的集线器。

    webdriver_hub = '/wd/hub'
    PORT.nil? ? port = ':4444' : port =':' + PORT
    @driver = Selenium::WebDriver.for(
      :remote,
      :url => 'http://' + SELENIUM_HUB + port + webdriver_hub,
      :desired_capabilities => caps,
      :http_client => client
    )

默认端口为“:4444”。而是拥有静态默认端口,我希望能够根据可用的集线器动态分配它。有没有办法在测试运行之前获得可用的集线器?

2 个答案:

答案 0 :(得分:2)

这就是HUB的用途,可以使用可用资源。您应该只在配置中指定NODES的端口,而不是测试执行。测试执行应该将HUB与您希望它执行的浏览器/平台/版本连接起来,没有别的。

答案 1 :(得分:0)

我不确定您到底在寻找什么,所以我将尝试解释我认为您可能能够得到您想要的东西。

有两种情况,特定的集线器可能“不可用”。第一个是集线器未运行时。集线器已手动关闭或通过错误关闭,或者它刚刚未启动(可能在重新启动后)。

另一种情况是集线器可用,但当前没有可用于服务请求的节点。如果只有一个节点可以为所有请求提供服务并且当前正在运行最大数量的请求,则可能会发生这种情况。

第一种情况是最容易寻找的。只需尝试连接到远程服务器以确定它是否有服务器正在侦听它。

    /// <summary>
    /// Will test to verify that a Selenium Hub is running by checking a URL
    /// </summary>
    /// <param name="defaultURL">The url to test.</param>
    /// <returns>true if the hub is running.  false if it is not running.</returns>
    /// <example>IsHubRunning(new Uri("http://localhost:4444/wd/status"));</example>
    static public bool IsHubRunning(Uri defaultURL)
    {
        HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(defaultURL); 
        httpReq.AllowAutoRedirect = false;
        HttpWebResponse httpRes;
        try
        {
            httpRes = (HttpWebResponse)httpReq.GetResponse();
            if (httpRes.StatusCode != HttpStatusCode.OK)
            {
                return false;
            }
        }
        catch (WebException ex)
        {
            // Inspect the exection.  An exception of 500 in the case
            // of Selenium may NOT be a problem.  This means a server is listening.
            if (ex.Response == null || ex.Status == WebExceptionStatus.ConnectFailure)
            {
                return false;
            }

            // Did it return the expected content type.
            if(ex.Response.ContentType != "application/json;charset=UTF-8")
            {
                return false;
            }
            if (ex.Response.ContentLength <= 0)
            {
                return false;
            }

        }
        return true;
    }

上面的C#代码将向传入的Uri发出请求。它将执行GET请求,然后检查返回的状态代码。它还测试可能根据您请求的特定Uri报告的异常。如果你请求/ wd / status,你应该得到一个“OK”响应。

这将告诉您集线器是否正在运行。它不会告诉您是否有可用于维护特定请求的节点。您还可以检查响应的其他属性,例如Server属性,以确定响应是否来自Selenium网格中心。

第二种情况涉及更多。如果您想知道是否可以支持一组特定的功能,那么您可以对/ grid / console Uri执行类似的查询。这将返回有关节点及其功能的信息。

要确定集线器可用的节点,只需解析从上面的Uri返回的信息。但是,这需要您的测试客户端进行大量工作,以确定特定节点是否在请求的集线器上可用。

更好的方法是验证哪个集线器已启动并正在运行。然后尝试创建一个请求来自该中心的特定功能集的连接。如果您发出请求的集线器表示它无法为请求提供服务,那么您可以尝试下一个集线器。

以下是一些C#代码,可用于确定集线器是否提供了一组特定的功能。

    /// <summary>
    /// Determines if the hub can provide the capabilities requested.
    /// The specified hub is used.
    /// </summary>
    /// <param name="dc">The capabilities being requested.</param>
    /// <param name="hub">The hub to make the request to.</param>
    /// <returns>true if the requested capabilities are available, otherwise false;</returns>
    static public bool IsRemoteDriverAvailable(DesiredCapabilities dc, Uri hub)
    {
        bool isAvailable = false;
        // Verify that valid capabilities were requested.
        if (dc == null)
        {
            return isAvailable;
        }

        try
        {
            IWebDriver driver = new RemoteWebDriver(hub, dc);
            driver.Quit();
            isAvailable = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error {0}", ex.Message);
        }
        return isAvailable;
    }

希望有所帮助。

相关问题