Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

python - How to display/log which node ran Selenium test session?

I am building a test automation framework using a grid configuration of Windows machines. Without going into too much needless detail, I'm running the tests off a server running Robot Framework with Selenium2Library, which runs the test session through a hub that selects a node based on the browser & OS I selected. Standard stuff, and it all works fine; however on occasion a test hangs or something unexplainable happens, and I'll want to RDP to the node that ran the test to see what's what.

It would be nice if I could embed, either in the test log or programmatically via Python, the machine name or IP of the Webdriver node that the hub selected to test the execution. I know that this Python code returns a machine name for Windows:

import socket
nodeName=socket.gethostname()

But of course when you execute that in the test script it returns the name of the server running the script, not the name of the node running the test session.

Anyone know how I can do this? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can get the URL of the node from the hub using the API. Then you just have to parse the URL and extract the host portion. While WebDriver does store the hub URL, it does so in a private attribute. I will will respect that, so this keyword requires you to pass in the hub URL:

import urllib2, json
from robot.libraries.BuiltIn import BuiltIn
from robot.api import logger
from urlparse import urlparse, urljoin


class Selenium2LibraryExt(object):    

    def get_node_hostname(self, hub_url):
        '''Returns the hostname/IP of the node associated with the current browser.

        `hub_url` should be the URL of the Grid hub.
        '''
        session_id = BuiltIn().get_library_instance('Selenium2Library')._current_browser().session_id
        fragment = '/grid/api/testsession?session=%s' % session_id
        query_url = urljoin(hub_url, fragment)
        req = urllib2.Request(url=query_url)
        resp = urllib2.urlopen(req).read()
        logger.debug('GET of %s returned:
%s' % (query_url, resp))
        json_blob = json.loads(resp)
        if 'proxyId' in json_blob:
            proxy_id = json_blob['proxyId']
            logger.info('Selenium session is executing on %s' % proxy_id)
            parse_result = urlparse(proxy_id)
            return parse_result.hostname
        else:
            raise Exception('Failed to get hostname. Is Selenium running locally? hub response: %s' % resp)

In the version of this keyword I use, I retrieve the URL from a global variable rather than using an argument as above.

hub_url = BuiltIn().replace_variables('${SELENIUM GRID URL}')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...