• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Still_Walking

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

ruby on rails 集成测试-1

ruby on rails 集成测试-1

这里,我的集成测试用的是capybara+rspec

一、有Gemfile里添加gem引用

  gem 'rspec-rails', '~> 3.5'
  gem 'factory_girl_rails'
  gem 'database_cleaner' # 清理测试数据
  gem 'capybara'
  gem 'capybara-screenshot'  # 保存测试错误的页面
  gem 'selenium-webdriver', '~>2.53.4' #提供网页支持
  gem "chromedriver-helper"   #chrome浏览器支持

 

二、配置

在spec_helper.rb文件里添加如下配置:

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

 

  config.after(:all, type: :feature) do
    DatabaseCleaner.clean
  end

 

三、测试

rails generate rspec:install  #产生spec文件夹

在spec的文件夹里新建文件夹:integration,在这个文件夹里存放集成测试的文件代码。

在这个文件里新建一个login_spec.rb,这查写测试登录的代码:

require 'rails_helper'

# 使用chrome浏览器

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.default_driver = :chrome
Capybara.ignore_hidden_elements = false #这样隐藏的element也能进行操作

RSpec.configuration.include(IntegrationHelper)

RSpec.feature 'integration', type: :feature do
  before(:all) do
     create(:user)
  end

  after(:all) do
    @user.destroy if @user# with create in before(:all), it is not in a transaction
  end

  describe "登录、修改密码、退出", :type => :feature do
    describe 'GET /' do
      scenario 'sign_in and sign_out' do      
        within('#new_user') do
          fill_in 'user[email]', :with => '登录名'
          fill_in 'user[password]', :with => "错的密码"
        end
        click_button '提交'
        expect(page).to have_content '邮箱或密码错误'
 

    within('#new_user') do
          fill_in 'user[email]', :with => '登录名'
          fill_in 'user[password]', :with => "正确的密码"
        end
        click_button '提交'
        expect(page).to have_content '登录成功后页面包含的内容'
      end
    end
  end

end
四、运行测试

运行bundle exec rspec spec/integration,这里只是运行这个目录下的测试,也可以用bundle exec rspec运行所有的测试。这时就应该能够自动打开chrome浏览器,并可以看到效果了。

 

注意事项:启动不了浏览器,可能是因为selenium-webdriver或chrome版本的问题,可以更新一下版本。

 

posted on 2017-03-15 20:30  limanxian  阅读(447)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3