Thursday, December 11, 2008

Glimmer's Got BDD through RSpec and SWTBot

I got an RSpec Story Runner (Cucumber) test passing with Glimmer code that tests whether a user logs in successfully.

RSpec is a Ruby library that encourages BDD (Behavior Driven Development,) enabling you to drive the functionality of your application from the outside in by specifying the external behavior from the user's point of view and in the user's language.

Here is the test I got passing with Glimmer:


Feature: Login feature
In order to keep user data secure
As a user
I would like to be authenticated before I can access my data

Scenario: Login
Given I fill in "User" for "Username"
And I fill in "Pass" for "Password"
When I "Login"
Then my status should become "Logged In"


It uses SWTBot behind the scenes to execute the scenario steps, such as filling in text fields and pressing the login button.

Here is the user-interface:



Here is the user-interface Glimmer code relying on data-binding:


@shell = shell {
text "Login"
composite {
layout GridLayout.new(2, false) #two columns with differing widths

label { text "Username:" } # goes in column 1
text { # goes in column 2
text bind(presenter, :user_name)
enabled bind(presenter, :logged_out)
}

label { text "Password:" }
text(SWT::PASSWORD | SWT::BORDER) {
text bind(presenter, :password)
enabled bind(presenter, :logged_out)
}

label { text "Status:" }
label { text bind(presenter, :status) }

button {
text "Login"
enabled bind(presenter, :logged_out)
on_widget_selected { presenter.login }
}

button {
text "Logout"
enabled bind(presenter, :logged_in)
on_widget_selected { presenter.logout }
}
}
}
@shell.open


In a future post, I will discuss the technical details of how this works.

4 comments:

Nicolas Marchildon said...

wow! Is it possible to test an existing Java SWT application with Cucumber and SWTBot?

Andy Maleh said...

That's the point Nicolas. I gave my code to another developer who is testing out the idea. If I hear back from him about his results, I'll write a blog post about it.

Philipp Kursawe said...

Whats the progress on this? I would like to use cucumber to drive my Java BDD. How do you use JRuby to instantiate java classes in Ruby to perform cucumber steps?
I would be interested to see your SWTBot steps that you have written.

Andy Maleh said...

I have some code that is not polished and needs extra setup to make it work on your machine, but I can commit anyways under the Glimmer project if you need it. I will let you know once I have done so.