Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 721
Dipak Panchal Avatar asked Nov 06 '12 10:11

Dipak Panchal


2 Answers

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
like image 111
skalee Avatar answered Sep 28 '22 02:09

skalee


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
like image 31
jonathanccalixto Avatar answered Sep 28 '22 01:09

jonathanccalixto