Cucumber + Webrat + Selenium guide

前端 未结 1 1002
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 09:15

I have been using Cucumber and Webrat for a while. I now need to start writing behaviour that involve AJAX interactions so I was thinking to use the Selenium adapter for Web

相关标签:
1条回答
  • 2020-12-28 09:39

    I'm using Selenium with rspec on my project and generate code from a custom formatter for Selenium IDE.

    There is many selenium for rails but i success using Selenium-RC http://seleniumhq.org/download/ , so download to your pc.

    Here are my steps:

    1. Unzip and run> java -jar selenium-server.jar
    2. Open selenium-client-ruby, read the doc, follow it you will get success!
    3. gem install rspec, rspec-rails version 1.2.6 (it not, you need to comment version restrict of selenium-client source code)
    4. gem install selenium-client
    5. Open Selenium-IDE (Firefox of course), Open Options -> Options -> Formats
    6. Click Add, and paste this code in http://www.techdarkside.com/rspec_export.txt

    Now, You just export spec to your spec folder for me, I use spec/features/xxxx_spec.rb see code below.

    Very similar approach can find at here

    For webrat+cucumber, the latest Rspec book will give all you need. (They don't have selenium + cucumber chapter finish yet)

    example

     require 'rubygems'
    gem "rspec", "=1.2.6"
    gem "selenium-client", ">=1.2.15"
    require "selenium/client"
    require "selenium/rspec/spec_helper"
    
    describe "Google Search" do
        attr_reader :selenium_driver
        alias :page :selenium_driver
    
      before(:all) do
          @selenium_driver = Selenium::Client::Driver.new \
              :host => "localhost",
              :port => 4444,
              :browser => "*firefox",
              :url => "http://www.google.com",
              :timeout_in_second => 60
      end
    
      before(:each) do
        selenium_driver.start_new_browser_session
      end
    
      # The system capture need to happen BEFORE closing the Selenium session
      append_after(:each) do
        @selenium_driver.close_current_browser_session
      end
    
      it "can find Selenium" do
        page.open "/"
        page.title.should eql("Google")
        page.type "q", "Selenium seleniumhq"
        page.click "btnG", :wait_for => :page
        page.value("q").should eql("Selenium seleniumhq")
        page.text?("seleniumhq.org").should be_true
        page.title.should eql("Selenium seleniumhq - Google Search")
        page.text?("seleniumhq.org").should be_true
                page.element?("link=Cached").should be_true
      end
    
    end
    
    0 讨论(0)
提交回复
热议问题