Created
March 6, 2012 14:41
-
-
Save samir/1986620 to your computer and use it in GitHub Desktop.
Usefull cucumber steps in portuguese
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
# Commonly used email steps | |
# | |
# To add your own steps make a custom_email_steps.rb | |
# The provided methods are: | |
# | |
# last_email_address | |
# reset_mailer | |
# open_last_email | |
# visit_in_email | |
# unread_emails_for | |
# mailbox_for | |
# current_email | |
# open_email | |
# read_emails_for | |
# find_email | |
# | |
# General form for email scenarios are: | |
# - clear the email queue (done automatically by email_spec) | |
# - execute steps that sends an email | |
# - check the user received an/no/[0-9] emails | |
# - open the email | |
# - inspect the email contents | |
# - interact with the email (e.g. click links) | |
# | |
# The Cucumber steps below are setup in this order. | |
module EmailHelpers | |
def parse_email_count(amount) | |
case amount | |
when 'um' then 1 | |
when 'nenhum' then 0 | |
else super(amout) | |
end | |
end | |
def current_email_address | |
# Replace with your a way to find your current email. e.g @current_user.email | |
# last_email_address will return the last email address used by email spec to find an email. | |
# Note that last_email_address will be reset after each Scenario. | |
last_email_address || "[email protected]" | |
end | |
end | |
World(EmailHelpers) | |
# | |
# Reset the e-mail queue within a scenario. | |
# This is done automatically before each scenario. | |
# | |
Dado /^(?:que não há emails|que nenhum email foi enviado)$/ do | |
reset_mailer | |
end | |
# | |
# Check how many emails have been sent/received | |
# | |
Então /^(?:eu|ele|"([^\"]*?)") deveria receber (um|nenhum|\d+) emails?$/ do |address, amount| | |
unread_emails_for(address).size.should == parse_email_count(amount) | |
end | |
Então /^(?:eu|ele|"([^\"]*?)") deveria receber (um|nenhum|\d+) emails? com o assunto "([^\"]*?)"$/ do |address, amount, subject| | |
unread_emails_for(address).select { |m| m.subject =~ Regexp.new(Regexp.escape(subject)) }.size.should == parse_email_count(amount) | |
end | |
Então /^(?:eu|ele|"([^\"]*?)") deveria receber (um|nenhum|\d+) emails? com o assunto \/(.*?)\/$/ do |address, amount, subject| | |
unread_emails_for(address).select { |m| m.subject =~ Regexp.new(subject) }.size.should == parse_email_count(amount) | |
end | |
Então /^(?:eu|ele|"([^\"]*?)") deveria receber um email com o corpo:$/ do |address, expected_body| | |
open_email(address, :with_text => expected_body) | |
end | |
# | |
# Accessing emails | |
# | |
# Opens the most recently received email | |
Quando /^(?:eu|ele|"([^\"]*?)") abr(?:e|o|ir) o email$/ do |address| | |
open_email(address) | |
end | |
Quando /^(?:eu|ele|"([^\"]*?)") abr(?:e|o|ir) o email com o assunto "([^\"]*?)"$/ do |address, subject| | |
open_email(address, :with_subject => subject) | |
end | |
Quando /^(?:eu|ele|"([^\"]*?)") abr(?:e|o|ir) o email com o assunto \/(.*?)\/$/ do |address, subject| | |
open_email(address, :with_subject => Regexp.new(subject)) | |
end | |
Quando /^(?:eu|ele|"([^\"]*?)") abr(?:e|o|ir) o email com o texto "([^\"]*?)"$/ do |address, text| | |
open_email(address, :with_text => text) | |
end | |
Quando /^(?:eu|ele|"([^\"]*?)") abr(?:e|o|ir) o email com o texto \/(.*?)\/$/ do |address, text| | |
open_email(address, :with_text => Regexp.new(text)) | |
end | |
# | |
# Inspect the Email Contents | |
# | |
Então /^(?:|eu |ele )deveria ver "([^\"]*?)" no assunto do email$/ do |text| | |
current_email.should have_subject(text) | |
end | |
Então /^(?:|eu |ele )deveria ver \/(.*?)\/ no assunto do email$/ do |text| | |
current_email.should have_subject(Regexp.new(text)) | |
end | |
Então /^(?:|eu |ele )deveria ver "([^\"]*?)" no corpo do email$/ do |text| | |
current_email.default_part_body.to_s.should include(text) | |
end | |
Então /^(?:|eu |ele )deveria ver \/(.*?)\/ no corpo do email$/ do |text| | |
current_email.default_part_body.to_s.should =~ Regexp.new(text) | |
end | |
Então /^(?:|eu |ele )deveria ver que o email foi enviado (?:por|pelo) "([^\"]*?)"$/ do |text| | |
current_email.should be_delivered_from(text) | |
end | |
Então /^(?:|eu |ele )deveria ver "([^\"]*)" no cabeçalo "([^\"]*?)" do email$/ do |text, name| | |
current_email.should have_header(name, text) | |
end | |
Então /^(?:|eu |ele )deveria ver \/(.*)\/ no cabeçalo "([^\"]*?)" do email$/ do |text, name| | |
current_email.should have_header(name, Regexp.new(text)) | |
end | |
Então /^eu deveria ver um email multi\-part$/ do | |
current_email.should be_multipart | |
end | |
Então /^(?:I|they) should see "([^\"]*?)" in the email html part body$/ do |text| | |
current_email.html_part.body.to_s.should include(text) | |
end | |
Então /^(?:I|they) should see "([^\"]*?)" in the email text part body$/ do |text| | |
current_email.text_part.body.to_s.should include(text) | |
end | |
Então /^(?:eu|ele) deveria ver "([^\"]*?)" no corpo html do email$/ do |text| | |
current_email.html_part.body.to_s.should include(text) | |
end | |
Então /^(?:eu|ele) deveria ver "([^\"]*?)" no corpo texto do email$/ do |text| | |
current_email.text_part.body.to_s.should include(text) | |
end | |
# | |
# Inspect the Email Attachments | |
# | |
Então /^(?:eu|ele) deveria ver (um|nenhum|\d+) anexos? no email$/ do |amount| | |
current_email_attachments.size.should == parse_email_count(amount) | |
end | |
Então /^deveria haver (um|nenhum|\d+) anexos? com o nome "([^\"]*?)"$/ do |amount, filename| | |
current_email_attachments.select { |a| a.filename == filename }.size.should == parse_email_count(amount) | |
end | |
Então /^o anexo (\d+) deveria se chamar "([^\"]*?)"$/ do |index, filename| | |
current_email_attachments[(index.to_i - 1)].filename.should == filename | |
end | |
Então /^deveria haver (um|nenhum|\d+) anexos? com a extenção "([^\"]*?)"$/ do |amount, content_type| | |
current_email_attachments.select { |a| a.content_type.include?(content_type) }.size.should == parse_email_count(amount) | |
end | |
Então /^o anexo (\d+) deveria ter a extenção "([^\"]*?)"$/ do |index, content_type| | |
current_email_attachments[(index.to_i - 1)].content_type.should include(content_type) | |
end | |
Então /^nenhum anexo deveria ser vazio$/ do | |
current_email_attachments.each do |attachment| | |
attachment.read.size.should_not == 0 | |
end | |
end | |
Então /^mostre me a lista de anexos do email$/ do | |
EmailSpec::EmailViewer::save_and_open_email_attachments_list(current_email) | |
end | |
# | |
# Interact with Email Contents | |
# | |
Quando /^(?:eu|ele) sigo o link "([^\"]*?)" no email$/ do |link| | |
visit_in_email(link) | |
end | |
Quando /^(?:eu|ele) clico no primeiro link do email$/ do | |
click_first_link_in_email | |
end | |
# | |
# Debugging | |
# These only work with Rails and OSx ATM since EmailViewer uses RAILS_ROOT and OSx's 'open' command. | |
# Patches accepted. ;) | |
# | |
Then /^save and open current email$/ do | |
EmailSpec::EmailViewer::save_and_open_email(current_email) | |
end | |
Then /^save and open all text emails$/ do | |
EmailSpec::EmailViewer::save_and_open_all_text_emails | |
end | |
Then /^save and open all html emails$/ do | |
EmailSpec::EmailViewer::save_and_open_all_html_emails | |
end | |
Then /^save and open all raw emails$/ do | |
EmailSpec::EmailViewer::save_and_open_all_raw_emails | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# Extracted and modified from: | |
# https://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/step_definitions.rb | |
# | |
# Add the functionality to search the attributes names from localization files, and translate them | |
# to machine language. | |
module FactoryGirlStepHelpers | |
def convert_human_hash_to_attribute_hash(factory, human_hash, associations = []) | |
HumanHashToAttributeHash.new(factory, human_hash, associations).attributes | |
end | |
class HumanHashToAttributeHash | |
attr_reader :associations | |
def initialize(factory, human_hash, associations) | |
translations = I18n.t("activerecord.attributes.#{factory.build_class.name.underscore}") | |
normalized_hash = {} | |
human_hash.each do |key, value| | |
translations.each do |attr_name, human_attr_name| | |
if key == human_attr_name | |
key = attr_name.to_s | |
break | |
end | |
end | |
normalized_hash[key] = value | |
end | |
@human_hash = normalized_hash | |
@associations = associations | |
end | |
def attributes(strategy = CreateAttributes) | |
@human_hash.inject({}) do |attribute_hash, (human_key, value)| | |
attributes = strategy.new(self, *process_key_value(human_key, value)) | |
attribute_hash.merge({ attributes.key => attributes.value }) | |
end | |
end | |
private | |
def process_key_value(key, value) | |
value = value.strip if value.is_a?(String) | |
[key.downcase.gsub(' ', '_').to_sym, value] | |
end | |
class AssociationManager | |
def initialize(human_hash_to_attributes_hash, key, value) | |
@human_hash_to_attributes_hash = human_hash_to_attributes_hash | |
@key = key | |
@value = value | |
end | |
def association | |
@human_hash_to_attributes_hash.associations.detect {|association| association.name == @key } | |
end | |
def association_instance | |
return unless association | |
if attributes_hash = nested_attribute_hash | |
factory.build_class.first(:conditions => attributes_hash.attributes(FindAttributes)) or | |
FactoryGirl.create(association.factory, attributes_hash.attributes) | |
end | |
end | |
private | |
def factory | |
FactoryGirl.factory_by_name(association.factory) | |
end | |
def nested_attribute_hash | |
attribute, value = @value.split(':', 2) | |
return if value.blank? | |
HumanHashToAttributeHash.new({ attribute => value }, factory.associations) | |
end | |
end | |
class AttributeStrategy | |
attr_reader :key, :value, :association_manager | |
def initialize(human_hash_to_attributes_hash, key, value) | |
@association_manager = AssociationManager.new(human_hash_to_attributes_hash, key, value) | |
@key = key | |
@value = value | |
end | |
end | |
class FindAttributes < AttributeStrategy | |
def initialize(human_hash_to_attributes_hash, key, value) | |
super | |
if association_manager.association | |
@key = "#{@key}_id" | |
@value = association_manager.association_instance.try(:id) | |
end | |
end | |
end | |
class CreateAttributes < AttributeStrategy | |
def initialize(human_hash_to_attributes_hash, key, value) | |
super | |
if association_manager.association | |
@value = association_manager.association_instance | |
end | |
end | |
end | |
end | |
end | |
World(FactoryGirlStepHelpers) | |
# The instance variables solutions were copied from: | |
# https://gist.github.com/485974 | |
Dado /^que (?:a|o)s? (.+?) existem?:$/i do |human_name, table| | |
list = [] | |
factory = factory_to(human_name) | |
table.hashes.each do |human_hash| | |
attributes = convert_human_hash_to_attribute_hash(factory, human_hash, factory.associations) | |
list << FactoryGirl.create(factory.name, attributes) | |
end | |
# Create a instance var with the factory name with a list of all users created | |
instance_variable_set "@#{factory.name.to_s.pluralize}", list | |
end | |
Dado /^que (?:a|o) (.+?) existe$/i do |human_name| | |
factory = factory_to(human_name) | |
# Create a instance var with the factory name with the users created | |
instance_variable_set "@#{factory.name.to_s}", FactoryGirl.create(factory.name) | |
end | |
Dado /^que existem? (\d+) (.+?)$/i do |count, human_name| | |
factory = factory_to(human_name) | |
instance_variable_set "@#{factory.name.to_s.pluralize}", FactoryGirl.create_list(factory.name, count.to_i) | |
end | |
# Dado /^que existem um (.+?) com (a|o) (.+?) "([^\"]*)"$/i do |human_name, human_column_name, value| | |
# factory_name = factory_to(human_name) | |
# FactoryGirl.create(factory_name, name => value) | |
# end | |
# Dado /^que existem (\d+) (.+?) com (a|o) (a|o) "([^\"]*)"$/i do |count, human_name, human_column_name, value| | |
# factory_name = factory_to(human_name) | |
# FactoryGirl.create_list(factory.name, count.to_i, name => value) | |
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
Dir[Rails.root.join('spec/support/factories/**/*.rb')].each { |r| require r } | |
module FactoryHelpers | |
# Maps a name to a path. Used by the | |
# | |
# When /^I go to (.+)$/ do |page_name| | |
# | |
# step definition in web_steps.rb | |
# | |
def factory_name_to(model_name) | |
case model_name | |
when /administrador(es)?/ | |
:admin | |
else | |
# Tries to find the model based on the translations | |
I18n.t('activerecord.models').each do |key, value| | |
return key if model_name == value.downcase or model_name == value.downcase.pluralize | |
end | |
# If can't find throw an error | |
raise "Can't find mapping from \"#{model_name}\" to a factory.\n" + | |
"Now, go and add a mapping in #{__FILE__}" | |
end | |
end | |
def factory_to(model_name) | |
factory_name = factory_name_to(model_name) | |
factory = FactoryGirl.factories.select { |factory| factory.name == factory_name } | |
raise "Can't find factory with name \"#{factory_name}\".\n" if factory.empty? | |
factory.first | |
end | |
end | |
World(FactoryHelpers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NavigationHelpers | |
# Maps a name to a path. Used by the | |
# | |
# When /^I go to (.+)$/ do |page_name| | |
# | |
# step definition in web_steps.rb | |
# | |
def path_to(page_name) | |
case page_name | |
when /the home\s?page/ | |
'/' | |
else | |
raise "Can't find mapping from \"#{page_name}\" to a path.\n" + | |
"Now, go and add a mapping in #{__FILE__}" | |
end | |
end | |
end | |
World(NavigationHelpers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
require 'uri' | |
require 'cgi' | |
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths")) | |
module WithinHelpers | |
def with_scope(locator) | |
locator ? within(locator) { yield } : yield | |
end | |
end | |
World(WithinHelpers) | |
Dado /^(?:|que )(?:|eu )estou (?:na|no|em)(?: página de)? (.+)$/ do |page_name| | |
visit path_to(page_name) | |
end | |
Quando /^(?:|eu )vou para (?:a|o|a página de) (.+)$/ do |page_name| | |
visit path_to(page_name) | |
end | |
Quando /^(?:|eu )acesso (?:a|o|a página de) (.+)$/ do |page_name| | |
visit path_to(page_name) | |
end | |
Quando /^(?:|eu )clico em "([^\"]*)"(?: em "([^\"]*)")?$/ do |button, selector| | |
with_scope(selector) do | |
click_button(button) | |
end | |
end | |
Quando /^(?:|eu )sigo o link "([^\"]*)"(?: em "([^\"]*)")?$/ do |link, selector| | |
with_scope(selector) do | |
click_link(link) | |
end | |
end | |
Quando /^(?:|eu )preencho "([^\"]*)" com "([^\"]*)"(?: em "([^\"]*)")?$/ do |field, value, selector| | |
with_scope(selector) do | |
fill_in(field, :with => value) | |
end | |
end | |
# Use this to fill in an entire form with data from a table. Example: | |
# | |
# When I fill in the following: | |
# | Account Number | 5002 | | |
# | Expiry date | 2009-11-01 | | |
# | Note | Nice guy | | |
# | Wants Email? | | | |
# | |
# TODO: Add support for checkbox, select og option | |
# based on naming conventions. | |
# | |
Quando /^(?:|eu )preencho os campos(?: em "([^\"]*)")?:$/ do |selector, fields| | |
with_scope(selector) do | |
fields.rows_hash.each do |name, value| | |
When %{I fill in "#{name}" with "#{value}"} | |
end | |
end | |
end | |
Quando /^(?:|eu )seleciono "([^\"]*)" de "([^\"]*)"(?: em "([^\"]*)")?$/ do |value, field, selector| | |
with_scope(selector) do | |
select(value, :from => field) | |
end | |
end | |
Quando /^(?:|eu )marco "([^\"]*)"(?: em "([^\"]*)")?$/ do |field, selector| | |
with_scope(selector) do | |
check(field) | |
end | |
end | |
Quando /^(?:|eu )desmarco "([^\"]*)"(?: em "([^\"]*)")?$/ do |field, selector| | |
with_scope(selector) do | |
uncheck(field) | |
end | |
end | |
Quando /^(?:|eu )escolho "([^\"]*)"(?: em "([^\"]*)")?$/ do |field, selector| | |
with_scope(selector) do | |
choose(field) | |
end | |
end | |
Quando /^(?:|eu )anexo o arquivo "([^\"]*)" em "([^\"]*)"(?: em "([^\"]*)")?$/ do |path, field, selector| | |
with_scope(selector) do | |
attach_file(field, Rails.root.join('spec/support/fixtures', path)) | |
end | |
end | |
Então /^(?:|eu )deveria ver o JSON:$/ do |expected_json| | |
require 'json' | |
expected = JSON.pretty_generate(JSON.parse(expected_json)) | |
actual = JSON.pretty_generate(JSON.parse(response.body)) | |
expected.should == actual | |
end | |
Então /^(?:|eu )deveria ver "([^\"]*)"(?: em "([^\"]*)")?$/ do |text, selector| | |
with_scope(selector) do | |
if page.respond_to? :should | |
page.should have_content(text) | |
else | |
assert page.has_content?(text) | |
end | |
end | |
end | |
Então /^(?:|eu )deveria ver \/([^\/]*)\/(?: em "([^\"]*)")?$/ do |regexp, selector| | |
regexp = Regexp.new(regexp) | |
with_scope(selector) do | |
if page.respond_to? :should | |
page.should have_xpath('//*', :text => regexp) | |
else | |
assert page.has_xpath?('//*', :text => regexp) | |
end | |
end | |
end | |
Então /^(?:|eu )não deveria ver "([^\"]*)"(?: em "([^\"]*)")?$/ do |text, selector| | |
with_scope(selector) do | |
if page.respond_to? :should | |
page.should have_no_content(text) | |
else | |
assert page.has_no_content?(text) | |
end | |
end | |
end | |
Então /^(?:|eu )não deveria ver \/([^\/]*)\/(?: em "([^\"]*)")?$/ do |regexp, selector| | |
regexp = Regexp.new(regexp) | |
with_scope(selector) do | |
if page.respond_to? :should | |
page.should have_no_xpath('//*', :text => regexp) | |
else | |
assert page.has_no_xpath?('//*', :text => regexp) | |
end | |
end | |
end | |
Então /^o campo "([^\"]*)"(?: em "([^\"]*)")? deveria ser "([^\"]*)"$/ do |field, selector, value| | |
with_scope(selector) do | |
field = find_field(field) | |
field_value = (field.tag_name == 'textarea') ? field.text : field.value | |
if field_value.respond_to? :should | |
field_value.should =~ /#{value}/ | |
else | |
assert_match(/#{value}/, field_value) | |
end | |
end | |
end | |
Então /^o campo "([^\"]*)"(?: em "([^\"]*)")? não deveria ser "([^\"]*)"$/ do |field, selector, value| | |
with_scope(selector) do | |
field = find_field(field) | |
field_value = (field.tag_name == 'textarea') ? field.text : field.value | |
if field_value.respond_to? :should_not | |
field_value.should_not =~ /#{value}/ | |
else | |
assert_no_match(/#{value}/, field_value) | |
end | |
end | |
end | |
Então /^a caixa de seleção "([^\"]*)"(?: em "([^\"]*)")? deveria estar marcada$/ do |label, selector| | |
with_scope(selector) do | |
field_checked = find_field(label)['checked'] | |
if field_checked.respond_to? :should | |
field_checked.should == 'checked' | |
else | |
assert_equal 'checked', field_checked | |
end | |
end | |
end | |
Então /^a caixa de seleção "([^\"]*)"(?: em "([^\"]*)")? não deveria estar marcada$/ do |label, selector| | |
with_scope(selector) do | |
field_checked = find_field(label)['checked'] | |
if field_checked.respond_to? :should_not | |
field_checked.should_not == 'checked' | |
else | |
assert_not_equal 'checked', field_checked | |
end | |
end | |
end | |
Então /^(?:|eu )deveria estar (?:na|no|em)(?: página de)? (.+)$/ do |page_name| | |
current_path = URI.parse(current_url).path | |
if current_path.respond_to? :should | |
current_path.should == path_to(page_name) | |
else | |
assert_equal path_to(page_name), current_path | |
end | |
end | |
Então /^(?:|eu )deveria ter os parâmetros :$/ do |expected_pairs| | |
query = URI.parse(current_url).query | |
actual_params = query ? CGI.parse(query) : {} | |
expected_params = {} | |
expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')} | |
if actual_params.respond_to? :should | |
actual_params.should == expected_params | |
else | |
assert_equal expected_params, actual_params | |
end | |
end | |
Então /^me mostre a página$/ do | |
save_and_open_page | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment