如何显示/记录哪个节点运行了 Selenium 测试会话? [英] How to display/log which node ran Selenium test session?

查看:49
本文介绍了如何显示/记录哪个节点运行了 Selenium 测试会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Windows 机器的网格配置构建测试自动化框架.在不涉及太多不必要的细节的情况下,我在运行带有 Selenium2Library 的 Robot Framework 的服务器上运行测试,该服务器通过一个集线器运行测试会话,该集线器根据浏览器选择节点我选择的操作系统.标准的东西,一切正常;但是,有时测试会挂起或发生无法解释的事情,我想通过 RDP 连接到运行测试的节点以查看是什么.

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.

如果我可以在测试日志中或通过 Python 以编程方式嵌入集线器选择用于测试执行的 Webdriver 节点的机器名称或 IP,那就太好了.我知道这个 Python 代码返回了 Windows 的机器名称:

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.

推荐答案

您可以使用 API 从集线器获取节点的 URL.然后你只需要解析 URL 并提取主机部分.虽然 WebDriver 确实存储中心 URL,但它是在私有属性中存储的.我会尊重这一点,因此此关键字要求您传入集线器 URL:

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:\n%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)

在我使用的这个关键字的版本中,我从全局变量中检索 URL,而不是像上面那样使用参数.

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}')

这篇关于如何显示/记录哪个节点运行了 Selenium 测试会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆