Cucumber

icaruswings edited this page Sep 14, 2010 · 17 revisions

require ‘gizmo’

# features/support/env.rb

require 'gizmo'
World(Gizmo::Helpers)

Features

Scenario: A user can perform a search from the homepage
    Given a user is on the github homepage
    When the user enters "gizmo" into the search box and clicks the magnifying glass icon
    And the user clicks on the "icaruswings / gizmo" link
    Then the user is on the "gizmo" github repository details page
    And the user is on a github repository details page which belongs to "icaruswings"

Page Mixins

# features/support/pages/page_with_github_search.rb

module PageWithGithubSearch

  include Gizmo::PageMixin

  def valid?
    has_selector?("div.search")
  end

  def search_form
    element_struct do |form|
      form.container = @document.css("div.search")
      form.element = container= form.container.css("form")
      form.input = container.css("input[name=q]")
      form.submit = container.css("input[alt=search]")
    end
  end

  define_action :search do |query|
    fill_in search_form.input.attr('name').value, :with => query
    click search_form.submit.attr('alt').value
  end

end
# features/support/pages/page_with_github_search_results.rb

module PageWithGithubSearchResults

  include Gizmo::PageMixin

  def valid?
    has_selector?('div#code_search_results')
  end

  def search_results
    element_struct do |results|
      results.repositories = build_repositories
    end
  end


  private

  def build_repositories
    element_struct do |repos|
      container = find_results_container "Repositories"
      repos.heading = container.css('div.header')
      repos.results = container.css('div.result').map { |result| build_repository_result result }
    end
  end

  def build_repository_result result
    element_struct do |repo_result|
      repo_result.title = result.css('h2.title')
      repo_result.link = repo_result.title.css('a')
      link_parts = repo_result.link.inner_text.split('/').map(&:strip)
      repo_result.author = link_parts[0]
      repo_result.name = link_parts[1]
    end
  end

  def find_results_container heading_text
    all_headings = @document.css('div.header')
    header = all_headings.find do |heading_element|
      heading_element.css('div.title').inner_text =~ /#{heading_text} \((.+)\)/
    end
    header.parent unless header.nil?
  end

end

Steps

# features/step_definitions/homepage_steps.rb

Given /^a user is on the github homepage$/ do
  visit "http://github.com"
end

When /^the user enters "([^\"]*)" into the search box and clicks the magnifying glass icon$/ do |query|
  on_page_with :github_search do |page|
    page.perform :search, query
  end
end

When /^the user clicks on the "([^\"]*)" link$/ do |text|
  click_link text
end

Then /^the user is on the "([^\"]*)" github repository details page$/ do |repo_name|
  on_page_with :github_repo_details do |page|
    page.repo_details.name.should == repo_name
  end
end

Then /^the user is on a github repository details page which belongs to "([^\"]*)"$/ do |author|
  on_page_with :github_repo_details do |page|
    page.repo_details.author.should == author
  end
end
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.