Running Selenium RSpec tests for Rails 2.3.5
Sauce AI for Test Authoring: Move from intent to execution in mo.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in mo.
|
x
This article will get you setup with the bare-minimum environment to run Selenium tests with RSpec, for automated, full-stack examination of Rails apps. Once that & # x27; s done, we & # x27; ll employment on polishing it for a nicer experience. In this post you find uniting of mostly outdated articles from around the internet updated to work with a modernistic rails system. It & # x27; s written on a clear surround courtesy ofrvm, so you may have some of the stone already setup.
A new Rails app, RSpec, and rspec-rails
First off, we & # x27; ll start with a new runway project:
gem install rails -- no-ri -- no-rdoc
rails rspec_saucerc
gem install rspec
gem install rspec-rails
Edit config/environment.rb and add:
config.gem & quot; rspec & quot;,: lib = & gt; false,: version = & gt; & quot; & gt; = 1.2.9 & quot;
config.gem & quot; rspec-rails & quot;,: lib = & gt; mistaken,: version = & gt; & quot; & gt; = 1.2.9 & quot;
This sets up all the required files for RSpec to get off the ground, but we need to integrate it into Rails:
script/generate rspec
That will add the profligate tasks, create the appropriate directories, and introductory files. Now let & # x27; s get Selenium up and running!
Selenium
We & # x27; ll need to create a freestanding selenium database. in config/database.yml, let & # x27; s add:
selenium:
adapter: sqlite3
database: db/selenium.sqlite3
encoding: utf8
timeout: 5000
To get Selenium lam under our RSpec level, we & # x27; ll demand the Selenium gem (note the capital & quot; S & quot; — it & # x27; s case-sensitive). Let & # x27; s also make sure we have the sqlite3 gem installed, and then prepare an appropriate environment for our selenium tests:
gem install Selenium
gem install sqlite3-ruby
cp config/environments/test.rb config/environments/selenium.rb
Unfastened config/environments/selenium.rb and remove the final line that reads:
config.gem & # x27; rspec-rails & # x27;,: version = & gt; & # x27; & gt; = 1.3.2 & # x27;,: lib = & gt; false unless File.directory? (File.join (Rails.root, & # x27; vendor/plugins/rspec-rails & # x27;))
Sauce RC and Selenium RC
Sauce RC will broker all communication between railing and any browsers you might be driving. Get it from our page for Windows or Mac, or useSelenium RCfor Linux, and start it up so we can run our tests.
Testables
SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.
We & # x27; ll need some fodder to test. Use the RSpec generator, then create and migrate the database:
script/generate rspec_scaffold person name: string age: string language: twine
rake RAILS_ENV=selenium db: create
rake RAILS_ENV=selenium db: migrate
Startup our test waiter
Rails tests do not normally bind to a webserver, so there is no way for Selenium to access the frontend. As a irregular workaround, we & # x27; ll manually evoke a Rails host employ the selenium environs:
script/server -e selenium
Our spec helper
We will be using the Selenium gem, so let & # x27; s add it to the spec benefactor:
gem & quot; selenium-client & quot;
require & quot; selenium/client & quot;
require & quot; selenium/rspec/spec_helper & quot;
Example story
We & # x27; ll gloss over which types of tests should include selenium front-end testing for now, and only say it belongs in integration tests. Let & # x27; s run an example story that will fail the first clip through, and we & # x27; ll then fix it. Put the following in spec/integration/people_spec.rb:
require & # x27; spec_helper & # x27;
describe & quot; People & quot; do
before (: all) do
@ verification_errors = []
@ browser = Selenium: :Client: :Driver.new (
: horde = & gt; & quot; localhost & quot;,
: porthole = & gt; 4444,
: browser = & gt; & quot; * firefox & quot;
: url = & gt; & quot; http: //localhost:3000 & quot;,
: 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 & quot; should create a new Person with valid input & quot; do
@ browser.open & quot; /people & quot;
@ browser.click & quot; link=New mortal & quot;
@ browser.wait_for_page_to_load & quot; 2000 & quot;
@ browser.type & quot; person_name & quot;, & quot; Jason Huggins & quot;
@ browser.type & quot; person_age & quot;, & quot; 26 & quot;
@ browser.type & quot; person_language & quot;, & quot; Albanian & quot;
@ browser.click & quot; person_submit & quot;
@ browser.wait_for_page_to_load & quot; 30000 & quot;
@ browser.is_alert_present.should be_true
end
end
Let & # x27; s try it out:
rake spec: integrating
F
1) & # x27; People should make a new Person with valid input & # x27; FAILED
expected mistaken to be true
./spec/integration/people_spec.rb:22:
Finished in 7.443904 mo
1 example, 1 failure
It & # x27; s failing as we expected it to (there shouldn & # x27; t be a javascript alert on submit). Let & # x27; s change the last line:
@ browser.is_alert_present.should be_false
... and try our test again:
rakehell specification: integration
.
Finished in 7.079062 bit
1 example, 0 failure
Looks full! We finally receive a working rail project with rspec runners and Selenium.
Weaknesses
Although we & # x27; ve got Rails, RSpec, and Selenium all work together, the relationship is not proportionate. Machine-controlled test in rails are not meant to function the outside world, and as such they don & # x27; t bind to a webserver/port. Tests are run within the same process, which makes them quite fast, but prevents us from utilize 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 routine of reasons:
We experience to manually start/stop the test host that selenium wants to access
We have to manually create the selenium environment & # x27; s database
We have to manually migrate the selenium environment each time there & # x27; s a schema change
Worse, we have to manually reset the database after each run
Running Selenium tests in series isslow
We ‘ ve acquire a plugin to automatize some of these issue, phoneSpecStorm. We & # x27; ll go over install it to get the almost out of your tests (including scarper them in parallel) in the future post. Notes: Selenium matchers are case-sensitive: @ browser.click & quot; link=New Person & quot; won & # x27; t mate a tie with & quot; New mortal & quot;
Share this post
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free