if ENV['ENV'].upcase == 'PROD'
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
"mobileEmulation" => mobile_emulation,
"args" => ["headless"]
})
elsif (ENV['ENV'].upcase == 'STAGING') || (ENV['ENV'].upcase == 'PR')
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
"mobileEmulation" => mobile_emulation,
"extensions" => [
Base64.strict_encode64(File.open("../pf-utils/#{ENV['ENV'].downcase}_basic_auth.crx", 'rb').read)
],
"args" => ["headless"]
})
@driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
在这里,下面的条目是新添加的,
"extensions" => [
Base64.strict_encode64(File.open("../pf-utils/#{ENV['ENV'].downcase}_basic_auth.crx", 'rb').read)
],
在这种情况下,如何优化上面的if...else
语句以避免重复代码
答案 0 :(得分:1)
options = {
'chromeOptions' => {
'mobileEmulation' => mobile_emulation,
'args' => ['disable-infobars',
'disable-gpu',
'disable-extensions',
'ignore-certificate-errors']
}
}
if ENV['ENV'].casecmp('PROD').zero?
options['chromeOptions']['args'].push('headless')
elsif ENV['ENV'].casecmp('STAGING').zero? || ENV['ENV'].casecmp('PR').zero?
options['chromeOptions']['extensions'] = [
Base64.strict_encode64(File.open("../pf-utils/#{ENV['ENV'].downcase}_basic_auth.crx", 'rb').read)
]
end
caps = Selenium::WebDriver::Remote::Capabilities.chrome(options)
@driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
答案 1 :(得分:0)
只需预先声明密钥:
COMMON_KEYS =
%w[disable-infobars disable-gpu
disable-extensions ignore-certificate-errors].freeze
ARGS_KEYS = {
'STAGING' => COMMON_KEYS + %w[headless],
}.tap { |h| h.default = COMMON_KEYS }.freeze
EXT = {
'PROD' => {}
}.tap { |h| h.default = {"extensions" => Base64.strict_encode64(...)} }.freeze
caps =
Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
"mobileEmulation" => mobile_emulation,
"args" => KEYS[ENV['ENV'].upcase]
}.merge(EXT)
}
答案 2 :(得分:0)
extensions =
case ENV['ENV'].upcase
when 'STAGING', 'PR'
{"extensions" => [Base64.strict_encode64(File.open("../pf-utils/#{ENV['ENV'].downcase}_basic_auth.crx", 'rb').read)]
}
else {}
end
@driver = Selenium::WebDriver.for :chrome, desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
"mobileEmulation" => mobile_emulation,
"args" => ["headless"]
}.merge(extensions))