How to change the values in textbox using capybara? - ruby

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"

Related

trying to increment a value of the object by assigning to a variable i . Please advice

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)

Insert value in a text field using Ruby - watir/selenium

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")

Capybara - Fill in form element within form using arguments

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

How to find an element by matching exact text of the element in Capybara

I have following two elements in HTML
<a href="/berlin" >Berlin</a>
<a href="/berlin" >Berlin Germany </a>
I am trying to find the element by using following Capybara method
find("a", :text => "berlin")
Above will return two elements because both contains text berlin.
Is there a way to match exact text in Capybara ?
Use a regexp instead of a string for the value of the :text key:
find("a", :text => /\ABerlin\z/)
Check out the 'Options Hash' section of the Method: Capybara::Node::Finders#all documentation.
PS: text matches are case sensitive. Your example code actually raises an error:
find("a", :text => "berlin")
# => Capybara::ElementNotFound:
# Unable to find css "a" with text "berlin"
Depending on which version of the gem you are using
find('a', text: 'Berlin', exact: true)
may be deprecated. In which case you would have to use
find('a', text: 'Berlin', match: :prefer_exact)
You can do so too:
find('a', text: 'Berlin', exact_text: true)
That will find for CSS.
And using only exact: true instead of exact_text will show you a msg that exact option is only valid for XPATH.
Just use Capybara's exact option:
Capybara.exact = true
My preference is to use the have_selector with text and exact_text: true:
expect(body).to have_selector 'a', text: 'Berlin', exact_text: true
For using click_link in capybara you need to add one more property in the method using it.
click_link(link_name, :text => link_name)
Here the link_name is the text value of a link.
Using :text keyword we are specifying that we want to click on a link having the text value which is exact matching to our requirement.

Capybara: Select an option by value not text

For the HTML
<select id="date">
<option value="20120904">Tue 4 Sep 2012</option>
<option value="20120905">Wed 5 Sep 2012</option>
<option value="20120906">Thu 6 Sep 2012</option>
</select>
I have the following Capybara Ruby code:
select "20120905", :from => "date"
But this errors with:
cannot select option, no option with text '20120905' in select box 'date' (Capybara::ElementNotFound)
However, if I do
select "Wed 5 Sep 2012", :from => "date"
It's ok.
Is it possible to select an option in Capybara by Value not Text?
Thanks
This will work to select an option by value:
find("option[value='20120905']").click
To maintain the scope of the selector you could wrap it in a within block as such:
within '#date' do
find("option[value='20120905']").click
end
With Poltergeist as driver I can't click on an option like suggested in some of the other options above, instead you can do the following:
page.find_by_id('date').find("option[value='20120905']").select_option
I wrote a helper method:
def select_by_value(id, value)
option_xpath = "//*[#id='#{id}']/option[#value='#{value}']"
option = find(:xpath, option_xpath).text
select(option, :from => id)
end
Save in a .rb file in spec/support/
Example use:
before do
select_by_value 'some_field_id', 'value'
click_button 'Submit'
end
You can also achieve it by doing the following:
find_by_id('date').find("option[value='20120905']").click
That helper method is pretty clever. I would change it just a little bit:
def select_by_value(id, value)
option_xpath = "//*[#id='#{id}']/option[#value='#{value}']"
find(:xpath, option_xpath).click
end
or just:
find(:xpath, "//select[#id='date']/option[#value='20120904']").click
In my case I have a few options with same text, that's the reason why I need select by value. Combining a few answers together I've found the best solution for me:
def select_by_value(id, value)
option_xpath = "//*[#id='#{id}']/option[#value='#{value}']"
find(:xpath, option_xpath).select_option
end
Click using find_field works fine:
find_field("date").find("option[value='20120905']").click
You could also use capybara-ui which will look first to match the text, then to match the value.
# define your form widget, in this case in a role
class UserRole < Capybara::UI::Role
form :my_form do
select :my_select, 'my_select'
end
end
# then just submit your form params via #submit
role = UserRole.new
role.submit :my_form, my_select: '20120905'
See more about capybara-ui forms here.
In case of not visible field, try this
find("#date", visible: false).find("option[value='20120905']").click
Or with scope as:
within '#date', visible: false do
find("option[value='20120905']").click
end
You're setting the value.
find(selector).set(value)

Resources