Dipak Panchal
Dipak Panchal

Reputation: 6036

Rspec testing for iframe content

How can i write rspec testing for Iframe content? my Iframe look like

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

in between iframe whatever content, I want to write rspec test case for that content. How can i do ?

how can i check "test" is in iframe using rspec ? I write below but not pass

page.should have_content("test")

error like

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

I use capybara - 1.1.2 and rspec - 2.11.0 and rails 3.2.8

Upvotes: 7

Views: 5358

Answers (3)

Antony Fuentes
Antony Fuentes

Reputation: 1103

Answers from above are great, though I still wanted to extend them with the following:

If you just want to assert that certain text is present in a page and would like to automatically check inside any present iframes, then I did this generic code that would accomplish that:

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

Notice that it keeps checking the page until the given amount of seconds is met by the started timer or until the given text is found inside the page or an iframe. I believe the code is pretty clear and easy to understand, though let me know if you have any questions.

Upvotes: 2

jonathanccalixto
jonathanccalixto

Reputation: 3208

In my case, I needed to inspect an iframe that did not have name nor id. eg

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

so I could not find the iframe in any way, until I found this example in capybara entrails. Capybara source code

With that I got this solution:

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

Upvotes: 3

skalee
skalee

Reputation: 12665

Following one works with selenium driver, possibly with other drivers too, but not for 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>

spec:

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

Upvotes: 7

Related Questions