使用无头 watir/firefox 时无法访问自定义 protocol/scheme
Redirect to custom protocol/scheme is not accessible when using headless watir/firefox
我正在为 dropbox oauth 序列进行一组集成测试,该序列以一系列 302 重定向结束,最后一个是自定义 protocol/scheme。在测试模拟的移动应用程序中,一切都按预期工作,一切都在集成测试中工作。
测试环境在 ubuntu 服务器上运行(无 GUI)并且使用 xvfb 是无头的。
客观地说,我实际上并不需要遵循自定义协议 URI,我只需要访问 URI 以确认内容符合预期。
我已经尝试了所有我能想到的方法来从 watir/selenium 中访问包含自定义方案的 URI,但我能找到的所有参考资料都说底层细节是设计故意隐藏的。
我也尝试了我能找到的所有选项,用于在 firefox 配置文件中创建自定义协议处理程序,但无论发生什么情况,脚本都没有被调用。
watir/selenium 日志中未留下任何有用信息。
有什么想法吗?
自定义协议处理程序片段:
# initialise headless
headless = Headless.new( reuse: false )
headless.start
# initialise profile
profile = Selenium::WebDriver::Firefox::Profile.new
profile[ 'general.useragent.override' ] = 'agent'
profile[ 'network.protocol-handler.app.biscuit' ] = '/usr/bin/biscuit'
profile[ 'network.protocol-handler.external.biscuit' ] = true
profile[ 'network.protocol-handler.expose.biscuit' ] = true
profile[ 'network.protocol-handler.warn-external.biscuit' ] = false
# initialise client
client = Selenium::WebDriver::Remote::Http::Persistent.new
# initialise browser
browser = Watir::Browser.new :firefox, profile: profile, accept_insecure_certs: true, http_client: client
# run dropbox authentication cycle
# cleanup
browser.close
headless.destroy
在追寻这个问题很久之后,发现 mozilla 站点和论坛上用于添加自定义方案的大部分文档都已弃用,并且没有任何新内容可以替代它。呜呜呜
通过反复试验,我发现webdriver使用的model profile不需要是完整的,任何缺失的都会从default profile中拉取。因此,所需要的只是一个包含自定义 scheme/s 的 handlers.json 文件,仅此而已。
演示片段:
# create a temporary model profile
profilePath = '/tmp/modelProfile'
FileUtils.mkpath profilePath
File.chmod( 0700, profilePath )
FileUtils.chown 0, 0, profilePath
open( profilePath + '/handlers.json', 'w' ) { |file| file.write '{ "defaultHandlersVersion": { "en-US": 4 }, "schemes": { "biscuit": { "action": 2, "handlers": [ { "name": "biscuit", "uriTemplate": "https://www.biscuit.me?url=%s" } ] } } }' }
# create profile
profile = Selenium::WebDriver::Firefox::Profile.new( '/tmp/modelProfile' )
# initialise browser
browser = Watir::Browser.new :firefox, profile: profile, accept_insecure_certs: true
我正在为 dropbox oauth 序列进行一组集成测试,该序列以一系列 302 重定向结束,最后一个是自定义 protocol/scheme。在测试模拟的移动应用程序中,一切都按预期工作,一切都在集成测试中工作。
测试环境在 ubuntu 服务器上运行(无 GUI)并且使用 xvfb 是无头的。
客观地说,我实际上并不需要遵循自定义协议 URI,我只需要访问 URI 以确认内容符合预期。 我已经尝试了所有我能想到的方法来从 watir/selenium 中访问包含自定义方案的 URI,但我能找到的所有参考资料都说底层细节是设计故意隐藏的。
我也尝试了我能找到的所有选项,用于在 firefox 配置文件中创建自定义协议处理程序,但无论发生什么情况,脚本都没有被调用。
watir/selenium 日志中未留下任何有用信息。
有什么想法吗?
自定义协议处理程序片段:
# initialise headless
headless = Headless.new( reuse: false )
headless.start
# initialise profile
profile = Selenium::WebDriver::Firefox::Profile.new
profile[ 'general.useragent.override' ] = 'agent'
profile[ 'network.protocol-handler.app.biscuit' ] = '/usr/bin/biscuit'
profile[ 'network.protocol-handler.external.biscuit' ] = true
profile[ 'network.protocol-handler.expose.biscuit' ] = true
profile[ 'network.protocol-handler.warn-external.biscuit' ] = false
# initialise client
client = Selenium::WebDriver::Remote::Http::Persistent.new
# initialise browser
browser = Watir::Browser.new :firefox, profile: profile, accept_insecure_certs: true, http_client: client
# run dropbox authentication cycle
# cleanup
browser.close
headless.destroy
在追寻这个问题很久之后,发现 mozilla 站点和论坛上用于添加自定义方案的大部分文档都已弃用,并且没有任何新内容可以替代它。呜呜呜
通过反复试验,我发现webdriver使用的model profile不需要是完整的,任何缺失的都会从default profile中拉取。因此,所需要的只是一个包含自定义 scheme/s 的 handlers.json 文件,仅此而已。
演示片段:
# create a temporary model profile
profilePath = '/tmp/modelProfile'
FileUtils.mkpath profilePath
File.chmod( 0700, profilePath )
FileUtils.chown 0, 0, profilePath
open( profilePath + '/handlers.json', 'w' ) { |file| file.write '{ "defaultHandlersVersion": { "en-US": 4 }, "schemes": { "biscuit": { "action": 2, "handlers": [ { "name": "biscuit", "uriTemplate": "https://www.biscuit.me?url=%s" } ] } } }' }
# create profile
profile = Selenium::WebDriver::Firefox::Profile.new( '/tmp/modelProfile' )
# initialise browser
browser = Watir::Browser.new :firefox, profile: profile, accept_insecure_certs: true