2

RoR アプリケーションで BDD に Cucumber と Capybara を使用しています。私は次の機能を持っています

Feature: List profiles
  In order to know that the people behind the profiles are real people 
  As a lurker
  I want to be able to find who is registed and see their profile picture

Background: Profiles have been added to the database, some with picture attached
  Given some profiles exist
  And  I am on the home page

Scenario: Search by city, avatars should be there
  When I search for a city with profiles
  Then I should see some result profiles with avatar

基礎となる Capybara ステップ ファイルには以下が含まれます。

Then /^I should see some result profiles with avatar$/ do
  page.should have_css("#profile_search_results .profile img.avatar", :count => some_profiles)
end

このステップでは、ページに含まれていることを確認します

<div id="#profile_search_results>
  <img class="avatar" src="" />

しかし...画像が存在するかどうかも確認したい(壊れた画像ではない)。

カピバラのステップでこれを行うにはどうすればよいですか?

4

3 に答える 3

3

The way to check, without having to use Selenium or any other drivers, was to extract the src attribute from the img element (Thanks MrDanA), and check the status code after visiting that URL:

page.should have_css("#profile_search_results .profile img.avatar", :count => some_profiles)
img = page.first(:css, "#profile_search_results .profile img.avatar")
visit img[:src]
page.status_code.should be 200 
于 2012-04-12T08:02:31.810 に答える
2

Capybara の finder メソッドを使用imgして問題のタグを検索し、そのsrc属性を取得できます (例: src = element[:src])。その後、Ruby を使用して存在するかどうかを確認できます。したがって、全体としては次のようになります。

element = page.first(:css, "...")
src = element[:src]
assert File.exists?("#{Rails.root}/#{src}")

に与えられたパスFile.exists?が正しいことを確認するためにこれを試していません。ただし、テストで最初に印刷すると、どのように見えるか、少しいじる必要があるかどうかがわかります。

于 2012-04-11T16:09:32.667 に答える
1

テストが Firefox で実行されていると仮定するとnaturalWidth、画像のプロパティを確認できます。0 の場合、画像が壊れています。

if find('img.avatar')[:naturalWidth] == 0
  raise('image broken')
end
于 2012-04-12T03:00:08.567 に答える