我正在尝试使用Ruby& amp;黄瓜,部分跟随一本名为"使用Ruby进行脚本GUI测试的书#34;作者:Ian Dees。我是Ruby的新手,我正面临着一个简单的问题 - 未定义的方法。我有两个类,每个类都在不同的模块中。第一个类将处理Win32API调用,第二个类表示应用程序的顶级(启动,关闭,查找窗口等)。它们将像页面对象一样工作,将UI交互逻辑与测试逻辑分开。我在Windows API类上获得了一个未定义的方法,对于user32:
#Win32API class
require 'Win32API'
class WindowsAPI
def user32(name, param_types, return_value)
Win32API.new 'user32', name, param_types, return_value
end
@find_window = user32 'FindWindow', ['P', 'P'], 'L'
end
第二个:
#Application class
require_relative 'WindowsAPI'
class VideoLibrarian
@main_win_title = "VidLibMainWin"
attr_accessor :main_win_handle, :win_api
def initialize
@win_api = WindowsAPI.new
end
def Start()
system 'start "" "C:/Users/VideoAnalyser.exe"'
sleep 0.2 while (@main_win_handle = win_api.find_window.call nil, @main_win_title) <= 0
end
end
vl = VideoLibrarian.new
vl.Start
完整堆栈跟踪/错误消息是:
C:/Users/Ruby Scripts/vidlibtests/WindowsAPI.rb:11:in `<class:WindowsAPI>': undefined method `user32' for WindowsAPI:Class (NoMethodError)
from C:/Users/Ruby Scripts/vidlibtests/WindowsAPI.rb:5:in `<top (required)>'
from C:/Users/Ruby Scripts/vidlibtests/VideoLibrarian.rb:3:in `require_relative'
from C:/Users/Ruby Scripts/vidlibtests/VideoLibrarian.rb:3:in `<main>'
任何帮助都非常感谢!
答案 0 :(得分:1)
您将user32
定义为实例方法,而您明确需要将其作为WindowsAPI
类的类方法:
def self.user32(name, param_types, return_value)
Win32API.new 'user32', name, param_types, return_value
end