Claire Nagle Claire Nagle - 3 months ago 13
Ruby Question

Using a var to complete watir agent

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?

Answer

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'.

Comments