Last Updated: February 25, 2016
·
588
· wehappyfew

How many nodes do I have each moment registered on my Selenium Grid?

I was struck with a weird idea last night.

What if I could poll the Selenium Grid Hub somehow and take back the number of Nodes that are attached to it .

We needed this in order to dynamically set the maximum number of tests that we are going to send to the Grid concurrently.

That is 5 * nodes_number(eg 4) = 20 tests .

(5 is the number of the default browser instances that each node accommodates.)

I don’t know Java so it was unthinkable to try and dig inside the selenium server JAR file.
Then I googled for a possible lead but everyone said that was not possible.

So I thought of this …. I parse the HTML source of the grid console and isolate the class=“proxyid”.
That’s it! :] And this is the code.

def grid_nodes_num(grid_console_url="http://my_super_company.com:8080/grid/console#"):
    import requests
    from bs4 import BeautifulSoup

    r = requests.get(grid_console_url)
    html_doc = r.text
    soup = BeautifulSoup(html_doc)
    # print soup.prettify() # for debuggimg

    grid_nodes = soup.find_all("p",     class_="proxyid")
        if grid_nodes == []:
            print "-No Nodes detected. Grid is down!-"
        else:
            nodes_num = len(grid_nodes)
            print "-Detected ",nodes_num," node(s)-"
            return nodes_num