Im trying to fill in a field via its name. Because there are two fields with the same name in 2 different forms I can't just use fill_in on its own as there is an ambiguous match.
Im trying to do something like below, but obviously the within doesnt look by name, so it cannot find the form.
When(/^I type "(.*?)" into the "(.*?)" textbox in the "(.*?)" form$/) do |textboxValue, textboxName, textboxForm|
within(:name, textboxForm) do
fill_in textboxName, :with => textboxValue
end
end
texboxForm - Name of the form the element is within
textboxName - Name of the textbox
textboxValue - The value I want to fill the textbox with
Any ideas?
Thanks
Its a bit horrible, but this worked.
find("form[name='" + textboxForm + "']").fill_in textboxName, :with => textboxValue
Related
Below is my code.I'm trying to input value from the excel sheet to the text boxes in the page. Eg : In text box id is 'allocationRegContrib[17].newFundValue' I want to input a value say 20. Similarly for another text box whose id is 'allocationRegContrib[18].newFundValue', I want to input a value say 40. How to achieve. Similarly the id goes on upto 60. But I do not want to input in all the textboxes. So I'm trying to use like fill_in "allocationRegContrib[i].newFundValue".
#i=17
for j in (workbook.first_row..workbook.last_row)
for k in (workbook.first_column..workbook.last_column)
if(k==1)
fill_in "searchInput", :with => workbook.cell(j,1)
find(:xpath, '//*[#id="sdcaLink"]').click
sleep 3
else
choose("sdcaOption", :option => "YES")
select(workbook.cell(j,k), :from => 'sdcaDuration')
fill_in "allocationRegContrib[i].newFundValue", :with => workbook.cell(j,k)
#i=i+1
find(:xpath, '//*[#id="specialDCAupdate"]').click
But it does not work for me. Error is "unable to locate the capybara element allocationRegContrib[i].newFundValue". Please Advice
You may have two issues:
i and #i aren't the same thing. I think you may want to use i in all the places. i is a local variable (that is, a simple plain old variable). #i is an instance variable of a class, like a "field" or "property" in other languages.
fill_in "allocationRegContrib[i].newFundValue", :with => workbook.cell(j,k)
should probably be:
fill_in "allocationRegContrib[#{i}].newFundValue", :with => workbook.cell(j,k)
#{i} puts in the value of the variable i into the string, then the capybara matcher can find, for example, element allocationRegContrib[17].newFundValue (when i=17)
I am trying to insert a number in the 'Mine Id' using Ruby - Watir/Selenium
Somehow the field <input> field is not being recognized as a text box and hence I am unable to enter values through the code.
The id 'inputdrs' is used multiple times on the same page.
Any suggestions how to achieve it.
The URL is this
http://www.msha.gov/drs/drshome.htm
The below don't work:
#browser.text_field(:id => /inputdrs/, :index => 2).set("3607277")
browser.text_field(:name, "inputdrs").set("3607277")
Thank you for your help
require 'watir-webdriver'
$browser = Watir::Browser.start "http://www.msha.gov/drs/drshome.htm"
a = 0
b = $browser.text_fields.length
while a < b
$browser.text_fields[a].set a
a += 1
end
This will put the value of a in each text field on this page. I REALLY drew out the loop so you can see whats going on. This isn't as dynamic as you would like it, but if the page has the same amount of text_fields you should be fine.
OR you can do something like..
$browser.text_field(:name => "MineId").select
$browser.send_keys "hello"
Problem
The html of the Mine ID field is:
<input size="8" maxlength="8" name="MineId" onclick="this.value='';" id="inputdrs" align="middle" type="number">
The line:
#browser.text_field(:id => /inputdrs/, :index => 2).set("3607277")
Will fail because it is inputting into the wrong text field. If you get all of the text fields with that id:
browser.text_fields(:id => 'inputdrs').collect(&:name)
#=> ["q", "MineId", "OperSearch", "MineName", "CntctrId", "CntCtrSearch", "Controller"]
You can see it is the second field. However, because Watir is using a 0-based index, you actually get the OperSearch field. This would have worked by using an :index => 1 instead.
The line:
browser.text_field(:name, "inputdrs").set("3607277")
Will fail because the "inputdrs" is the value of the id attribute and not the name attribute.
Solution
Given that the id attribute value is not unique for this page, you should probably not use it for locating. Instead, use something unique, such as the name attribute.
browser.text_field(:name => "MineId").set("3607277")
Ok, this should be on here already somewhere, but I can't find it.
I'm building a form with simple_form, and I'm using a dummy radio select input which values are based on a method (named 'simple?') in my 'Price' model. The methods looks if some attributes are used and returns false if they are. This way I can hide the advanced inputs in my form if they are not used.
I want to use simple_form so the method is connected to the right object because more prices can be made (and created) in the same form.
I use the radio buttons (and javascript) so users can still unhide the advanced fields.
Problem is when I submit the form, the form including the :simple? method is submitted, which returns errors because it is just a method.
Question is: How do I make sure the ':simple?' value for the is not submitted through the form, but is initiated to the right value? Or is there a smarter way of doing this?
form.haml
= f.input :simple?, as: :radio, label: "Simple price?"
price.rb
def simple?
true unless advanced?
end
def advanced?
specific_days? || period_id? || description? || begin_time? || end_time?
end
Thanks for any feedback!
If you rename simple? to is_simple you can implement a virtual attribute in your model
def is_simple
true unless advanced?
end
def is_simple= _ignored
# NOOP
end
def advanced?
specific_days? || period_id? || description? || begin_time? || end_time?
end
and then use :is_simple in your form
= f.input :is_simple, as: :radio, label: "Simple price?"
The renaming is necessary because def simple?= will lead to a syntax-error.
For example the text box is having the value 10
I need to change the value to 20
while I am using the below Capybara command,
fill_in "#{ID}", :with => "20"
the value is not getting changed ... instead its appended as 1020.
Give me your suggestions.
I've had to use javascript to do this. I wrote a separate step to clear the field before changing the value.
page.execute_script("$('#{field_id}').val('');")
I believe the correct syntax is
fill_in "ID", :with => "20"
I have an array of strings, called #theModels, in a routine implemented as part of a Sinatra server. These models are options for the user to select, and are obtained by the back end (the idea being, as new models are added, then the front end code should not change).
I'm using haml to render html.
How can I enumerate each element in the list of #theModels such that each element is a checkbox? And how can I obtain which checkboxes the user has selected?
I see that just putting
= #theModels
will give me the list of strings contained in #theModels, but without spacing or the like, and certainly not in checkboxes. I've found this question that appears to be similar, but my haml-fu isn't good enough to convert that into what I need.
UPDATE:
These are options associated with a file upload, such that now the code looks like:
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
- #theModelHash.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
Problem is, that puts a file upload button on each option, instead of at the end. I only want one submit button in the end; should I have two forms that both report their results when the 'Upload' button is pressed?
UPDATE2:
After a moment's thought, the above can be modified to:
Thanks!
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
- #theModelHash.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}
And that appears to do what I want.
I think you should send the content as an hash instead.
This will give you the opportunity to set initial values in the form.
The hash #params will give you the result.
E.g. {"oranges"=>"1"}
#app.haml
%form{:method => 'post', :action => "/"}
- #models.each do |key,value|
%br
%input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
=key
%input{:type => :submit, :value => "Save"}
#app.rb
require 'sinatra'
require 'haml'
get '/' do
#models = {"oranges" => true, "bananas" => false}
haml :app
end
post '/' do
#params.inspect
end
The link you provided linked to a rails solution where you have a function returning the proper html.
You can define this function yourself:
Input: key, value
Output: %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
def check_box(key, value)
...
end
and call it in haml with
=check_box(key,value)