Acceptance testing with Rspec, Selenium web-driver, Rack session and Capybara

Acceptance testing, also known as user-acceptance testing or end-user testing is a formal testing that is conducted to ensure that the behavior of an application meets acceptance criteria, users’ needs and the quality of outputs expected when certain activities are carried out on an application. It helps to verify or ensure that a provided solution works for the user and tests performance aligns with what would occur in real life scenarios.
In rails applications, there are different acceptance testing tools but the specific tools to be discussed in this article are Rspec, Selenium web-driver, rack session and capybara. These tools are used within rails project environment.
Rspec is a BDD(Behavior driven development) framework. It helps to design, describe, and test the behavior of your code-whether you’re new to testing tools or an experienced developer. In rails application, Rspec is installed with:
gem install rspecInitialize the spec/ directory (where specs will reside) with:
rails generate rspec:installThis adds the following files which are used for configuration:
.rspecspec/spec_helper.rbspec/rails_helper.rb
In rails, Rspec has two helpers, spec_helper.rb and rails_helper.rb. spec_helper.rb is the conventional RSpec configuration helper, while the Rails specific loading and bootstrapping is configured in rails_helper.rb file. When Rspec gem is installed in a Rails app, RSpec will generate spec files when commands like rails generate model and rails generate controller are run.
Rack session-access is a middleware that supports a user in managing sessions independently. There are different sessions supported by rack session.
- page.set_rack_session: sets your desired session data.
- config.middleware.use RackSessionAccess::Middleware: provides access to rack session
An example snippet of a rack_session_access middleware is shown here:
[ExampleRailsApp]::Application.configure do |config|
...
# Access to rack session
config.middleware.use RackSessionAccess::Middleware
...
endImportant! Ensure you include rack_session_access middleware only in test environment to avoid having security issues.
Capybara is a Behavior Driven Development test automation software that provides DSL(Domain-specific Language) which is used to describe actions executed by a web driver. It is designed to extend the mostly readable style of BDD frameworks such as cucumber and Rspec into the automation code itself.
When the webpage is loaded using the DSL (and maybe selenium web driver), Capybara will try to locate the relevant element in the DOM(Document Object Model) and execute a proposed action, such as click button or link. It also simulates scenarios for user stories.
scenario "Login to an example app" do
visit '/sessions/new'
within("#session") do
fill_in 'Email', with: 'test.user@example.com'
fill_in 'Password', with: 'password'
end
click_button 'Sign in'
expect(page).to have_content 'Success'
endSelenium web-driver makes direct calls to the browser using each browser’s native support for automation. When using capybara with selenium web-driver, you need rack session-access to change session values. The code injected should handle when a session is set, therefore after an activity has been carried out, the session expires.
In spec/spec_helper.rb
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
endCapybara.configure do |config|config.default_max_wait_time = 10 # seconds
config.default_driver = :selenium
end
There you have it readers! For more information on practical use of these acceptance testing tools, visit this link and have fun testing your features.
