Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Mechanize: multiline value for textarea gets merged

Tags:

ruby

mechanize

EDIT: upon further testing, it seems like the issue is site-specific and should theoretically work just fine.


Textarea values which should be on multiple lines are being submitted all on one line.

textarea_values = "value1\nvalue2"

form = page.form_with(:id => 'form_id_here')
form['my_textarea'] = textarea_values
submit = form.button_with(:value => 'Submit')
form.click_button(submit)

The value being submitted is value1\nvalue2 instead of being on multiple lines as intended.

Is there another way to add form values that I can try?

like image 655
Marco Avatar asked Jun 04 '11 21:06

Marco


1 Answers

Have you really tried the code you posted here?

Well I have in a website that has internal messaging system, with two textfields and one textarea.

my initial sample code:

@page = @agent.get "thewebsiteItold"

form = @page.form_with(:id => 'form')

textarea_values = "value1\nvalue2"

form['to'] = "username"
form['subject'] = "somesubject"
form['text'] = textarea_values

button = form.button_with(:name => "send")

@agent.click_button button

Although the form had this html:

<form method="post" action="/somethingyoudontneedtoknow" name="header" id="form">

running my script returned this error:

in 'method_missing': undefined method `id' for #<Mechanize::Form:0x21efb98> (NoMethodError)

Ok, so I changed the id for name and got another problem: the

in `<main>': undefined method `click_button' for #<Mechanize:0x253f380> (NoMethodError)

Before you tell me, I double checked the contents of "button", which holded the correct button I wanted to press to fire the form action.

So I changed it accordingly to these mechanize examples and the code worked well:

form = @page.form_with(:name => 'header')

textarea_values = "value1\nvalue2"

form['to'] = "username"
form['subject'] = "anotherboringsubject"
form['text'] = textarea_values

button = form.button_with(:name => "send")

@agent.submit(form, button)

and, well, it worked like expected:

resulting message on chrome

Conclusion: Check if your original code is too far from what you posted us or the version of mechanize is the same as the link I passed before

like image 185
bpaulon Avatar answered Oct 31 '22 17:10

bpaulon