Sunday, 6 April 2014

Spree and using Integration Specs to automate samples.

Spreecommerce is a completely opensource e-commerce storefront & backend written in Ruby on Rails. Getting a storefront running is about a 10 minute project, after that you can spend days understanding all the configurations and extensions. Trust me it is worth it.

As Spree runs as an engine, there is not much testing you need to write. After setting up custom Payment gateways in seed data and importing the customers existing clients and orders into spree. To test this I would end up going to the storefront webpage:-
  • select and item
  • click add to cart
  • click checkout
  • click new user
  • fill in details plus address
  • enter test credit card details
  • you get the idea....
And then repeat to test adding for an existing client.

After multiple times of copying and pasting in test mastercard numbers it became evident that I needed to  automate this.

Enter Integrations Specs…

This is not your typical spec as it has multiple expectations and is very long, the goal here is to run a full order process and the expectations are just there to let you know where it might of failed. Spree uses JS for many of its pages and could not select the state (‘Queensland’) unless this was run using Selenium. To do this, note the js: true

  it 'Check full payment order process', js: true do

This will open and run the script inside a web browser (normally firefox, but it is configurable). 

to use this your gemfile will need:-

  gem "capybara"
  gem 'selenium-webdriver'
The other important change is in spec_helper is right at the top, this line is change to 

  ENV["RAILS_ENV"] = ‘development'
note: that the ||= was changed to =, this could also just be added only in the integration spec so it does not effect any other specs.

and add below within the config block:-

  config.include Capybara::DSL

Now that we have this set we can actually run the script and it will create an entry in our development system, allowing you to use and see this order within the Spree admin system. Another advantage is that you can use and test the seed data you have created for a project.

Extra tips…

When the script completes it will close the browser window it opened. It is possible to change this behaviour but you end up with lots of open browser windows so its not recommended. Instead if you are having an issue and want to see what is happening, put a the following at the point where you want to debug:-


which will make the scrip sleep for 30 seconds allowing you to look at the failing screen.

Also remember you can dump the screens html by using:-

  p page.body

or using this gem, do screenshots:-


within the integration spec.

Have fun!

No comments:

Post a Comment