Sha256: 1dcc164164a4309e55f2d9adccc5d01774ca0cec9f1212cd5c62cbddf3838b49

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

#---
# Excerpted from "Scripted GUI Testing With Ruby",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/idgtr for more book information.
#---

require 'rubygems'
require 'watir'

class BookSearch
  def initialize
    @browser = Watir::IE.new
  end
  
  def close
    @browser.close
  end
end




class BookSearch
  def find(term)
    @browser.goto 'http://www.pragprog.com'
    @browser.text_field(:id, 'q').set('Ruby') #(1)
    @browser.button(:class, 'go').click       #(2)
    
    bookshelf = @browser.table(:id, 'bookshelf')
    num_results = bookshelf.row_count

    (1..num_results).inject({}) do |results, i|
      book = bookshelf[i][2] #(3)

      full_title = book.h4(:index, 1).text
      byline     = book.p(:class, 'by-line').text
      url        = book.link(:index, 1).href

      title, subtitle = full_title.split ': '
      authors = authors_from byline
      
      results.merge title => {
        :title => title,
        :subtitle => subtitle,
        :url => url,
        :authors => authors }
    end
  end
end



class BookSearch
  def authors_from(byline)
    byline[3..-1].gsub(/(,? and )|(,? with )/, ',').split(',')
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
win_gui-0.1.6 book_code/tubes/book_watir.rb
win_gui-0.1.4 book_code/tubes/book_watir.rb
win_gui-0.1.3 book_code/tubes/book_watir.rb
win_gui-0.1.2 book_code/tubes/book_watir.rb
win_gui-0.1.1 book_code/tubes/book_watir.rb
win_gui-0.1.0 book_code/tubes/book_watir.rb