2012-11-06 52 views
5

Iframeコンテンツのrspecテストを作成するにはどうすればよいですか?私のインラインフレームは、私はそのコンテンツのRSpecのテストケースを書くことが好きなコンテンツのiframeの間にiframeコンテンツのRspecテスト

<iframe src="/any url" name="Original"> 
    ... 
    <div>test </div> 
</iframe> 

のように見えます。どのようにできるのか ?

rspecを使用してiframeに「test」が表示されていることを確認するにはどうすればよいですか? 1.1.2とRSpecの - - 私はカピバラ使用

page.should have_content("test") 

エラー

Failure/Error: page.should have_content("test") 
     expected there to be content "test" in "Customize .... 

のように通過し、以下のではなく、書き込み2.11.0を、もう1つはで動作する以下の3.2.8

+0

iframeが読み込まれるまで待つ必要があります。[返答](http://stackoverflow.com/questions/6890364/capybarawebkit-unable-to-find-iframe-or-its-content)によると、 'have_content'をテストする前に仕様で数秒間眠っているとどうなりますか? –

+1

@shioyama - 'should have_content'はすでに待ちます(デフォルトは2秒です)。 – skalee

+0

@skaleeああ、それを知らなかった。面白い。 –

答えて

5

レールセレニウムドライバー、おそらく他のドライバーとの接続も可能ですが、rack_testでは使用できません。

/index.htmlが:

<!DOCTYPE html> 
<html> 
    <body> 
    <h1>Header</h1> 
    <iframe src="/iframer" id='ident' name='pretty_name'></iframe> 
    </body> 
</html> 

/iframer.html:

<!DOCTYPE html> 
<html> 
    <body> 
    <h3>Inner Header</h3> 
    </body> 
</html> 

が仕様:私の場合は

visit "/" 
page.should have_selector 'h1' 
page.should have_selector 'iframe' 

page.within_frame 'ident' do 
    page.should have_selector 'h3' 
    page.should have_no_selector 'h1' 
end 

page.within_frame 'pretty_name' do 
    page.should have_selector 'h3' 
    page.should have_no_selector 'h1' 
end 
1

、私はしませんでしたIFRAMEを検査するために必要な名前もIDもない。例えば

HTML

<iframe class="body" src='messages/1.html'>...</iframe> 

RSpecの

expect(page).to have_content "From [email protected]" 

within_frame '.body' do 
    expect(page).have_content 'You can confirm your account email through the link below:' 
end 

私はカピバラの内臓で、この例を見つけるまで私は、どのような方法ではiframeを見つけることができませんでした。それとCapybara source code

私は、このソリューションを得た:上記から

expect(page).to have_content "From [email protected]" 

within_frame find('iframe.body') do 
    expect(page).have_content 'You can confirm your account email through the link below:' 
end 
0

回答は、私はまだ、以下でそれらを拡張したかったものの、素晴らしいです:

あなただけでは、特定のを主張したい場合テキストがページ内にあり、現在のiframeを自動的にチェックしたい場合は、次のような一般的なコードを実行します。

Then (/^I should see the text "(.*?)" in less than "(.*?)" seconds$/) do |text,time| 
    result = false 
    Capybara.default_wait_time = 5 
    time_start = Time.now 
    begin 
     time_running = Time.now - time_start #Calculate how much time have been checking 
     result = page.has_text?(text) 
     unless result #If not found in normal DOM, look inside iframes 
      if page.has_css?("iframe") #Check inside iframes if present 
       (page.all(:css,"iframe")).each do |element| #If multiple iframes found, check on each of them 
        within_frame(element) do 
         result = page.has_text?(text) 
         break if result #Stop searching if text is found 
        end 
       end 
      end 
     end 
    end until (time_running.to_i >= time.to_i) || (result == true) 
    expect(result).to be true 
end 

開始タイマーが所定の秒数に達するまで、または指定されたテキストがページまたはiframe内に見つかるまで、ページのチェックを続けます。 私はコードがかなり明確で理解しやすいと思っていますが、ご質問がある場合はお知らせください。