The answer I found while looking that this problem was to use a global variable or a class or function.
So for example, if I am using watir
and I have a browser object I am interacting with, I can get it out of a test script with the following:
Global Variables
watir_test.rb
require "watir-webdriver"
browser = Watir::Browser.new :chrome
$b = browser
# all sorts of preliminary stuff I do on the browser object
# end of my test script
Now after launching irb
, I load the test file
irb(main):001:0> load './watir_test.rb'
irb(main):002:0> browser
NameError: undefined local variable or method `browser' for main:Object
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):004:0> $b
=> #<Watir::Browser:0x..fd9200f9a url="http....">
Returned Element(s)
test_code.rb
def test_code
a = 5
return a
end
> load 'test_code.rb'
> a = test_code()
Return + local_variables
Here is a neat example:
https://stackoverflow.com/a/1961317/999943
Ruby local_variables returns :symbols?
def foo(((a, b, c, d), e, *f), g, *h)
local_variables.sort.each do |lvar| puts "#{lvar} => #{eval(lvar).inspect}" end
end
foo([[1, 2, 3], 4, 5, 6], 7, 8, 9)
# a => 1
# b => 2
# c => 3
# d => nil
# e => 4
# f => [5, 6]
# g => 7
# h => [8, 9]