【发布时间】:2012-03-22 14:54:41
【问题描述】:
当我将一段代码移动到另一个文件时,我无法使用方法。
下面提到的代码有效,因为所有代码都在一个文件中。
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
在上面的代码中,“URL”出现在名为 testdata.xls 的 Excel 工作表中。上面的代码工作得很好。假设这个文件名为 file1.rb
但是我想将浏览器的打开移动到不同的文件中,这样我就不会在所有测试文件中使用此代码,而是仅在一个文件中使用它并从中调用所有其他测试。以下是我所做的更改,但这不起作用。
在 File1.rb 中我保留了
所有必需的文件+我提到的打开浏览器的常用代码的新文件
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
我已经创建了一个文件来打开我想用作常用功能的浏览器。让我们在function.rb中说这个文件名
所有必需的文件+下面的代码
include Watir
module Commonfunctions
def openbrowser
@ie = IE.new
@ie.maximize
@ie.goto (record['Url'])
@ie.focus
end
end
现在,当我运行 file1.rb 时,出现以下错误
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>'
“记录”对象来自不同的文件,即我的数据驱动器。在该文件中,这是存在方法记录的代码
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
谁能帮我解决这个问题。我想将所有常用函数移动到一个文件中,并在所有测试中使用这些函数,而不是在所有测试中编写相同的函数。
【问题讨论】:
标签: ruby methods include watir