Skip to content
This repository

require ‘gizmo’

# spec/spec_helper.rb

require 'gizmo'
Spec::Runner.configure do |config|
    include Gizmo::Helpers
end

Gizmo.configure do |config|
    # this is just so we can use the same page mixins as the cucumber tests
    config.mixin_dir = File.join(File.dirname(__FILE__), '../features/support/pages/')
end

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

Specs

# spec/homepage_spec.rb

describe "Github" do

  describe "Home Page Search" do

    before(:all) { visit 'http://github.com' }

    it "should have a text input which accepts a search query" do
      on_page_with :github_search do |page|
        page.perform :search, 'gizmo'
      end
    end

    it "should perform a search when clicking the magnifying glass" do
      on_page_with :github_search do |page|
        click page.search_form.submit.attr('alt').value
      end
    end

    it "should redirect to the search results page with gizmo as the first result" do
      on_page_with :github_search_results do |page|
        page.search_results.repositories.results.first.name.should == 'gizmo'
      end
    end

  end

end
Something went wrong with that request. Please try again.