在Firefox和Selenium测试中自动化SSL客户端证书

Mik*_*maa 11 ssl firefox selenium webdriver ssl-certificate

是否可以使用Selenium和任何浏览器测试客户端SSL证书?例如,您可以创建一个Web驱动程序并为其提供虚拟证书吗?或者使用准备好的Firefox配置文件?

Mik*_*maa 16

为SSL客户端证书创建Selenium Firefox测试配置文件

您需要准备Selenium的WebDriver Firefox配置文件,其中包含导入的客户端证书.

首先,在测试代码中使用以下配置启动WebDriver:

# Pre-seeded Firefox profile directory
profile_directory = os.path.join(os.path.dirname(__file__), "..", "..", "certs", "firefox-client-ssl-profile")
self.assertTrue(os.path.exists(profile_directory))

profile = FirefoxProfile(profile_directory)

# Make sure the client side certificate selection does not interrupt the test
# XXX: What happens in other language versions?
profile.set_preference("security.default_personal_cert", "Select Automatically")
self.driver = WebDriver(firefox_profile=profile)

self.selenium_helper = SeleniumHelper(self, self.driver)
self.selenium_helper.driver = self.driver
Run Code Online (Sandbox Code Playgroud)

启动单元测试并将它们驱动到Zope测试服务器启动的位置.使用"import pdb; pdb.set_trace()"停止测试

您现在应该在屏幕上有一个Selenium的"WebDriver"Firefox实例.

导入客户端证书.首选项>高级>加密>查看证书.从客户端证书供应中导入"client.p12".

访问在Webdriver的Firefox中触发客户端证书对话框的URL ::

    https://yourservevr/triggers-client-side-certificate-ssl-handshake
Run Code Online (Sandbox Code Playgroud)

这应该会提示您接受针对测试服务器的客户端证书.手动接受一切.

访问菜单帮助>疑难解答信息>应用程序基础>在Finder中显示.这将打开包含Webdriver活动配置文件的临时目录.

复制Firefox的配置文件cert8.db,并key3.db给你的单元测试包的webdriver的Firefox配置文件的种子文件夹.这是Selenium在测试开始时为Firefox Web驱动程序选择种子的文件夹firefox-client-ssl-profile.

中断测试.重启测试.再次运行直到暂停.在Webdriver的Firefox中,在设置中看到它现在包含您在"首选项">"高级">"加密">"查看证书"中上次运行时批准的证书.

更多信息

  • 从 Firefox 版本 58 开始,它将是 cert9.db 和 key4.db。使用 Visual Studio 确保在属性中将它们标记为“始终复制”,以确保在构建时复制这些文件。 (3认同)