Cucumber how to simulate multiple drop down selection? - ruby

Hi I'm new to Cucumber and Ruby on Rails, and I can't figure out how to simulate multiple drop down menu selection. I am able to do it for a single drop down menu (in my case "Rating").
Given /the following movies exist:$/ do |movies_table|
movies_table.hashes.each do |movie|
# each returned element will be a hash whose key is the table header.
# you should arrange to add that movie to the database here.
Given %Q{I am on the Create New Movie page}
When %Q{I fill in "Title" with "#{movie[:title]}"}
And %Q{I select "#{movie[:rating]}" from "Rating"}
And %Q{I select "#{movie[:release_date]}" from "Released On"}
And %Q{I press "Save Changes"}
end
assert false, "Unimplmemented"
end
The error that i believe is relevant to my problem is.
cannot select option, no select box with id, name, or label 'Released On' found (Capybara::ElementNotFound)
(eval):2:in `select'
./step_definitions/web_steps.rb:86:in `/^(?:|I )select "([^"]*)" from "([^"]*)"$/'
filter_movie_list.feature:9:in `Given the following movies exist:'
What I believe is the relevant source code from my web app is
<label for="movie_title">Title</label>
<input id="movie_title" name="movie[title]" size="30" type="text" />
<label for="movie_rating">Rating</label>
<select id="movie_rating" name="movie[rating]"><option value="G">G</option>
<option value="PG">PG</option>
<option value="PG-13">PG-13</option>
<option value="R">R</option>
<option value="NC-17">NC-17</option></select>
<label for="movie_release_date">Released On</label>
<select id="movie_release_date_1i" name="movie[release_date(1i)]">
<option value="2007">2007</option>
<option value="2008">2008</option>
< And so on ...>
</select>
<select id="movie_release_date_3i" name="movie[release_date(3i)]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
< And so on ...>
</select>
<input name="commit" type="submit" value="Save Changes" />
So I am refered to the label "Released On" but my error message says it has no select box, yet it works for the Rating. Also from my .feature file is in the form of
25-Nov-1992
If it plays a part at all. But I assumed not since since I think ruby understands how to handle dates. Thank you for your time.

The problem is that the Released On label has for="movie_release_date" where as the select box has id="movie_release_date_1i". The for and id values have to match for Capybara to locate the field based on its label text.
Possible Solutions:
1) Correct the HTML if you have control over it.
You want to either have:
<label for="movie_release_date">Released On</label>
<select id="movie_release_date" name="movie[release_date(1i)]">
or
<label for="movie_release_date_1i">Released On</label>
<select id="movie_release_date_1i" name="movie[release_date(1i)]">
2) Access the select field by ID or Name.
And %Q{I select "#{movie[:release_date]}" from "movie_release_date_1i"}
or
And %Q{I select "#{movie[:release_date]}" from "movie[release_date(1i)]"}
This option will only work if the ID or Name is static (ie that number at the end is always the same, not randomly generated each time the page loads).
3) Use Capybara directly rather than using web_steps.rb
If you use Capybara directly, rather than using web_steps.rb (which I assume the steps you have are from), you get a lot more options for locating the select field (ex xpath).

Related

Submit a disabled selected option in a Codeception Functional test

I'm trying to test my backend by passing invalid options to it. I have a select item like so:
<label for="title">Title</label>
<select id="title" name="title" required="required">
<option value="" disabled="" selected="selected">Select one</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
<option value="Sir">Sir</option>
<option value="Prof">Prof</option>
</select>
The user must select an option, but there's no sensible default for a person's title so "Select one" is displayed, but can't be reselected once another option has been chosen.
However, in the Functional test
If I don't select anything, Mr (the first non-disabled option) gets submitted.
If I try to explicitly set Select one with the following code:
$I->selectOption('title', 'Select one');
I get a message saying:
[InvalidArgumentException] Input "title" cannot take "" as a value (possible values: "Mr", "Ms", "Mrs", "Miss", "Dr", "Sir", "Prof").
How can I get a Codeception Functional test to submit a disabled, selected <option/>?

Capybara Datalist Select not visible

I have the following HTML snippet:
<input type="text" id="manufacturer" list="manufacturers" placeholder="Search by manufacturer name or supplier code" class="form-control form-control-2-3" value="" name="manufacturer">
<datalist id="manufacturers">
<select>
<div>
<option value="Jaguar">AA</option>
<div></div>
</div>
<div>
<option value="Audi">AB</option>
<div></div>
</div>
<div>
<option value="Mercedes">AC</option>
<div></div>
</div>
</select>
</datalist>
It's a dropdown menu and I want to select one of the options. No matter what I try with any find command or select function. I always get the same error:
Selenium::WebDriver::Error::ElementNotVisibleError: element not visible: Element is not currently visible and may not be manipulated
Does anyone have any suggestions on how to scope to those options and select one?
Thanks.
What you're trying to do isn't currently possible because it's not actually a dropdown select element. The <datalist> option elements are never actually visible on the page because the standard states "In the rendering, the datalist element represents nothing and it, along with its children, should be hidden." - https://html.spec.whatwg.org/dev/form-elements.html#the-datalist-element . Instead any <option> elements in the datalist are just used as autofill suggestions for the input element (but don't actually restrict the value a user can input in any way). Because of this and since the datalist user can just type anything they want into the input element you can set the input value like any other text input.
fill_in("manufacturer", with: 'Jaguar')

Rspec + Capybara: Validating select options using xpath

I have an HTML document (there is a DIV with an ID of "countries" that wraps this code below), and in that I need to -
Validate the options in the select. I tried this but this is not valid.
expect(page).to have_xpath(('//*[#id="countries"]//select')[1],
:options => ['US CAN GER POL'])
Validate that the second select is disabled
Validate that CAN is disabled in the first select
Validate that POL is selected in the first select
Change the selected option to GER in the first select
<li>
<fieldset>
<select>
<option value="US">USA</option>
<option value="CAN" disabled>Canada</option>
<option value="GER">Germany</option>
<option value="POL" selected>Poland</option>
</select>
<fieldset>
<li>
<li>
<fieldset>
<select disabled>
<option value="US">USA</option>
<option value="CAN">Canada</option>
<option value="GER">Germany</option>
<option value="POL">Poland</option>
</select>
<fieldset>
<li>
I appreciate any help that you can provide. Thanks!
Word of warning, I personally still use the should syntax for rspec, and I also use css selectors versus xpath. I simply find them easier to read.
(1) Because you have two dropdowns without any specific IDs or class names identifying one from the other, I would use all to restrict the context of your expectations
all('#countries select')[0].should have_text('USA Canada Germany Poland')
(2) Same concept as above, restrict the scope. Second fieldset should contain a disabled dropdown.
all('#countries fieldset')[1].should have_css('select[disabled]')
(3) all('#countries select')[0].should have_css('option[disabled]', :text => 'Canada')
(4) Same answer as #3 but with different attribute and text
(5) all('#countries select')[0].find('option', :text => 'Germany').click

Selecting second Dropdown Value Watir-Webdriver

Design:
First Dropdown:
<select id="MainContent_drpVehicleType" style="width:175px;" name="ctl00$MainContent$drpVehicleType">
<option value="">- SELECT -</option>
<option value="1" title="AUTO">AUTO</option>
<option value="2" title="HD">HD</option>
<option value="3" title="MARINE">MARINE</option>
</select>
Second Drop Down:
<select id="MainContent_drpMake" style="width:175px;" name="ctl00$MainContent$drpVehicleType">
<option value="1" title="ACURA">ACURA</option>
<option value="2" title="ALFA ROMEO">ALFA ROMEO</option>
<option value="74" title="ALLIS CHALMERS LIFT TRUCK">ALLIS CHALMERS LIFT TRUCK</option>
<option value="75" title="ALLIS CHALMERS TRACTOR">ALLIS CHALMERS TRACTOR</option>
<option value="4" title="AMERICAN MOTORS">AMERICAN MOTORS</option
</select>
Code used for execution:
b.select_list(:id, "MainContent_drpVehicleType").select("AUTO")
b.select_list(:id, "MainContent_drpMake").select("ACURA")
and also tried
`b.select_list(:id, "MainContent_drpMake").wait_until_present.option(:text, 'ACURA')`
**What my Problem is able to select "AUTO" from first dropdown
and not able to select "ACURA" from second dropdown
Error While Executing:
C:/Ruby193/menu.rb:23:in `<main>':
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir-webdriver/el
ements/select.rb:218:in `no_value_found': "ACURA" not found in select list (Wati
r::Exception::NoValueFoundException)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:152:in `rescue in select_by_string'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:149:in `select_by_string'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:131:in `select_by'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.2/lib/watir
-webdriver/elements/select.rb:64:in `select'
from C:/Ruby193/menu.rb:23:in `<main>'**
It sounds like your second dropdown is populated after the first dropdown value is selected. In that case, you need to wait for the second dropdown list to be populated (rather than for the second dropdown to appear).
You can wait for the specific option to appear and then set it by doing:
#The option element that you want:
option = b.select_list(:id, "MainContent_drpMake").option(:text => "ACURA")
#Wait for the option to appear
option.wait_until_present
#Set the option
option.select
Or if you want to do it on one line, you can use when_present:
b.select_list(:id, "MainContent_drpMake").option(:text => "ACURA").when_present.select

Watir question regarding selecting a hidden dropdown

I have two dropdowns, the second dropdown does not show until a choice is made from the first one. Using watir, i can select the first dropdown, and when i watch it, the second one becomes active, but it cannot select it. i just tried the regular select_list using name and id. Here is the code for the second drop down.
<td>
<input type="hidden" value="1" name="list" id="list">
<script type="text/JavaScript" language="JavaScript"></script>
<select>
<option value="">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
I've also notice the value for the hidden field change as i select different options.
Thanks for any help
The way that I usually access drop down lists is by using this string:
#browser.select_list(:name, "list").set("3")
Does this help?
I usually select hidden dropdowns this way
ie.hidden(:name=>"list").value='2'
Try this:
browserObj = Watir::Browser.new
browserObj.goto('you html in browser')
//browserObj.hidden(:id,"list").value
browserObj.select_list(:name, "list").select_value('2')

Resources