在 Watir 测试中处理 Chrome 个客户端证书

Handling Chrome client certificates in Watir tests

我有一个基于客户端证书的身份验证的应用程序,我一直在尝试将其自动化。通过选择不同的证书,用户可以获得不同的应用权限。这个想法是使用基于 watir-webdriver 的脚本和 rautomation gem 并登录到应用程序。在 Chrome 网络浏览器中它看起来很像这样:

基本思路如下:

require 'watir-webdriver'
require 'rautomation'

b = Watir::Browser.new :chrome  
b.goto 'https://example.com' 

# Get the Chrome window
window = RAutomation::Window.new(:title => /Chrome/i)
# Select client certificate     
window.send_keys :return

但是,当脚本执行并到达 b.goto 'https://example.com' 时,它会卡住,因为在选择证书之前不会加载页面。 60 秒后,这导致客户端超时,我得到一个 Net::ReadTimeout 异常。因此,永远不会达到证书选择代码。

我已经通过捕获 Net::ReadTimeout 异常解决了这个问题:

begin
 b.goto 'https://example.com' 
rescue      
  window = RAutomation::Window.new(:title => /Chrome/i)   
  window.send_keys :return
end

这个解决方案远非最佳,因为脚本必须等待 60 秒才能开始执行。使用以下代码可以将超时降低到合理的等待时间:

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 5 # seconds – default is 60
b = Watir::Browser.new :chrome, :http_client => client

但对于脚本的其余部分,5 秒的 client.timeout 时间太短了。

我认为问题出在 goto 上,所以我尝试了其他方法,但它们似乎都以相同的方式运行:

b.driver.navigate.to 'https://example.com' # => Net::ReadTimeout
b.execute_script('window.location.href = "https://example.com"') # => Net::ReadTimeout

任何人都可以为我提供优化建议或其他一些最佳方式来处理上述客户端证书吗?

Watir 仅适用于浏览器呈现的页面。尝试完全忽略证书。 http://watirwebdriver.com/browser-certificates/

Thread 不会在这里帮助您吗?不确定是否会,因为它取决于 Ruby 的 GIL(全局解释器锁)和底层 Webdriver 的技术细节,但您可以尝试一下。

这些方面的一些东西可能工作(未测试):

t = Thread.start { b.goto }

# Not sure if getting handle works or not, but if it does
# then it should be a better way to locate the browser window
window = RAutomation::Window.new(:hwnd => b.window.handle)

# Wait until client certificate window exists
RAutomation::WaitHelper.wait_until { window.windows.any? { |w| w.text =~ /Select a certificate/ }}

# Select client certificate     
window.send_keys :return

# Wait for the page load to finish
t.join