如何修复“无法读取Browsermob-Proxy json”?

时间:2018-08-13 14:00:12

标签: selenium browsermob-proxy

我正在尝试使用browsermob-proxy来监视硒测试的所有请求和响应。就我而言,我正在seleniumpy.test图像内的docker框架中运行jenkins测试。我正在使用以下版本:

  • selenium == 3.8.0
  • pyvirtualdisplay == 0.2.1
  • pytest == 3.4.0
  • browsermob-proxy == 0.8.0

也是Java openjdk-8-jdk

py.test代码中,我具有以下行来创建和启动服务器:

proxyserver =  Server(
           path="/root/tests/bsp_usecase_tests/bin/browsermob-proxy",
           options={'port': 8090}
       )
proxyserver.start()
driverproxy = proxyserver.create_proxy() # line 127

但在最后一行中,出现以下错误:

conftest.py:127: in basedriver
    driverproxy = proxyserver.create_proxy()
/usr/local/lib/python2.7/dist-packages/browsermobproxy/server.py:40: in create_proxy
    client = Client(self.url[7:], params)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <browsermobproxy.client.Client object at 0x7f1a5f73f1d0>
url = 'localhost:8090', params = {}, options = {}

    def __init__(self, url, params=None, options=None):
        """
            Initialises a new Client object


            :param url: This is where the BrowserMob Proxy lives
            :param params: URL query (for example httpProxy and httpsProxy vars)
            :param options: Dictionary that can contain the port of an existing
                            proxy to use (for example 'existing_proxy_port_to_use')
            """
        params = params if params is not None else {}
        options = options if options is not None else {}
        self.host = "http://" + url
        if params:
            urlparams = "?" + unquote(urlencode(params))
        else:
            urlparams = ""
        if 'existing_proxy_port_to_use' in options:
            self.port = options['existing_proxy_port_to_use']
        else:
            resp = requests.post('%s/proxy' % self.host + urlparams)
            content = resp.content.decode('utf-8')
            try:
                jcontent = json.loads(content)
            except Exception as e:
                raise Exception("Could not read Browsermob-Proxy json\n"
>                               "Another server running on this port?\n%s..." % content[:512])
E               Exception: Could not read Browsermob-Proxy json
E               Another server running on this port?
E               <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
E               <html><head>
E               <meta type="copyright" content="Copyright (C) 1996-2016 The Squid Software Foundation and contributors">
E               <meta http-equiv="Content-Type" CONTENT="text/html; charset=utf-8">
E               <title>ERROR: The requested URL could not be retrieved</title>
E               <style type="text/css"><!-- 
E                /*
E                * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
E                *
E                * Squid software is distributed under GPLv2+ license and incl...

/usr/local/lib/python2.7/dist-packages/browsermobproxy/client.py:37: Exception

也许我忘记了将重要文件复制到Docker映像中吗?

我为browsermob-proxy进行了以下设置(除了安装pip之外):

bsp_usecase_tests/bin:
browsermob-proxy

bsp_usecase_tests/lib:
browsermob-dist-2.1.4.jar

即我已将文件browsermob-proxy复制到bin并将文件browsermob-dist-2.1.4.jar复制到lib文件夹中。另外,服务器本身似乎正在启动并正在运行。但是,当我尝试获取可用于selenium驱动程序的代理信息时,出现了这个奇怪的错误。

有什么想法我做错了什么,或者缺少什么?

1 个答案:

答案 0 :(得分:0)

在我看来,该端口已在使用中。您可以使用lsof检查端口是否正在使用中,然后将其杀死(如果正在使用中)。您可以使用lsof -i:8090

编辑: 您已经在此线程中完成了此操作- How to fix 'Address already in use' error with browsermob-proxy?

我将使用来初始化服务器

proxyserver =  Server(
       path="/root/tests/bsp_usecase_tests/bin/browsermob-proxy",
       options={'existing_proxy_port_to_use': 8090})

这应该做到。

Browsermob-Proxy中的内部代码正在寻找名为'options'的{​​{1}}字典中的密钥,否则它将使用默认的即空字典。