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

12

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

2个回答

17

为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

启动单元测试并推进它们直到Zope测试服务器启动。 使用"import pdb ; pdb.set_trace()"停止测试

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

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

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

    https://yourservevr/triggers-client-side-certificate-ssl-handshake

您需要提示您接受针对测试服务器的客户端证书。 手动接受所有内容。

在菜单“帮助”> “故障排除信息”> “应用程序基础知识”> “在Finder中显示” 这将打开保存Webdriver活动配置文件的临时目录。

将Firefox配置文件中的cert8.dbkey3.db复制到您的单元测试包中 WebDriver的Firefox配置文件种子文件夹。 这是Selenium在测试开始时选择Firefox网页驱动程序的文件夹firefox-client-ssl-profile

中断测试。 重新启动测试。 继续运行,直到再次暂停。 在Webdriver的Firefox中 查看设置,以便它现在包含您在上次批准的证书 在“首选项”>“高级”>“加密”>“查看证书”中。

更多信息


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

0

我不确定这是否有帮助,但您可以更改配置文件中的一些偏好设置。在Java中,您可以这样做。

ProfilesIni allProfiles = new ProfilesIni();
    FirefoxProfile profile = allProfiles.getProfile("default"); //change profile name. there is a note somewhere on how to change it
    profile.setPreference(uaKey, uaValue);
    profile.setAcceptUntrustedCertificates(acceptUntrustedSsl);

我不确定这是否是你所需要的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接