4

I currently have to do a job where I have to copy the code of a website into a textfield.

I'm using watir to do the browser handling. As far as I know, I can only fill the field using the set function, which means that I have to do something like

browser.text_field(:id => "text").set sitetext

With sitetext being the code of the website that I'm copying into it. I've loaded the code from a file into an array before and then pushed it into the string (probably not the best choice, but easiest for me right now), using the following code.

contentArray=Array.new
inputFile=File.open("my-site.html")
inputFile.each{|line| contentArray<<line}
inputFile.close

Now when I execute the first command to fill in the text_field, it slowly types in all the letters (is there an easy way to speed this up?), but after 692 characters it stops in the middle of the sentence. [I pasted the text that was entered into charcounter.com, that's how I know this number.]

Where is the problem? Is ruby giving my strings a limited size for some reason? Can I somehow lift this barrier?

Is there another way to fill the text_field?

sawa
  • 165,429
  • 45
  • 277
  • 381
Sebastian
  • 63
  • 2

1 Answers1

2

Try the .value method

browser.text_field(:id => "text").value=(open('my-site.html') { |f| f.read })

OR

I'm thinking the misprinting of umlauts etc is something to do with the codepage settings on your machine and the file you're reading from. You might have to experiment going from one code page to another ... I'm guessing your source file is CP850 or perhaps even UTF-8 and I think you need western european to get umlauts... but being Australian I really have no idea =) http://en.wikipedia.org/wiki/ISO_8859

e.g.

require 'iconv'

browser.text_field(:id => "text").value=(
  Iconv.iconv('CP850', 'ISO-8859-1', open('my-site.html') { |f| f.read })
)
Tim Koopmans
  • 788
  • 4
  • 9
  • Wonderful! Thank you :-) Now the only problem is, that certain special characters are represented in a wrong way. In the HTML-File they are correct, but when pasting them to the text_field, they get turned in other characters somehow :( Do you have any idea how to solve this? Thanks again! You were a great help :) – Sebastian May 07 '11 at 07:38
  • `require "watir"` `browser=Watir::Browser.new` `browser.goto("http://localhost/joomla15/administrator/index.php")` `browser.text_field(:name => "username").set "admin"` `browser.text_field(:name => "passwd").set "pass"` `browser.button(:value =>"Anmelden").click` `browser.link(:text =>"Neuer Beitrag").click` `browser.maximize` `browser.text_field(:name => "title").set "Testbeitrag"` `browser.text_field(:name => "alias").set "testbtr"` `browser.link(:title =>"Editor an/aus").click` `browser.text_field(:id => "text").value=(open('website.html') { |f| f.read })` – Sebastian May 08 '11 at 10:13
  • Sorry for the formating, somehow it won't do new lines, although I added two spaces at the end of every line :( And thanks for trying to help me again! – Sebastian May 08 '11 at 10:16
  • I mean what other characters are getting displayed? can you give some examples of what you're expecting to be output and what is actually displayed? e.g. Hello Günter => Hello+G%FCnter – Tim Koopmans May 08 '11 at 22:00
  • Ah okay. Here are a few examples: über => ³ber vergißt => vergi▀t wählen => wõhlen geförderter => gef÷rderter Hope that helps :) – Sebastian May 09 '11 at 07:42
  • What code page is your OS on? Send back the output of this from dos: chcp – Tim Koopmans May 09 '11 at 09:38
  • Whatever the first part meant, the output is: 850. – Sebastian May 09 '11 at 10:10
  • Now all the newlines are shown as \n and the special signs are shown differently, for example: \x81ber = über vergi\xE1t = vergißt – Sebastian May 10 '11 at 15:57
  • I'd suggest, starting a new question, we are now a bit past the original one of setting the text field, and off into the land of code pages, unicode, etc better to use a new question where you can format stuff much much much better than in comments which are very restricted in their capabilities. – Chuck van der Linden May 11 '11 at 18:41
  • Okay, thanks for the suggestion. I opened a new question with hopefully all the info one could need at the following adress: http://stackoverflow.com/questions/5974692/ruby-encoding-problem-when-filling-out-a-text-field-with-watir – Sebastian May 12 '11 at 07:32