query: tag:webrat

WWW::Mechanize::Page#images

WWW::Mechanize::Page は、forms, links メソッドでそれぞれの要素の配列が取得できるが、画像(img)を取得するメソッドはまだないので簡易実装。

ruby>>
require 'mechanize'

WWW::Mechanize::Page.class_eval do
def images
@images ||= search('img').map do |img|
case src = img['src']
when %r{^https?://}
src
else
(uri + src).to_s
end
end.uniq
end
end
<<--

forms, links では Page::XXX オブジェクトが返るので、
同様に Page::Image オブジェクトを作成するのが正しい姿であるが、
今はページの画像のURL一覧が欲しいだけなので、割愛。
そこは誰かに任せた。

ruby>>
agent = WWW::Mechanize.new
page = agent.get('http://blog.s21g.com/')
page.forms.size # => 1
page.links.size # => 109
page.images.size # => 17
<<--

とりあえず快適!

posted by maiha maiha on Fri 28 Aug 2009 at 09:32 with 0 comments

Cucumberのfeatureを実行すると、上記のエラーが出て困ったので探したら

I resolved it by uninstalling the old "bryanary-webrat" gem and making sure that "webrat" was up to date (0.4.3)

When you have both installed, ruby tends to load the old one instead.
http://groups.google.com/group/webrat/browse_thread/thread/d8ea685a1b1931a5#

古いwebrat gemを消しなさいと。

shell>>
gem search webrat

*** LOCAL GEMS ***

aslakhellesoy-webrat (0.3.2.2)
webrat (0.4.4, 0.4.3, 0.3.4)
<<--

案の定古いwebratがあったのでaslakhellesoy-webratをuninstallしたら動きました!

posted by satoko satoko on Tue 14 Apr 2009 at 15:15 with 0 comments

[追記 2009.01.20]script/generate featureの項を追加

moroさんの記事を読んで、Cucumber wktk!と思っていたのでtry

http://d.hatena.ne.jp/moro/20081112/1226486135
http://d.hatena.ne.jp/moro/20081118/1226977015
http://gist.github.com/26024

こちらの導入手順を参考しました:
http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails

###必要なgemをinstall

  • rspec
  • rspec-rails
  • cucumber
  • webrat

ローカルには0.1.15を入れたのですが、念のため0.1.13で依存しているというgem:aslakhellesoy-webratもinstallしました。

shell>>
gem sources -a http://gems.github.com
sudo gem install aslakhellesoy-webrat
<<--

###Webratを使うようにCucumberのenvに書く
${RAILS_ROOT}/features/support/env.rb

shell>>
require "webrat"
Webrat.configure do |config|
config.mode = :rails
end
<<--

###script/generate feature
moroさんところのcucumberの構造辺りで紹介されているgeneratorでfeatureやstepsを生成:

shell>>
$ ruby script/generate feature Product
<<--

###Webratの仕様が変わってた
moroさんのgistを下記に保存して使用し始めたのですが、
shell>>
${RAILS_ROOT}/features/step_definitions/webrat_ja_steps.rb
<<--

細かい仕様が変更されていました:
ディレクトリstep_definitionsの位置も変わっているのに加えて、visits => visit、fills_in => fill_inなどが変更されていました。ただ、前もって調べなくても、rake featuresすると仕様変更された旨のmessageが出るので安心です。

rails>>
visit home_path
fill_in "Email", :with => "good@example.com"
<<--

またpendingされたstepには、下記のようにsnippetsが出力されて便利だなぁと思いました:

shell>>
You can use these snippets to implement pending steps which have no step definition:
When /^パラメータを入力する product_comment[body]=dummy comment!$/ do
end
<<--

###Refs
http://moriq.tdiary.net/20081022.html
http://d.hatena.ne.jp/hs9587/20081231/1230691812
http://barkingiguana.com/2008/11/11/getting-started-with-story-driven-development-for-rails-with-cucumber

posted by satoko satoko on Mon 19 Jan 2009 at 15:44 with 0 comments