2011-06-29 98 views
0

我有以下集成測試。它用一個表單加載一個頁面。按下提交按鈕時沒有任何數據,因此表單應顯示錯誤框。表單用ajax提交,並且應該將表單放回到出現錯誤的頁面上。NOOB硒rspec測試未通過

我可以在瀏覽器中看到這個,但測試失敗。

我在做什麼錯?我是一個完整的NOOB,所以需要一些指導。

需要 'spec_helper' 要求 「RubyGems的」

describe "Boards" do 

    describe "board creation failure" do 
    attr_reader :selenium_driver 
    alias :page :selenium_driver 

    before(:all) do 
     @verification_errors = [] 
     @selenium_driver = Selenium::Client::Driver.new \ 
     :host => "localhost", 
     :port => 4444, 
     :browser => "*chrome", 
     :url => "http://localhost:3000/", 
     :timeout_in_second => 60 
    end 

    before(:each) do 
     @selenium_driver.start_new_browser_session 
    end 

    after(:each) do 
     @selenium_driver.close_current_browser_session 
     @verification_errors.should == [] 
    end 

    it "should show the error explanation div" do 
     page.open "/" 
     page.click "board_submit" 
     page.is_element_present("error_explanation").should be_true #should show error box 
    end 
    end 

回答

1

我想通了。

我需要添加以下方法來告訴Selenium等待所有ajax調用完成。

我把這個方法放在我的spec/spec_helper.rb文件中,並確保在文件中需要'spec_helper'。

這裏是spec_helper.rb方法:

 #needed for selenium ajax testing 
     def wait_for_ajax(timeout=5000) 
      js_condition = 'selenium.browserbot.getUserWindow().jQuery.active == 0' 
      @selenium_driver.wait_for_condition(js_condition, timeout) 
     end 



    #needed for selenium ajax testing 

    def selenium_setup 

     @verification_errors = [] 
     @selenium_driver = Selenium::Client::Driver.new \ 
     :host => "localhost", 
     :port => 4444, 
     :browser => "*firefox", 
     :url => "http://localhost:3000/", 
     :timeout_in_second => 60 
     #return @selenium_driver 
    end 

正如你可以看到上面我也感動了selenium_driver設置代碼到spec_helper.rb文件清理我的代碼,使之更加乾燥:

這裏是我的集成測試文件:

require 'spec_helper' 

describe "Board form" do 
    attr_reader :selenium_driver 
    alias :page :selenium_driver 

    before(:all) do 
    selenium_setup 
    @selenium_driver.start_new_browser_session 
    end 

    after(:all) do 
    @selenium_driver.close_current_browser_session 
    @verification_errors.should == [] 
    end 

    describe "create board" do 
     describe "failure" do 
     it "test_ home page form" do 
      page.open "/" 
      ("Happy Birthday Greetings | Home").should == page.get_title 
      page.is_element_present("board_bp_name").should be_true 
      page.is_text_present("Name").should be_true 
      ("Happy Birthday Greetings | Home").should == page.get_title 
      page.click "board_submit" 
      wait_for_ajax 
      page.is_element_present("board_bp_name").should be_true 
      page.is_text_present("Name").should be_true 
      page.is_element_present("board_birthday_1i").should be_true 
      page.is_element_present("board_submit").should be_true 
      page.is_text_present("exact:Oops, looks like 1 error occured: \n Hey whose birthday is it? Please enter a name.").should be_true 
      page.is_element_present("error_explanation").should be_true 
     end 
     end 

     describe "success" do 

     it "should create a new board for a properly filled in form and show the correct flash message" do 
      page.open "/" 
      page.type "board_bp_name", "Test User" 
      page.select "board_birthday_2i", "label=October" 
      page.select "board_birthday_1i", "label=1967" 
      page.select "board_birthday_3i", "label=7" 
      page.click "board_submit" 
      wait_for_ajax 
      page.wait_for_page_to_load("30000") 
      page.get_location().should =~ /boards\/\d/i 
      page.is_element_present("css=div.flash.success").should be_true 
      page.is_text_present("Your friend's board has been created.").should be_true 
      page.is_text_present("Test User").should be_true 
      page.is_text_present("43").should be_true 
      page.is_element_present("greeting_link").should be_true 
      page.is_text_present("Add greeting").should be_true 
     end 
     end 
    end 
    end