Reputation: 69
I am working on a way to share a Ruby program. This is my test code.
require 'rubygems'
require 'watir-webdriver'
require 'headless'
require 'google_drive'
@browsertype =""
@name =""
@champsname = ""
@date = DateTime.now
file = File.exists?("temp.txt")
if file == true
input = IO.read("temp.txt")
input_hash = Hash[*input.gsub(/"/,"").split(/\s*[\n=]\s*/)]
browsertype = input_hash.shift
name = input_hash.shift
champsname = input_hash.shift
@browsertype = browsertype[1]
@name = name[1]
@champ = champsname[1]
end
if file == false
f = File.new("temp.txt", "w")
# processing
puts "What browser will you be using? "
browsertype = gets
f.write("browser = :")
f.write(@browsertype)
puts "What is your name? "
name = gets
f.write("name = ")
f.write(@name)
puts "what is your champs name"
champsname = gets
f.write("champion = ")
f.write(@champsname)
@browsertype = browsertype
@name = name
@champ = champsname
end
puts @browsertype
puts @name
puts @champ
puts @date
#@b = "Watir::Browser.new :"+@browsertype
agent = Watir::Browser.new
@b = agent+@browsertype
#b = @b
#b.goto 'https://docs.google.com/spreadsheet/ccc?key=0AncPM9_7wL02dHVaSWg5eklfYW5jdTE3NGtJSGJPb3c'
I used this question as a reference for making var out of files, Variables magic and read from file. I want my friends to be able to use the finished product by using orca Building a Windows executable from my Ruby app?. The problem I have been encountering is Watir runs Firefox by default when you use
agent = Watir::Browser.new
But not everyone uses Firefox, so this is why I created the browsertype in the file. But when I use
@b = "Watir::Browser.new :"+@browsertype
I get an error saying that the +string
is invalid and I get the same for symbol. Does anyone have any suggestions on how I can have a user defined browser type?
Upvotes: 1
Views: 303
Reputation: 6660
Watir accepts a small specific set of 'symbols' such as :chrome for the browser type. If accepting input from a user I'd use a case statement to setup a variable that contains the specific symbol (:firefox, :chrome, etc) based on their input and give feedback to the user if they don't type in a value that matches what you've anticipated.
alternatively you can also use .to_sym on a string to cast it to a symbol.. so
@browser_type = "chrome"
@b = Watir::Browser.new @browser_type.to_sym
Upvotes: 0
Reputation: 46836
You should pass the browser type to the initialization of the browser.:
@b = Watir::Browser.new @browsertype
This assumes that @browsertype
is something like 'firefox'
.
Upvotes: 1