Submit a disabled selected option in a Codeception Functional test - functional-testing

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/>?

Related

Option selected is equal to database value

I'm trying to build a form to edit my content and have a select box. How do i make it so the rating score that was inputted into the database through the option value(1-5) will be the selected option when the user enters the edit form?
edit code:
{{ Form::label('ratings_id', 'Ratings') }}
<select class="form-control edit-form" name="ratings">
<option value="1">Very Poor</option>
<option value="2">Poor</option>
<option value="3">Fair</option>
<option value="4">Good</option>
<option value="5">Very Good/option>
</select>
the old rating score(1-5) can be reached through this:
$reviews->rating
How do i make it so that the old rating score shows as the selected option value, when they user enters the form?
Compare the value of the option with the $rating being edited.
<option value="2" {{ $rating->value === 2 ? 'selected' : '' }}>Poor</option>

Select input ,selected Text codeigniter

I have form with input select in the aplication/view codeigniter
and the form is submited by codeigniter, no ajax.
<select name="sel_options">
<option value="1">hi</option>
<option value="2" selected>bye</option>
</select>
in my aplication/controller
$this->input->post("sel_options");
result:
2
but i need the text ("bye");
When you post a value, it's going to use the value from the input. So in your case:
<select name="sel_options">
<option value="1">hi</option>
<option value="<!-- I'm Submitting this value -->" selected>bye</option>
</select>
The value for the second option is 2 (<option value="2" ... />) if you wanted to pull in the value from the html ('bye'), you'd have to set the value to be bye.
<option value="bye" selected>bye</option>

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

Cucumber how to simulate multiple drop down selection?

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

Html select dropdown list - how to choose the selected value from server code

I have a select list say of states in the country, which i have in a helper to include easily in any form. (removing most options to make it brief).
I have the value of the current selection stored in the database say "CA". How would i set selected="true" to option CA before rendering the list to the user?
#helper StateSelect(string name = "State")
{
<select name="#name" id="#name" class="required">
<option value="">-- Select -- </option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
<option value="AR">Arkansas</option>
<option value="AZ">Arizona</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="VA">Virginia</option>
<option value="VT">Vermont</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
}
As Darin Dimitrov says, the built-in stuff would be better. However, if you do need to, I think you have a few options:
Add code like this to every line:
<option value="CA" #(name == "CT" ? "selected=selected" : "")> Connecticut</option>
Just re-add the selected item to the top of the list
This keeps the code cleaner, the selected option is just repeated at the top (and selected there), before the entire list

Resources