0
我有这样的Ruby代码:红宝石测试用例:在`self.startup`方法定义实例变量
class GoogleTestCase < BaseTestCase
def test_search
@browser.find_element(:name, 'q').send_keys "Hello Ruby"
@browser.find_element(:name, 'btnK')
end
end
然后,我通过这个文件运行GoogleTestCase:
...
class BaseTestCase < Test::Unit::TestCase
def self.startup
@browser = Selenium::WebDriver.for :chrome
@browser.get('https://google.com')
end
def self.shutdown
@browser.quit
end
end
exit Test::Unit::AutoRunner.run(true, test_dir)
启动后,一切都很好。 Selenium将运行Chrome浏览器,它会打开Google网页。但是,当test_search
方法烧制,红宝石不能看到@browser
变量:
我怎样才能在self.startup
方法定义@browser
变量,这样test_search
方法里面我看可以吗?
而不是类的方法'startup'和'shutdown',您可以使用实例方法'setup'和' teardown'。或者使用[class variable](http://ruby-doc.org/core-2.4.1/doc/syntax/assignment_rdoc.html#label-Class+Variables),即'@@ browser'。 – Stefan
不,我不能。因为'setup'方法的行为与'startup'方法不同。 'Startup'只被调用一次,但在每个类的测试方法之前调用'setup'。我不想那样。 – nanuqcz
“或使用类变量” – Stefan