[英]include file ruby selenium
I have multiple ruby test cases for selenium-webdriver and all the files are sharing the same functions. 我有selenium-webdriver的多个ruby测试用例,所有文件都共享相同的功能。 is there any way to create a global file and include the file to these test cases instead of typing them over and over again 有没有办法创建一个全局文件,并将文件包含在这些测试用例中,而不是一遍又一遍地输入它们
for example - I create a file setup.rb 例如 - 我创建了一个文件setup.rb
def setup
@driver = Selenium::WebDriver.for :firefox
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
end
then in my test_file.rb I start 然后在我的test_file.rb中我开始
require setup
setup
@driver.find_element(:xpath => '//span[text()="войти"]').click
There is an error 有一个错误
NoMethodError:
undefined method `find_element' for nil:NilClass
Change it to a global variable from an instance variable. 将其从实例变量更改为全局变量。 Make it $driver
instead of @driver
and you shouldn't have a problem. 使它成为$driver
而不是@driver
,你应该没有问题。 Change it to something like.. 把它改成像......
def self_setup
$driver = Selenium::WebDriver.for :firefox
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
end
and then 接着
require "./setup.rb"
setup.setup
$driver.find_element(:xpath => '//span[text()="войти"]').click
That should work. 这应该工作。 You'd probably want to go to a page first before you look for that xpath though. 您可能希望在查找该xpath之前先转到页面。 setup will only open up a new instance of firefox webdriver. 安装程序只会打开一个新的firefox webdriver实例。 Also I would suggest changing the name of setup.rb so it can be foo.setup
insead of setup.setup
. 另外我建议更改setup.rb的名称,以便它可以是foo.setup
foo.setup setup.setup
。 I use Lib.rb for the methods I want to be able to call regularly so for instance one would be Lib.signin_admin
我使用Lib.rb作为我希望能够定期调用的方法,例如一个是Lib.signin_admin
Hope this works for you. 希望这对你有用。
In response to your example, I think you forgot to include the setup module (you did put your method definition inside a module, right?). 在回答您的示例时,我认为您忘记包含设置模块(您确实将方法定义放在模块中,对吧?)。 Also, the comment that mentions assigning the driver as a global variable (by naming it with a starting dollar sign) is a good idea. 此外,提及将驱动程序指定为全局变量的注释(通过用起始美元符号命名)是个好主意。 So things would look like this... 所以事情看起来像这样......
setup.rb 的setup.rb
module Setup
def setup
$driver = Selenium::WebDriver.for :firefox
$wait = Selenium::WebDriver::Wait.new(:timeout => 10)
end
end
test_file.rb test_file.rb
require 'setup'
class SeleniumTest < Test::Unit::TestCase
include Setup # Modules need to be included (mixed-in) in order to be used inside classes
# Setup is automagically called when using TestUnit
$driver.get "http://www.yoururl.com"
$driver.find_element(:xpath => '//span[text()="войти"]').click
end
The downside is that for each new module and file you create, you have to require and include all of the new files and modules you want to use. 缺点是对于您创建的每个新模块和文件,您必须要求并包含您要使用的所有新文件和模块。
The method that I have found to work for me is to create a 'test_helper.rb', and to use a gem called 'require_all' that requires and includes all of the files from the directories you specify. 我发现为我工作的方法是创建一个'test_helper.rb',并使用一个名为'require_all'的gem,它需要并包含你指定目录中的所有文件。
My test_helper.rb looks something like this: 我的test_helper.rb看起来像这样:
require "rubygems"
require "require_all"
require "selenium-webdriver"
require "test-unit"
require_all relative_path("../lib/selenium/")
module TestHelpers
include Selenium
def setup
$driver = Selenium::WebDriver.for :firefox
...
end
def teardown
$driver.quit
end
end
And the test_page.rb only requires two lines: test_page.rb只需要两行:
# Line 1: Ensures the test_helper.rb gets loaded from the same directory the test_page.rb resides in
require File.join(File.dirname(__FILE__), 'test_helper')
class TestPage < Test::Unit::TestCase
# Line 2: Module needs mixed in to use its methods
include TestHelpers
def test_page
$driver.get "http://www.mysite.com"
assert $driver.find_element(:css => "div#my_site_logo")
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.