2011-06-29 67 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