脱离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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年03月27日 02时19分38秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
手把手教你使用Unity制作一个飞机喷射火焰尾气的粒子效果
2019-04-27
gitee使用教程,创建项目仓库并上传代码
2019-04-27
Unity3D 上传日志
2019-04-27
Unity3D 序列帧
2019-04-27
Unity3D 动态创建图集并压入精灵(NGUI)
2019-04-27
C# 将Excel转xml
2019-04-27
Unity3D log写入文件
2019-04-27
Unity3D动态创建摄像机
2019-04-27
unity 根据纪元时间(1970/1/1)转换为DateTime
2019-04-27
C# 字符串md5加密
2019-04-27
unity3D 拖拽旋转3D物体
2019-04-27
unity3D LineRender的使用:插值移动终点
2019-04-27
unity3d 根据手指触摸的位置去放置UI
2021-06-30
加锁单例
2021-06-30
unity3D 让粒子在UI上播放
2021-06-30
C++的dll导出类
2021-06-30
Matlab与C++混合编程
2021-06-30
unity3D 射线球体投射碰撞检测
2021-06-30
Unity3D 摇一摇功能
2021-06-30
unity3D 在屏幕边框创建碰撞框
2021-06-30