0

我有以下代码

require 'watir-wedriver'

browsers = ['chrome', 'safari']

browsers.each do |browserName|
    browser = Watir::Browser.new :browserName
    # more code here
end

browserName在这里不起作用,因为该命令按字面意思解释它(即,它正在读取browser = Watir::Browser.new :browserName而不是browser = Watir::Browser.new :chrome后跟browser = Watir::Browser.new :safari.

如何使该变量在那里扩展?

4

1 回答 1

1

不要从字面上引用变量

browsers.each do |browserName|
    browser = Watir::Browser.new browserName.to_sym # string to a symbol
    ...
 end

或者只是最初使用符号

 browsers = [:chrome, :safari]
 ...
     browser = Watir::Browser.new browserName
于 2013-10-09T03:23:17.233 回答