Skip to content

Instantly share code, notes, and snippets.

@gabrielfalcao
Created December 21, 2011 23:17
Show Gist options
  • Save gabrielfalcao/1508156 to your computer and use it in GitHub Desktop.
Save gabrielfalcao/1508156 to your computer and use it in GitHub Desktop.
sample lettuce step definitions
from lettuce import *
from lettuce.django import django_url
from splinter.browser import Browser
PAGES = {
"the login page": "/login",
"the user management page": "/manage/users"
}
def page_name_is_valid(name):
assert PAGES.has_key(name), \
'the page "%s" is not mapped in the PAGES dictionary, ' \
'check if you misspelled it or add into it' % name
return True
@before.each_scenario
def prepare_browser(scenario):
world.browser = Browser()
@step(ur'I am at (.*)')
def i_am_at_some_url(step, name):
assert page_name_is_valid(name)
full_url = django_url(PAGES[name])
world.browser.visit(full_url)
@step(ur'I type the (\S+) "(.*)"')
def i_type_some_value_into_a_field(step, field, value):
browser.fill(field, value)
@step(ur'I try to perform the login')
def try_to_perform_login(step, field, value):
browser.find_by_css("button#submit-login").click()
@step(ur'it works and I am redirected to (.*)')
def it_works_and_im_redirected_to(step, name):
assert page_name_is_valid(name)
current_url = world.browser.url
full_url = django_url(PAGES[name])
assert current_url == full_url, \
'the current url is "%s" but should be "%s"' % (current_url, full_url)
@step(ur'I log in as "(\w+)" with password "(.*)"')
def i_log_in_as(step, username, password):
step.behave_as(ur'''
Given I am at the login page
When I type the username "%s"
And I type the password "%s"
And I try to perform the login
Then it works and I am redirected to the admin page
''' % (username, password))
@step(ur'I click to manage users')
def try_to_perform_login(step, field, value):
browser.find_by_css("button#manage-users").click()
@step(ur'I am pointed out to (.*)')
def im_pointed_out_to(step, name):
step.then("it works and I am redirected to %s" % name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment