Draiken
Draiken

Reputation: 3815

Testing mobile and desktop versions of the same site with Cucumber

I have a site that completely differs on the front-end between the mobile and desktop versions.

You cannot scope steps when defining them on Cucumber, so I am stuck with two solutions (as I see):

None of this seem optimal to me.

Is there a better/cleaner way to do this?

Upvotes: 0

Views: 708

Answers (2)

Draiken
Draiken

Reputation: 3815

I ended up using a solution similar to Jon M.

For the concurring steps I made a "meta" step, that passes on to the correct context. Here is an example:

When /^I log in$/ do
  step %Q{I log in on #{@context}}
end

Then in another file (separated by context) I have:

When /^I log in on mobile$/ do
  # do stuff
end

To setup the context, I created a support module. Basically, it keeps the current context and also has a default one, that you can change with a step:

Given /^I am on the "(.*?)" version$/ do |version|
  @context = version
end

It's not exactly how I setup the context part, but you get the picture.

The advantage in this, is that I can have infinite contexts and this "meta" steps will pick them up. I also don't have bloated steps. Each step stays simple and only deals with it's own context logic.

The downside is that for each non-default context I have, I need a Background stating that I am on a different context.

I won't accept a right answer here, since there is no such thing. You can deal with it with different approaches and until now, there isn't even a best practice about this :)

Upvotes: 0

Jon M
Jon M

Reputation: 11705

You can use tags and hooks for this, tag each scenario/feature with the version of the site it tests:

@mobile
Scenario: Logging in on mobile
  Given I visit the login page

@desktop
Scenario: Logging in on desktop
  Given I visit the login page

Use some hooks to set a variable indicating the version of the site being tested:

Before '@mobile' do
  @version = :mobile
end

Before '@desktop' do
  @version = :desktop
end

Then in your steps:

Given /^I visit the login page$/ do
  if @version == :desktop
    # Desktop specific code
  elsif
    # Mobile specific code
  else
    raise "Don't know what to do!"
  end
end

Upvotes: 1

Related Questions