I am unable to use a method when I move a piece of code to another file.
Below mentioned code works cause all the code is in one file.
require 'rubygems'
require 'watir'
require 'win32ole'
require 'erb'
require 'ostruct'
require 'C:/classes/html.class'
require 'C:/classes/Xls'
require 'C:/classes/screen_capture'
require 'C:/classes/RequiredRubies'
include Watir
begin
xlFile = XLS.new(Dir.pwd + '/testdata.xls')
myData = xlFile.getRowRecords('a2:z3','Pit')
xlFile.close
myData.each do |record|
@ie = IE.new
@ie.maximize
@ie.goto (record['Url'])
@ie.focus
end
end
In above code ‘URL’ is present in an excel sheet names testdata.xls. The above code works just fine. Lets say this file name is file1.rb
But I want to move the opening of browser into different file so that I don’t use this code in all the test files and use it in only one file and call from it to all other test. Below is the change which I have done but this does not work.
In File1.rb I have kept
All Required Files+new file where I have mentioned the common code to open the browser
require 'C:/function.rb
include Watir
include Commonfunctions
begin
xlFile = XLS.new(Dir.pwd + '/testdata.xls')
myData = xlFile.getRowRecords('a2:z3','Pit')
xlFile.close
myData.each do |record|
openie = openbrowser
end
end
And I have created a file for opening the browser which I want to use as common function. Lets say this file name in function.rb
All Required files+below code
include Watir
module Commonfunctions
def openbrowser
@ie = IE.new
@ie.maximize
@ie.goto (record['Url'])
@ie.focus
end
end
Now when I run my file1.rb I get the below error
C:/function.rb:17:in `openbrowser': undefined local variable or method `reco
rd' for main:Object (NameError)
from test.rb:23:in `block in <main>'
from test.rb:21:in `each'
from test.rb:21:in `<main>'
The ‘record’ object is coming from different file which is my data drive. In that file this is the code where method record is present
numRecords = myRange.Rows.Count
(0..numRecords-1).each do |i|
record=[]
areas.each do |area|
record.concat(area[i])
end
#Clean up formatting
record.collect! do |x|
if x.is_a?(Float) and x % 1 == 0
x.to_i.to_s
else
x.to_s.strip
end
end
data << record
end
return data
end
Can any one please help me to resolve this issue. I want to move all the common functions to one file and use those functions in all test instead of writing the same functions in all tests.
recordis a lexically scoped variable defined in the args to your block which beginsmyData.each do |record|. Since it’s lexically scoped it can not be referenced by name outside of the context it’s declared in, so you must pass it to the method you made explicitly.BTW, In your question you state that “The ‘record’ object is coming from diffent file”. This is not true, as I’ve already pointed out where the
recordin the context of your issue is set. You were confused here because you’re using poor generic names and not writing small blocks of code with limited scope.I appreciate that you’re working to refactor to improve the code organization. There are many more things with your code samples that deserve some attention: some stylistic (camelCaseNames, random indentation), some structural (no OO, imperative file scripting), some syntactic traps (space between method name and first paren of args), some idiom (poor command of enumerable methods). You might benefit by walking through your code with an experienced ruby programmer and ask for pointers in a code review.