Selecting second Dropdown Value Watir-Webdriver - ruby

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

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

Change dropdowns with same .class name

I have three dropdowns with same class name:
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<select class="MyClass">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I want to change any of these dropdown values based on any other dropdrop selected. If I select option two from second dropdown - I want 1st and 3rd dropdown values to be two. All these dropdown should change values no matter which one I select.
Thanks.
You will need JavaScript to do this. Here is an code example using jQuery:
$('.MyClass').on('change',function(){
$('.MyClass').val( $(this).val() );
});
This will add an EventListeneer for the change Element and update all DropDowns to the value of the changed one.
http://jsfiddle.net/CWP7Q/
you can use jQuery or normal JS, but my example contains Jquery, which will give you an idea. Please check the example in Jsfiddle.
The example binds a change event to the class name of the three selectors:
$(".MyClass").change(function(){
$(".MyClass").val($(this).val());
});
and Voila, every selector gets changed.
Somehting like (with jQuery)
$('.MyClass').change(function() {
$('.MyClass').val($(this).val());
});

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

display selected item from dropdown onchange function into html label through javascript , please help how to it

<form method="post">
<select name="users" id="users" onchange="showUser(this.value)" value="">
<option value="">Select a person:</option>
<option value="1" onchange="copy();">tcs</option>
<option value="2" onchange="copy();">wipro</option>
<option value="3" onchange="copy();">Hcl</option>
<option value="4" onchange="copy();">krystal kones</option>
</select>
</form>
I have this drop down i want that when the value is changed like tcs,wipro or hcl then that value should shown in html label
Try putting the onChange attribute in the select tag.
Example: http://jsfiddle.net/r6Fus/
HTML:
<div id="label"></div>
<select id="mySelect" onchange="copy();">
<option value="">Select a person:</option>
<option value="tcs" >tcs</option>
<option value="wipro" >wipro</option>
<option value="Hcl" >Hcl</option>
<option value="krystal kones" >krystal kones</option>
</select>
Javascript:
function copy() {
document.getElementById("label").innerHTML = document.getElementById("mySelect").value
}
Otherwise you could use jQuery.
First of all, the onchange event is for the select element, not the option elements. Those don't actually change. Also, you have two JavaScript functions. showUser() and copy(). But you describe only one piece of functionality. What do these two functions do?
As for showing the text in the label, here's one way to do it (using jQuery, because everybody does):
$(document).ready(function() {
$('#users').change(function() {
$('#myLabel').text($(this).val());
});
});
What this is basically doing is:
Wait until the DOM is loaded and ready.
Bind a function to the change event of the specified select element.
The function contains one line, which sets the text of the specified label to the value of the select.

Resources