1

我想测试几个按钮单击事件的行为。这是我的 *_spec.rb

***pause_spec.rb***

require 'spec_helper'
require 'rspec'

describe CreditApplicationsController, type: :controller do
  render_views  

before do
  visit '/users/sign_in'                             #### FIRST SIGN IN
    #user = User.find_by_email("mymail@example.com") #### AND FILL REQUIRED FIELDS
  @user = FactoryGirl.create(:user)

  sign_in @user
  fill_in_fields(@user)
  main_page_available?(@user)                         #### *** ####
end

it 'should check pause_on button' do
  click_link 'pause-link'    ### OK HERE, after this - pop up bootbox.js
                             ### confirm window

  #click_link 'Pause-confirm'             ### HERE IS MY PROBLEM ###  
                                          ### click_link / click_button can't find
                                          ### button within my bootbox.js     
  assert has_selector? "leave"
end

end

我尝试了另一种方法来通过水豚脚本对此进行测试。

我尝试了以下方法:

describe CreditApplicationsController, type: :controller, 
                                       js: true, driver: :selenium do

.....

并改变

 click_link 'Pause-confirm'

=>

find('.btn-success').trigger('click')
... or ...
page.execute_script("$('.btn-success').click()")

它没有给出任何结果,而且我的方法 main_page_available?(@user) 中有错误,只是因为我为描述部分添加了js: true (如果在它的“应该...部分添加 js: true”,结果相同)

我在 spec_helper.rb 中的方法 fill_in_fields(user) 和 main_page_available?(user)

    def fill_in_fields(user)
    # @user = User.find_by_email("a.solodkov@prbb.com")
      fill_in "user[email]", with: user.email
      fill_in "user[password]", with: "12345678"
      click_button "Sign in"

      ## Success with sign in action?
      if has_selector? 'input#user_net_name'
        assert has_selector? 'input#user_net_name'
        fill_in "user[net_name]", with: user.net_name
        fill_in "user[password]", with: "12345678"
        fill_in "user[password_confirmation]", with: "12345678"
        click_button 'change-button'
      end
    end

    def main_page_available?(user)
      # Main page?
      if has_selector? 'small.pull-right'
        within "small.pull-right" do
          assert has_content? "#{user.id}"
        end
      else
        assert has_selector? 'table#admin-credit-application'
      end
    end

我究竟做错了什么?

提前致谢!任何帮助将不胜感激!

4

1 回答 1

1

如果您在检查 js 模态窗口中的元素时遇到问题(在我的例子中是 bootbox.js - CustomConfirmBox),您必须为您的规范指定js: true

这是我解决上述问题的方法:

it 'should check pause_on pause_off', js: true do #, driver: :selenium
  click_link 'pause-link'
  page.should have_xpath("//div[@class='modal-body']")
  page.should have_xpath("//div[@class='bootbox modal fade in']")
  page.should have_xpath("//a[@class='btn btn-success']")
  click_link 'Pause'
  page.should have_xpath("//a[@class='btn btn-block btn-success']")
end
于 2013-10-24T07:48:00.957 回答