脱离Rails环境用ruby对普通网页进行测试
发布日期:2021-09-29 20:08:34 浏览次数:3 分类:技术文章

本文共 4736 字,大约阅读时间需要 15 分钟。

也是一些普通的例子,抄回来以方便[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"

转载地址:https://blog.csdn.net/horace_lee/article/details/83712235 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Rails测试确认框
下一篇:关于cucumber中webrat 模拟浏览器事件操作

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月27日 02时19分38秒