脱离Rails环境用ruby对普通网页进行测试

也是一些普通的例子,抄回来以方便[url=hlee.iteye.com]老猪[/url]自己好找。

普通的页面测试,需要一下包的支持

require "rubygems"
gem "selenium-client"
require "selenium"



[size=large]例子一[/size],最简单的测试

    #!/usr/bin/env ruby
#
# Sample Ruby script using the Selenium client API
#
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"

begin
@browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60

@browser.start_new_browser_session
@browser.open "/"
@browser.type "q", "Selenium seleniumhq.org"
@browser.click "btnG", :wait_for => :page
puts @browser.text?("seleniumhq.org")
ensure
@browser.close_current_browser_session
end


[size=large]例子二[/size], 使用Rails Unit test测试框架进行测试

    #!/usr/bin/env ruby
#
# Sample Test:Unit based test case using the selenium-client API
#
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.16"
require "selenium/client"

class ExampleTest < Test::Unit::TestCase
attr_reader :browser

def setup
@browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60

browser.start_new_browser_session
end

def teardown
browser.close_current_browser_session
end

def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end

end


[size=large]例子三[/size], 使用rspec风格进行测试

    require 'rubygems'
gem "rspec", "=1.2.6"
gem "selenium-client", ">=1.2.16"
require "selenium/client"
require "selenium/rspec/spec_helper"

describe "Google Search" do
attr_reader :selenium_driver
alias :page :selenium_driver

before(:all) do
@selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
end

before(:each) do
selenium_driver.start_new_browser_session
end

# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
@selenium_driver.close_current_browser_session
end

it "can find Selenium" do
page.open "/"
page.title.should eql("Google")
page.type "q", "Selenium seleniumhq"
page.click "btnG", :wait_for => :page
page.value("q").should eql("Selenium seleniumhq")
page.text?("seleniumhq.org").should be_true
page.title.should eql("Selenium seleniumhq - Google Search")
page.text?("seleniumhq.org").should be_true
page.element?("link=Cached").should be_true
end

end



[size=large]例子四[/size],seleinium测试

    require 'selenium/rake/tasks'

Selenium::Rake::RemoteControlStartTask.new do |rc|
rc.port = 4444
rc.timeout_in_seconds = 3 * 60
rc.background = true
rc.wait_until_up_and_running = true
rc.jar_file = "/path/to/where/selenium-rc-standalone-jar-is-installed"
rc.additional_args << "-singleWindow"
end

Selenium::Rake::RemoteControlStopTask.new do |rc|
rc.host = "localhost"
rc.port = 4444
rc.timeout_in_seconds = 3 * 60
end


生产测试报告

    require 'spec/rake/spectask'
desc 'Run acceptance tests for web application'
Spec::Rake::SpecTask.new(:'test:acceptance:web') do |t|
t.libs << "test"
t.pattern = "test/*_spec.rb"
t.spec_opts << '--color'
t.spec_opts << "--require 'rubygems,selenium/rspec/reporting/selenium_test_report_formatter'"
t.spec_opts << "--format=Selenium::RSpec::SeleniumTestReportFormatter:./tmp/acceptance_tests_report.html"
t.spec_opts << "--format=progress"
t.verbose = true
end

地址如下:
[url]http://ph7spot.com/examples/selenium_rspec_report.html[/url]



# To capture screenshots and logs on failures, also make sure you require the following files in your `spec_helper`
require "rubygems"
require "spec"
require "selenium/client"
require "selenium/rspec/spec_helper"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值