【问题标题】:remote selenium grid over https is not workinghttps上的远程硒网格不起作用
【发布时间】:2018-08-23 23:25:27
【问题描述】:

我工作的公司要求/印象深刻的是,当使用我们在 AWS 中托管的远程 selenium 网格服务器时,我们所有的 selenium 测试流量都通过 https 进行。

到目前为止,这似乎一直有效,但使用的是最新版本的 selenium 3.14。即使是最简单的测试,我现在也收到了 100 条“InsecureRequestWarnings”。我已经诊断出这些是由与 selenium 网格的 https 连接而不是它自身的测试目标 url 引起的,因为如果我从本地 selenium 服务器和 webdriver 运行相同的测试,我不会遇到同样的问题。

我正在使用以下内容: python 3.6.4、pytest 3.7.2、pytest-selenium 1.13、selenium 3.14(本地和远程 selenium 网格)、chromedriver 2.41(本地和远程)、certifi 2018.8.13、urllib3 1.23

从各种 windows 盒子(server 2008、windows 10 等...)运行

在运行以下代码(基本上是我的 conftest.py 和一个简单的登录测试脚本的组合)时,我收到以下警告(其中 62 个重复)。

D:\Work\PyTestFramework\VirtEnv3_6\lib\site-packages\urllib3\connectionpool.py:857: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    InsecureRequestWarning)

我的示例代码:

import pytest
import webdriverwrapper
import webdriverwrapper.wrapper
from webdriverwrapper import DesiredCapabilities

# testtools are just my reusable helper libraries
from testtools import credentials_helper, login_helper, appointment_helper



def pytest_addoption(parser):
    parser.addoption('--url', action='store', default='https://bookingportal.mycompany.com.au/OBP',
                     help='target machine url')


@pytest.fixture(scope='session')
def url(request):
    return request.config.getoption('url')


@pytest.fixture(scope='function')
def browser(request):
    desired_cap = DesiredCapabilities.CHROME
    desired_cap['chromeOptions'] = {}
    desired_cap['chromeOptions']['args'] = ['--disable-plugins', '--disable-extensions']
    desired_cap['browserName'] = 'chrome'
    desired_cap['javascriptEnabled'] = True
    hostname = "https://selenium.mygriddocker.com.au/wd/hub"
    executor = webdriverwrapper.wrapper.remote.remote_connection.RemoteConnection(hostname, resolve_ip=False)
    b = webdriverwrapper.Remote(executor, desired_cap)
    request.addfinalizer(lambda *args: b.quit())
    return b


@pytest.fixture(scope='function')
def driver(browser, url):
    driver = browser
    driver.set_window_size(1260, 1080)
    driver.get(url)
    return driver


# ------------ put test script here



@pytest.mark.usefixtures("driver")
def test_OBP_Login(driver):
    testId = 'LogIn01'
    credentials_list = credentials_helper.get_csv_data('OBP_LoginDetails.csv', testId)

    assert driver.title == 'Booking Portal'
    rslt1 = login_helper.login_user(driver, credentials_list)
    assert rslt1

    rslt2 = appointment_helper.logout_OBP(driver)
    assert rslt2

您可以看到我正在调用的 selenium 网格服务器的地址是 https://selenium.mygriddocker.com.au/wd/hub(不是真实地址)

并且我已验证它确实具有由公共机构 (Comodo) 颁发的有效证书。 请注意,如果我回滚到 urllib3 和 certifi,我没有这个问题,但我不确定它在什么时候停止工作

很多人一直在压制警告,但我实际上希望证书能够工作。

显而易见的解决方案是按照链接并按照他们的建议添加证书验证,但问题是如何通过 selenium RemoteConnection 类发送连接池管理器参数?

来自https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl 的建议修复:

 import certifi
 import urllib3
 http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())

当我深入研究 selenium 库时,我发现 urllib3.PoolManager 的使用位于 remote_connection 模块中,但它没有任何参数可用于在调用 PoolManager 时通过附加信息发送。您可以在我的示例代码中看到 executor 对象是我调用 remote_connection 的地方(通过 webdriverwrapper,在常规 selenium 中,路径是 selenium/webdriver/remote/remote_connection)

RemoteConnection 类没有设置我可以看到的 PoolManager。

我尝试只编辑 remote_connection,希望将证书信息硬编码到文件中,以便以后弄清楚如何包装它。所以我在我看到 urllib3.PoolManager() 的地方添加了cert_reqs='CERT_REQUIRED', ca_certs=certifi.where() 行(我还将证书导入到该模块中)。靠一些惊人的愚蠢运气,它确实奏效了。现在我不知道如何包装它。

有没有人对如何使远程硒通过 https 工作有任何其他想法?

【问题讨论】:

    标签: python-3.x selenium selenium-grid urllib3 pytest-selenium


    【解决方案1】:

    所以我发现在 selenium 3.13 和 3.14 之间,他们将 http 请求库从 httplib 更改为 urllib3,并且似乎没有适当地处理证书。

    所以我发现我的选择是:1) 使用 selenium 3.13 客户端 2) 使用 selenium 3.14 客户端并禁用 InsecureRequestWarnings import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

    3) 使用 selenium 3.14 客户端并破解 remote_connections.py 文件,将 urllib3.PoolManager() 的所有实例替换为 urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())

    要注意的另一件事是,我认为 Pycharm 缓存库,所以如果我执行 pip 更新但不清除缓存,我会得到混合结果

    4) 以某种方式将 remote_connection.py 包装到我自己的库中并导入添加到 PoolManager 参数中的类 - 这超出了我的能力。

    这不再是一个问题,而是我帮助其他一些在远程 selenium 调用上使用 https 时收到 urllib3 不安全警告的人的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 2018-08-27
      • 2013-01-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多