2013-10-23 40 views
1

我想測試幾個按鈕點擊事件的行爲。這是我的* _spec.rbRSpec,水豚,bootbox.js,Rails-3.2。奇怪的行爲:selenium驅動

***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("[email protected]") #### 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:真用於描述部分離子(如果加上js:true'應該......部分)。

我的方法fill_in_fields(用戶)和main_page_available?(用戶)在spec_helper.rb

def fill_in_fields(user) 
    # @user = User.find_by_email("a[email protected]") 
     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 

我在做什麼錯?

在此先感謝!任何幫助將不勝感激!

回答

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