Skip to content

Instantly share code, notes, and snippets.

View liangzan's full-sized avatar

Wong Liang Zan liangzan

View GitHub Profile
Before('@upload') do
$upload_path = File.join(RAILS_ROOT, 'features', 'upload_files')
end
After('@upload') do
Dir.foreach($upload_path) do |file|
file_path = File.join($upload_path, file)
if file == '.' or file == '..' then next
else File.delete(file_path)
end
#Cucumber Steps for file uploads
When /^I focus on "([^\"]*)" field to attach "([^\"]*)" file$/ do |field_name, file_name|
attach_file(field_name, file_name)
end
Given /^I have "([^\"]*)" csv file containing:$/ do |file_name, csv_values|
create_csv_file(file_name, csv_values)
end
#Celerity Helper
Given I have "invitee_list.csv" csv file containing:
"""
mimi@famil.com, momo@xmail.com, lulu@mmail.com
"""
When I focus on "invitee_list" field to attach "invitee_list.csv" file
When /^I follow the PDF link "([^\"]+)"$/ do |link|
File.open($pdf_file_path, "w") do |file|
file.puts find_link(link).download.readlines
end
end
#note that the pdf reader removes all white spaces
Then /^I should see "([^\"]+)" on page "(\d+)"$/ do |text, page|
(pdf_reader.content[page.to_i - 1] =~/#{text}/).present?.should == true
end
Before('@pdf') do
$pdf_file_path = File.join(RAILS_ROOT, 'tmp', "temp.pdf")
end
After('@pdf') do
File.delete($pdf_file_path) if File.exist?($pdf_file_path)
end
#http://github.com/yob/pdf-reader/blob/master/examples/rspec.rb
class PageTextReceiver
When I follow the PDF link "Foo"
Then the pdf document should have "2" pages
And I should see "Chunky" on page "1"
And I should see "Bacon" on page "2"
#config/environment.rb
config.gem 'facets', :version => '2.8.0', :lib => false
#config/initializers/facets.rb
require 'facets/enumerable'
def find_button(text)
button = $browser.element_by_xpath(%\//input[@value="#{text}"]\)
raise "button #{text} not found" unless button
button
end
Then /^I should not see button "([^\"]+)"$/ do |text|
button = find_button(text) rescue nil
button.should be_nil
end
klass = String
metaclass = class << klass; self; end
baz = "baz"
metaclass.class_eval(%\def foo; 'foo'; end\)
# defines class methods
# klass.foo = 'foo'
# String.foo = 'foo'
metaclass.instance_eval(%\def foo; 'foo'; end\)
@object.class.reflections.collect do |name, _|
if by_associations.present?
name if by_associations.include?(_.macro)
else
name
end
end.compact