Rspec + Capybara: Validating select options using xpath - 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

Related

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

ElementNotVisibleError - A Specific Watir-Webdriver Issue

Trying to do
b.select_list(:name => "bedroomsMin").select '3+ Beds'
on
<div class="beds col-sm-2 hidden-xs">
<div class="form-group">
<select class="form-control wide" name="bedroomsMin">
<option value="null">All Beds</option>
<option value="0">0+ Beds</option>
<option value="1">1+ Beds</option>
<option value="2">2+ Beds</option>
<option value="3">3+ Beds</option>
<option value="4">4+ Beds</option>
<option value="5">5+ Beds</option>
</select>
</div>
</div>
but get the following error:
element not visible: Element is not currently visible and may not be manipulated (Selenium::WebDriver::Error::ElementNotVisibleError).
This list selector is contained in a dropdown element that is clicked like this:
b.link(:class => 'btn-open-filers').when_present.click
How can I select if it's not visible? Is there a way to force visibility?
Sounds like a timing issue. When the link is clicked, the dialog containing the select list may not appear before Watir tries to interact with it.
Try waiting for the select list:
b.select_list(:name => "bedroomsMin").when_present.select '3+ Beds'
Looks like the <div class="beds col-sm-2 hidden-xs"> having hidden may have something to do with it.
Also it appears you're are only checking for when the element is present, which is to say if it loads in the html but is not visible it'll still return true.
Here's a quick read on that:
What's the difference between `visible?` and `present?`?
Since you have it clicking the dropdown after it checks for it's presence, there might be a timing issue. In which case it may check that the dropdown is present in the DOM but is not actually opening the dropdown.
May want to try something like:
dropdown = b.link(:class => 'btn-open-filers')
dropdown.click if dropdown.visible?
b.select_list(:name => "bedroomsMin").select '3+ Beds'
Watir is designed to interact with the elements that a user can interact with. So if it isn't visible to a user, you shouldn't be able to interact with it. So, it's feature, not a bug. :)
That being said, it isn't clear from your post what action is causing the error. This code will wait for the select list to become visible before you try to select an option inside it:
b.link(class: 'btn-open-filers').when_present.click
b.div(class: 'form-group').wait_until_present
b.select_list(name: 'bedroomsMin").select '3+ Beds'
Justin's answer is functionally the same as this, and when_present is probably more elegant.

Remove some items in dropdownlist based on selected keywords

I have a dropdownlist in my page and the html part lists below:
<select id="Permission_id" name="Permission_id"><option value="">Pls select</option>
<option value="001f492b-fbb2-440c-b2ac-1fab5d97f5e3">super</option>
<option value="559ede95-1dbb-45d5-ae41-2df6f460e87a">admin</option>
<option value="80b330ee-37dc-42c4-8902-c30254112c11">user</option>
<select>
And in the same page, I have another dropdownlist , the html part lists below:
<select id="TopPranet_id" name="TopPranet_id"><option value="">Pls select</option>
<option value="65c61442-e02f-4071-b746-2c7cbfcc859a">Settings--admin</option>
<option value="66d21c35-66a2-40d8-a9db-675b337fa0bb">Control--admin</option>
<option value="cd612bfc-9f61-4228-b986-90bda4be9d8d">Action--admin</option>
<option value="2a137846-5ae0-4104-b0c9-be09a0e772d8">Settings--super</option>
<option value="b75c43f3-cafb-41a2-a76b-c572fffd8a6a">Control--super</option>
<option value="2c3f618c-373f-4559-967f-d8dcea1b6996">Action--super</option>
</select>
Now I want to do things like below:
When Permission_id's select item changes, then TopPranet_id's item will only contains the items that have Permission_id's selected text inside. that is, if I select admin in Permission_id control, then TopPranet_id control will only contains Settings--admin,Control--admin,Action--admin item, others are removed.
I know I can use Jquery to do this, but I am a newer to this, also, regex maybe also required for doing this.
Need your help, thx.
Please check here i have added one ex
Here
You html all seems ok
Now
in javascript
$('#Permission_id').change(function()
{
$('#TopPranet_id option').each(function()
{
if(this.innerHTML.indexOf($('#Permission_id :selected').text())!=-1)
{}
else
{
this.style.display='none';
}
});
});
We have added change function which will detect the change event of the first dropdown and after this we will check that text with second dropdown which will show and hide the dropdowns

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

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