LoginSignup
6
6

More than 5 years have passed since last update.

cucumberとかrspecとか使わずWebrat単体でWebページを巡る

Last updated at Posted at 2013-11-19

Webratをrequireし、Webrat::Methodsをincludeすることで、いつものように快適にウェッブサーフィンを行うことができます。

require 'pry'
require 'webrat'
require 'mechanize'

module StageSurfer
  class Core

    Webrat.configure do |config|
      config.mode = :mechanize
    end

    include Webrat::Methods

    def initialize login_page_url, user_id, password
      visit  login_page_url
      #下記はログインページの作りによって変える必要がある!
      fill_in "id", :with => user_id
      fill_in "password", :with => password
      click_button "submit"
    end

    def surf target_urls
      target_urls.each {|url| yield(url, visit(url))}
    end

  end
end

urls = STDIN.each_line.map {|line| line.chomp }
proc = eval("lambda {|url, page| #{ARGV[3]}}")
StageSurfer::Core.new(ARGV[0], ARGV[1], ARGV[2]).surf(urls, &proc)

上記のクラスを使って、http://your_site/contents_pageにContent-typeという文言が存在する場合は"o"を、なければ"x"を出すという場合は、下記のように呼び出します。

echo http://your_site/contents_page | ruby stage_surf.rb http://your_site/login_page id password 'puts "#{url}\t#{/Content-type/ =~ page.content ? :o : :x}"'
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6