1
votes

I am really stuck now. I have an iframe in which there is a < p> tag where I want to send some text, but I am just not able to do it.

HTML:

<iframe id="edit-field-verdict-0-value_ifr" frameborder="0" src="javascript:""" style="width: 100%; height: 100px;">
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head xmlns="http://www.w3.org/1999/xhtml">
    <body id="tinymce" class="mceContentBody " spellcheck="false" dir="ltr">
      <p>
        <br mce_bogus="1">
      </p>
    </body>
    </html>
</iframe>

The code that i have tried is :

@browser.elements(:xpath => '//*[@id="tinymce"]').p.send_keys [:control, 'a']

The error that I am getting is:

undefined method `elements' for #<String:0x24ba570> (NoMethodError)

I also tried

$browser.frame(:id,'edit-field-verdict-0-value_ifr').html.body(:id,'tinymce').p.send_keys [:control, 'a']

But as the body is not recognized by Watir, I tried elements_by_xpath as well. It didn't work.

How can I make this thing work?

1

1 Answers

4
votes

For the first attempt, the error message is saying that @browser is a string rather than a Watir::Browser object. You should verify that @browser is correctly set. Based on your second example, perhaps it is meant to be the global variable $browser.

For the second attempt, body is supported in Watir. However, html will return the page's html rather than the html element. Given that there should only be one body element, the html element can be omitted.

$browser.frame(:id,'edit-field-verdict-0-value_ifr').body(:id,'tinymce').p.send_keys [:control, 'a']

But also keep in mind that you only need to include the frame method (to tell watir to look inside the frame) and as little as needed to reliably find the element you are interacting with. Anything extra is just making the code more verbose, and also perhaps making things more brittle and easy to break. So the above could be shortened down to just

$browser.frame(:id,'edit-field-verdict-0-value_ifr').p.send_keys [:control, 'a']

Based on the id of the element you are testing, I assume it is a WYSIMYG Editor. You should look at the Watir-Webdriver page for an example - http://watirwebdriver.com/wysiwyg-editors/. The TinyMCE Editor example from the webpage:

require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'http://tinymce.moxiecode.com/tryit/full.php'
b.execute_script("tinyMCE.get('content').execCommand('mceSetContent',false, 'hello world' );")
b.frame(:id => "content_ifr").send_keys 'hello world again'