<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Selenium Testing? Do Cross Browser Testing with Sauce Labs &#187; rails</title>
	<atom:link href="http://saucelabs.com/blog/index.php/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://saucelabs.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 10 May 2012 17:20:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Running Selenium RSpec tests for Rails 2.3.5</title>
		<link>http://saucelabs.com/blog/index.php/2010/02/running-selenium-tests-for-rails/</link>
		<comments>http://saucelabs.com/blog/index.php/2010/02/running-selenium-tests-for-rails/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 20:03:24 +0000</pubDate>
		<dc:creator>The Sauce Labs Team</dc:creator>
				<category><![CDATA[Selenium Knowledge]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[rspec-rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://saucelabs.com/blog/?p=349</guid>
		<description><![CDATA[This article will get you setup with the bare-minimum environment to run Selenium tests with RSpec, for automated, full-stack testing of Rails apps. Once that&#8217;s done, we&#8217;ll work on polishing it for a nicer experience. In this post you find amalgamations of mostly outdated articles from around the internet updated to work with a modern [...]]]></description>
			<content:encoded><![CDATA[<p>This article will get you setup with the bare-minimum environment to run Selenium tests with RSpec, for automated, full-stack testing of Rails apps. Once that&#8217;s done, we&#8217;ll work on polishing it for a nicer experience. In this post you find amalgamations of mostly outdated articles from around the internet updated to work with a modern rails system.</p>
<p>It&#8217;s written on a clean environment courtesy of <a href="http://rvm.beginrescueend.com/">rvm</a>, so you may have some of the gems already setup.</p>
<h3>A new Rails app, RSpec, and rspec-rails</h3>
<p>First off, we&#8217;ll start with a new rails project:</p>
<pre class="brush:bash">gem install rails --no-ri --no-rdoc
rails rspec_saucerc
gem install rspec
gem install rspec-rails</pre>
<p>Edit config/environment.rb and add:</p>
<pre class="brush:ruby">  config.gem "rspec", :lib =&gt; false, :version =&gt; "&gt;= 1.2.9"
  config.gem "rspec-rails", :lib =&gt; false, :version =&gt; "&gt;= 1.2.9"</pre>
<p>This sets up all the required files for RSpec to get off the ground, but we need to integrate it into Rails:</p>
<pre class="brush:bash">script/generate rspec</pre>
<p>That will add the rake tasks, create the appropriate directories, and basic files. Now let&#8217;s get Selenium up and running!</p>
<h3>Selenium</h3>
<p>We&#8217;ll need to create a separate selenium database. in config/database.yml, let&#8217;s add:</p>
<pre class="brush:plain">selenium:
  adapter: sqlite3
  database: db/selenium.sqlite3
  encoding: utf8
  timeout: 5000</pre>
<p>To get Selenium running under our RSpec stories, we&#8217;ll need the Selenium gem (note the capital &#8220;S&#8221; — it&#8217;s case-sensitive). Let&#8217;s also make sure we have the sqlite3 gem installed, and then prepare an appropriate environment for our selenium tests:</p>
<pre class="brush:bash">gem install Selenium
gem install sqlite3-ruby
cp config/environments/test.rb config/environments/selenium.rb</pre>
<p>Open config/environments/selenium.rb and remove the last line that reads:</p>
<pre class="brush:ruby">config.gem 'rspec-rails', :version =&gt; '&gt;= 1.3.2', :lib =&gt; false unless File.directory?(File.join(Rails.root, 'vendor/plugins/rspec-rails'))</pre>
<h3>Sauce RC and Selenium RC</h3>
<p>Sauce RC will broker all communication between rails and any browsers you might be driving. Get it from our <a href="http://saucelabs.com/products/downloads">downloads</a> page for Windows or Mac, or use <a href="http://seleniumhq.org/projects/remote-control/">Selenium RC</a> for Linux, and start it up so we can run our tests.</p>
<h3>Testables</h3>
<p>We&#8217;ll need some fodder to test. Use the RSpec generator, then create and migrate the database:</p>
<pre class="brush:bash">script/generate rspec_scaffold person name:string age:string language:string
rake RAILS_ENV=selenium db:create
rake RAILS_ENV=selenium db:migrate</pre>
<h3>Startup our test server</h3>
<p>Rails tests do not normally bind to a webserver, so there is no way for Selenium to access the frontend. As a temporary workaround, we&#8217;ll manually invoke a Rails server using the selenium environment:</p>
<pre class="brush:bash">script/server -e selenium</pre>
<h3>Our spec helper</h3>
<p>We will be using the Selenium gem, so let&#8217;s add it to the spec helper:</p>
<pre class="brush:ruby">gem "selenium-client"
require "selenium/client"
require "selenium/rspec/spec_helper"</pre>
<h3>Example story</h3>
<p>We&#8217;ll gloss over which types of tests should include selenium front-end testing for now, and just say it belongs in integration tests. Let&#8217;s run an example story that will fail the first time through, and we&#8217;ll then fix it.</p>
<p>Put the following in spec/integration/people_spec.rb:</p>
<pre class="brush:ruby">require 'spec_helper'
describe "People" do
  before(:all) do
    @verification_errors = []

    @browser = Selenium::Client::Driver.new(
      :host =&gt; "localhost",
      :port =&gt; 4444,
      :browser =&gt; "*firefox"
      :url =&gt; "http://localhost:3000",
      :timeout_in_second =&gt; 90)

    @browser.start
  end

  before(:each) do
    @browser.start_new_browser_session
  end

  append_after(:each) do
    @browser.close_current_browser_session
    @verification_errors.should == []
  end

  it "should create a new Person with valid input" do
    @browser.open "/people"
    @browser.click "link=New person"
    @browser.wait_for_page_to_load "2000"
    @browser.type "person_name", "Jason Huggins"
    @browser.type "person_age", "26"
    @browser.type "person_language", "Albanian"
    @browser.click "person_submit"
    @browser.wait_for_page_to_load "30000"
    @browser.is_alert_present.should be_true
  end
end</pre>
<p>Let&#8217;s try it out:</p>
<pre class="brush:bash">rake spec:integration
F
1) 'People should create a new Person with valid input' FAILED
expected false to be true
./spec/integration/people_spec.rb:22:

Finished in 7.443904 seconds

1 example, 1 failure</pre>
<p>It&#8217;s failing as we expected it to (there shouldn&#8217;t be a javascript alert on submit). Let&#8217;s change the last line:</p>
<pre class="brush:ruby">    @browser.is_alert_present.should be_false</pre>
<p>&#8230; and try our test again:</p>
<pre class="brush:bash">rake spec:integration
.

Finished in 7.079062 seconds

1 example, 0 failures</pre>
<p>Looks good! We finally have a working rails project with rspec runners and Selenium.</p>
<h3>Weaknesses</h3>
<p>Although we&#8217;ve got Rails, RSpec, and Selenium all working together, the relationship is not harmonious.</p>
<p>Automated tests in rails are not meant to serve the outside world, and as such they don&#8217;t bind to a webserver/port. Tests are run within the same process, which makes them quite fast, but prevents us from using browser-based frontend tools such as Selenium. To get around that, we manually fired up a rails server instance with the selenium environment, but this is messy for a number of reasons:</p>
<ul>
<li>We have to manually start/stop the test server that selenium wants to access</li>
<li>We have to manually create the selenium environment&#8217;s database</li>
<li>We have to manually migrate the selenium environment each time there&#8217;s a schema change</li>
<li>Worse, we have to manually reset the database after each run</li>
<li>Running Selenium tests in serial is <em>slow</em></li>
</ul>
<p>We‘ve developed a plugin to automate some of these issues, called <a href="http://github.com/sgrove/spec_storm">SpecStorm</a>. We&#8217;ll go over installing it to get the most out of your tests (including running them in parallel) in the next post.</p>
<p>Notes:</p>
<p>Selenium matchers are case-sensitive: @browser.click &#8220;link=New Person&#8221; won&#8217;t match a link with &#8220;New person&#8221;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsaucelabs.com%2Fblog%2Findex.php%2F2010%2F02%2Frunning-selenium-tests-for-rails%2F&amp;title=Running%20Selenium%20RSpec%20tests%20for%20Rails%202.3.5" id="wpa2a_2"><img src="http://saucelabs.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://saucelabs.com/blog/index.php/2010/02/running-selenium-tests-for-rails/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Creating Selenium Tests for Mature Ruby on Rails Project</title>
		<link>http://saucelabs.com/blog/index.php/2009/12/creating-selenium-tests-for-mature-ruby-on-rails-project/</link>
		<comments>http://saucelabs.com/blog/index.php/2009/12/creating-selenium-tests-for-mature-ruby-on-rails-project/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 12:16:13 +0000</pubDate>
		<dc:creator>John Dunham</dc:creator>
				<category><![CDATA[Selenium Knowledge]]></category>
		<category><![CDATA[Functional testing]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://saucelabs.com/blog/?p=290</guid>
		<description><![CDATA[Consultant Sarah Mei talks about effectively using outsourcing to &#8216;catch up&#8217; implementing Selenium tests on an existing Ruby on Rails project.]]></description>
			<content:encoded><![CDATA[<p>Consultant Sarah Mei talks about effectively using outsourcing to &#8216;catch up&#8217; implementing Selenium tests on an existing Ruby on Rails project.</p>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/YTZzx2qp-R8&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/YTZzx2qp-R8&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsaucelabs.com%2Fblog%2Findex.php%2F2009%2F12%2Fcreating-selenium-tests-for-mature-ruby-on-rails-project%2F&amp;title=Creating%20Selenium%20Tests%20for%20Mature%20Ruby%20on%20Rails%20Project" id="wpa2a_4"><img src="http://saucelabs.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://saucelabs.com/blog/index.php/2009/12/creating-selenium-tests-for-mature-ruby-on-rails-project/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

