Select a value from a select List by value element - ruby

recently I came upon an issue while using page object that I could not find an answer to.
When using a select list to choose an option. I need to select by partial text or by attribute. here is an example:
<select id="custcol95" name="custcol95">
<option selected="" value="">- Select -</option>
<option value="5">Monthly Basic Plan - $27.99</option>
<option value="1">Monthly Business Plan - $54.99</option>
</select>
I tried:
select_list(:planPurchase, :id => 'custcol95')
I can do this:
selectCloudPlan_element.option(:value, CLOUD_PLANS['PersonalMonthly']).select
but that is deprecated.
I have yet to see a select by value option or by index anywhere on the web. Does one exist?
Also if there was a search by partial text that would fix my issue.
Thanks in advance.

The page object gem already supports selecting by partial text:
page.planPurchase = /Basic/
puts page.planPurchase
#=> "Monthly Basic Plan - $27.99"
Selecting by value is also supported by using select_value:
page.planPurchase_element.select_value('5')
puts page.planPurchase
#=> "Monthly Basic Plan - $27.99"
The page object gem supports selecting options by index using []. Note that you have to use .click as there is no .select. The index is 0-based.
page.planPurchase_element[2].click
puts page.planPurchase
#=> "Monthly Business Plan - $54.99"

Related

How to set default selected option in very big select tag with many options in Laravel?

I have 3 select tags with a lot of options inside them. One for year, one for month and one for day.
The question is ... How to set selected on the option I want.
I was thinking of this solutions:
Pass the options as array to the view. After that forech them in the select tag and with "#if" check every single one if its the right one and set "selected:selected".
But in my case this isnt good solution because I have many options ... that means the array will be very very big.
Put if statement in every option in the html and check if its te right one and set "selected:selected" to that option. This is also a bad solution ... there will be like 200+ if statements.
Maybe create 3 db tables with this options inside. Getting them in the controller method, foreaching them in the select tag and checking them with if statement and setting "selected" to the right one.
I really dont know if Im thinking in the right way. I will apriciate some help.
what you r doing is right here is a solution that it did worked for me, i have a user city, and when i want the user to update the city i show it as selected
just pass the arrays to the view then :
#foreach ($citys as $city)
#if (Auth::user()->city_id == $city->id)
<option value="{{$city->id}}" selected="selected">{{$city->name}}</option>
#else
<option value="{{$city->id}}">{{$city->name}}</option>
#endif
#endforeach
here is a short version u can use
#foreach ($citys as $city)
<option value="{{$city->id}}" #if (Auth::user()->city_id == $city->id) selected="selected" #endif > {{$city->name}}</option>
#endforeach

In Angular JS Kendo Drop down not setting the model value

HI I have following Kendo Dropdown, even though I have a value in ng-model, the dropdown is not selecting the default values.
<select name="myDropDown"
id="myDropDown"
class="width-90"
kendo-drop-down-list k-data-text-field="'key'"
k-data-value-field="'value'"
k-data-source="myDataSource"
ng-model="model.selectedProperty"></select>
model.selectedProperty does have the value . However the UI does't select the options.
DataSource Values:
[{"key":"Critical","value":0},{"key":"High","value":1},{"key":"Medium","value":2},{"key":"Low","value":3}]
model.selectedProperty = 1
The above one should have selected the second option, but the dropdown selects none
Well I have to make the selected value same object as in the list:
$scope.model.selectedProperty = {"key":"Critical","value":0}
And I updated the select tag to,
<select name="myDropDown"
id="myDropDown"
class="width-90"
ng-model="model.selectedProperty"
ng-options="option.key for option in myDataSource track by option.value">
</select>

Selecting the prompt with Capybara

I have a select dropdown:
<%= f.select :image_size_id, options_for_select(#image_sizes.collect{ |i| [i.dimension, { id: i.dimension} ]}), { prompt: "Please select a Print Size" }, id: 'image_size_select' %>
Which returns this in HTML:
<select id="image_size_select" name="cart_item[image_size_id]">
<option value="">Please select a Print Size</option>
<option id="10x8" value="13">10x8 £14.0</option>
<option id="A4" value="14">A4 £33.0</option>
<option id="A3" value="15">A3 £36.0</option>
<option id="A2" value="16">A2 £47.0</option>
</select>
At the moment to select an option within my tests I can use for example:
find('#image_size_select').find("option[id='A2']").select_option
find('#image_size_select').find("option[value='14']").select_option
What I want to do though is select the prompt so that I can test my validation, can I set an id of the prompt or select by text ? I have tried:
find('#image_size_select').find("option[text='find('#image_size_select').find("option[id='A2']").select_option']").select_option
But that doesn't work, nor does:
find('#image_size_select').find(:xpath, 'option[1]').select_option
Capybaras #select chooses the option by the text, not value, of the option element. When passed with a :from option it is implemented as find(:select, '<select id, name, or associated label text (the :from option)>').find(:option, '<text of option element>').select_option which means the correct call to select the first option in the specified select is
page.select "Please select a Print Size", from: 'image_size_select'
to select the next option
page.select "10x8 £14.0", from: 'image_size_select'
Due to Capybaras default partial string matching
page.select "10x8", from: 'image_size_select'
would also select the 10x8 option as long as "10x8" is a partial match to only one of the options in the select
page.select "", from: 'image_size_select' should work. select allows you to select options from a select drop-down based on value (which in this case it's actually an empty string) and the select drop-down can be found in the page via name, id or label.
The documentation of the method is at http://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FActions%3Aselect

Xpath to Scrape Size Information with Scrapy

I aim to scrape the following bit of code:
Select Size:</b>
<select name="option[225]">
<option value=""> --- Please Select --- </option>
<option value="480">UK 11 </option>
<option value="478">UK 8 </option>
<option value="477">UK 7 </option>
</select>
I'm facing two problems:
1) the value in "option[225] isn't constant for all the pages on that site and changes from each product.
2) Is there anyway, I can store the data with semi-colon separators between each value.
I wish for the data to be displayed in the following manner:
UK 11;UK 8;UK 7
>>> ';'.join(map(str.strip, sel.xpath('//option[string-length(#value)!=0]/text()')))
'UK 11;UK 8;UK 7'
// for selecting all option tags starting from the root of the document, string-length to filter out first empty name option and ';'.join(...) to join generator elements with ; between them
If "Select Size:" is something that is constant before the select/options you want to select, you can try an XPath expression like this:
xpath_expression = """//b[contains(., "Select Size:")]
/following-sibling::select[starts-with(#name, "option[")][1]
/option[#value != ""]/#value"""
Then, as #Guy suggests, you can use:
u";".join([val.strip() for val in sel.xpath(xpath_expression).extract()])

how to select dropdown on value option in ruby watir?

This is my drop down and i want to select it on its value option
<select id ="">
<option value="01" title=" 01 - Live animals"> 01 - Live animals</option>
</select>
I know that how to select drop down on its content i.e.
ie.select_list(:id, "DropDownList_Product").select("01 - Live animals")
actually I want to select drop down on its value 01,what should i have to do for that ?
Something like this should work:
ie.select_list(:id, "DropDownList_Product").select_value("01")
More information at http://rdoc.info/gems/watir-webdriver/Watir/Select#select_value-instance_method

Resources