开发者

How to create a Selenium test suite written in Ruby?

开发者 https://www.devze.com 2023-04-10 10:20 出处:网络
I have a bunch of Selenium tests written in Ruby. I record with the IDE and export HTML tests as Ruby(Test::Unit) tests. In my TestingProject (Ruby project created in NetBeans) I simply create a new f

I have a bunch of Selenium tests written in Ruby. I record with the IDE and export HTML tests as Ruby(Test::Unit) tests. In my TestingProject (Ruby project created in NetBeans) I simply create a new file (TestFile.rb) and copy/paste in the recorded content that was exported from Selenium IDE. After a while I have a few files each testing a different part of our application.

TestFile1.rb
TestFile2.rb
TestFile3.rb

Currently to run the test I have a main.rb file and I call these test files like so:

#file name: main.rb
require "./FolderName/TestFile1.rb"
require "./FolderName/TestFile2.rb"
require "./FolderName/TestFile3.rb"

In ruby the "require" method takes t开发者_开发百科he name of a file and executes the contents as ruby code. This method maintains a list of files it has already processed so it won't run any of these files more than once. However, I can't control the order of the files as they are executed nor can I rerun certain files without declaring them twice. How do I create a real test suite for my tests(programatically)? Please include steps. Thank you.

PS: This: Create JUnit TestSuite in a non-static way is not helpful to me. I would like steps in the context of my example. Thank you.


Until you find a better answer you can do this in a somewhat ghetto way using eval...

def test1
  eval(File.open(File.expand_path('~/FolderName/TestFile1.rb')).read)
end

def test2
  eval(File.open(File.expand_path('~/FolderName/TestFile2.rb')).read)
end

def test3
  eval(File.open(File.expand_path('~/FolderName/TestFile3.rb')).read)
end

test1
test2
test3
test1
#etc

Like I said, this is the wrong way to do it but hopefully when someone sees my horrible way of doing it they will provide a better one.


How about this? Grab the test script files and order them in whatever fashion (alphabetically in this case) and just run them one at a time.

scripts = DIR.entries('~/FolderName').select { |e| e =~ /.+\.rb/ }
scripts.sort.each { |s| `#{RUBY} s`}


So I think you could do something like this:

I am assuming each has setup and teardown already, but if you map them in a CSV can probably do the following

#file name: main.rb
require 'csv'

def test_case
    test_case = './FolderName/TestPlan.csv'
    descriptor = test_case.shift
    descriptor = descriptor.map { |key| key.to_sym }
    test_case.map { |test| Hash[descriptor.zip(test) ] }
end

def run
    setup
    yield
    teardown
end 

test_case.each do |test|
    run do
##someFlair
        puts "Running Test #{test[:RECORDNUM]} @ #{(Time.now.strftime("%m-%d-%Y_%H%M%S"))}"
        require test[://columnReferring"./FolderName/TestFile1.rb"//]
    end
end

this probably isn't the best way but I think it could work.

Edit: added some flair to show if you add "RECORDNUM" as a column to the csv it can tell you which test is being ran also.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号