I want to write step in Cucumber Scenario for selecting option "Grandfather's middle Name"
from drop down list of security_questions.
<td align="left">
<select id="security_question" class="sign-up-security Signup_red_text" name="user_detail[secret_question]">
<option value="Please Select"> Please Select</option>
<option value="grandfather middle name"> Grandfather's middle Name</option>
<option value="first date last name"> First Date's Last Name</option>
<option value="favorite place"> Favorite Place</option>
</select>
<br>
<div id="questionError" class="sign-up-ht"></div>
</td>
Please help me to write a step for this.
I have step definition for the same as
When /^I select "(.*)" from "(.*)"$/ do |value, field|
select(value, :from => field)
end
and i am writing this as
I select "grandfather middle name" from "user_detail[secret_question]"
But it is giving me error while running features as
Could not find field labeled "user_detail[secret_question]" (Webrat::NotFoundError)
Suggest any way.
Webrat is expecting a <label> tag. You should make that too. You should also be able to reference the <select> element by using the id-attribute.
As a side note: Using HTML names inside your cucumber features is not done. See Dan North's post on that.
Related
I'm doing a search form in my site. And what i want is to keep on my inputs, the value selected by the user. It's ok for my checkbox, but i can't do it for select.
I show you my form first :
<select name="garde" id="garde">
<option value="">Choisir un type de garde</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
<div class="flex items-center">
<input id="chats" name="chats" value="1" id="chats" type="checkbox"
#if(request()->chats) {{ 'checked' }} #endif>
<label for="chats" class="ml-3 text-sm text-gray-600">Chat</label>
</div>
I tried to use some :
<select blablabla>
<option value="{{$garde->id}}" #if(request()->garde) {{'selected'}} #endif/>{{$garde->garde}}</option>
</select>
So it's certainly good for the value, but it doesn't display the good option name. Always sending me the last value from the database (which is 3 in this case).
My controller
$garde = request()->input("garde");
$chats = request()->input("chats");
$g = Garde::where('id', 'like', "%$garde%")->pluck('id');
Annonce::when($g, function ($s) use ($g) {
return $s->where('garde_id', $g);})
->when($chats, function ($s) use ($chats) {
return $s->where('chats', $chats);})
Edit : Here the "%$garde%" was after trying another option, using "$garde" doesn't changed anything.
As i said, it's ok for checkbox, but for select i can not keep this value without doing a "bidouille" as we say in french ^^
<select name="garde" id="garde">
<option value="{{ request()->garde ?? '' }}">
#if(request()->garde == 1) Chez le Pet-Sitter
#elseif(request()->garde == 2) Visite à domicile
#elseif(request()->garde == 3) Chez le Pet-Sitter/En visite
#endif
</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
But this is not a good practice i know it, and it's display 2 times the selected value ofc..
Have you got some ideas?
Maybe not using the pluck('id) in my controller is the key? But what is other way to do it?
I'm spec-ing my form that will have some date pickers.
expect(page).to have_select('Start date') and expect(page).to have_select('deal[start_date]') both return no matches. The latter makes sense given that the html for date pickers is a little funky (name="deal[start_date(2i)])
expect(page).to have_date_select('deal[start_date]') says that Capybara doesn't recognize has_date_select.
Is there a way to do this?
Capybaras has_select? predicate and have_select matcher look for HTML <select> elements. Depending on what you mean by a date picker you'll have to use a matcher that would match the correct type of element, or have_selector if there isn't a specific matcher. If what you are actually checking for is an <input type="date"> element with an associated label containing the text 'Start date' then you can use the have_field matcher:
expect(page).to have_field 'Start date', type: 'date'
If you're using the Rails date_select view helper it produces HTML like
<div class="field">
<label for="post_start_date">Start date</label>
<select id="post_start_date_1i" name="post[start_date(1i)]">
<option value="2014">2014</option>
<option value="2015">2015</option>
...
</select>
<select id="post_start_date_2i" name="post[start_date(2i)]">
<option value="1" selected="selected">January</option>
<option value="2">February</option>
...
</select>
<select id="post_start_date_3i" name="post[start_date(3i)]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
...
</select>
</div>
You can see from looking at that HTML that the <label> element isn't actually associated with any of the form fields (the for attribute doesn't match an id) which means you won't be able to match the <select> elements using the label text. Instead you'd need to use either the id or name and match them individually
expect(page).to have_select('post_start_date_1i')
expect(page).to have_select('post[start_date(2i)]')
...
or you could use the id filter with a regex and a count to check that there are 3 elements matching like
expect(page).to have_select(id: /^post_start_date_[123]i$/, count: 3)
Note, that's not technically as 'correct' as doing them individually since you could actually have 3 elements with id of post_start_date_1i and it would still pass, but in that case your HTML would also be invalid.
If that's not the type of HTML element you're checking for then you'll need to provide the HTML to get an exact answer.
I'm trying to troubleshoot a feature spec I'm writing to learn Rails & testing. It's an edit_spec for a Rails 4 app. I'm using RSpec and Capybara.
The Problem:
Capybara can not find elements from two drop down lists on a form. Here's the relevant parts of my form's source code:
<div class="field">
<label for="appointment_member_id">Member</label><br>
<select id="appointment_member_id" name="appointment[member_id]">
<option value="1">Callis Callis</option>
<option value="2">Rachale Rachale</option>
<option value="3">Hal Hal</option>
<option value="4">Kallis Kallis</option>
<option value="5">Earle Earle</option>
<option value="6">Rudy Rudy</option></select>
</div>
<div class="field">
<label for="appointment_trainer_id">Trainer</label><br>
<select id="appointment_trainer_id"
name="appointment[trainer_id]"> <option value="1">Jacob Jacob</option>
<option value="2">Michele Michele</option>
<option value="3">Alex Alex</option>
<option value="4">Yanni Yanni</option>
<option value="5">Upton Upton</option>
<option value="6">Willham Willham</option></select>
</div>
In my edit_spec, I tried using "option value" and "select id" to get Capybara to select names from the drop-down lists:
select('4', :from => 'appointment_member_id')
select('2', :from => 'appointment_trainer_id')
The result is a Capybara::ElementNotFound error. Here's the full text of the error message:
updates an appointment successfully with corrected information (FAILED - 1)
Failures:
1) Editing appointments updates an appointment successfully with corrected information
Failure/Error: select('4', :from => 'appointment_member_id')
Capybara::ElementNotFound:
Unable to find option "4"
I researched the issue and documentation for Capybara, and also a Stackoverflow post with similar problem. The poster solved the issue, but couldn't recall how he did it. I'm baffled as to why Capybara can't find the member and trainer from the drop down list? The option values and names appear to be visible on the form. What am I missing? I appreciate any help!
Capybara simulates user behaviour. So instead of ids and values try to use what the user see:
select 'Kallis Kallis', from: 'Member'
select 'Michele Michele', from: 'Trainer'
You could try the code below, or possibly assign the div and id and replace "div.field" with whatever id you choose
within "div.field" do
find("option[value='4']", text: 'Yanni Yanni').select_option
click_button 'your_submit_button'
end
I have a select drop down which will display an error if a value is not selected.
<div class="field contain" >
<select name="myselect" id="myselect" class="error" >
<option value="0">--select a value--</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
<label for="myselect" generated="true" class="error" style>Please select at least one option</label>
</div>
I want to validate if this error label has been displayed using GEB. Tried to select it with 'error' selector does not work. Any suggestions would be helpful.
Thanks,
Abhijith
This doesn't work?
assert $("select.error option").text() == "--select a value--"
Try
assert $("label", for:"myselect").text() == "Please select at least one option"
Try CSS selector.
$('label[for=myselect]').text()
This one selects label tag with value of the "for" attribute = myselect. Then "text()" method returns it's body: "Please select at least one option"
I have an embed in my safecracker form.
Embed has a select box and can select a name of
a Tea.
I want to send name of the Tea with safecracker URL to next page.
This is the code:
{exp:safecracker channel="blending_log" return="sthome/blending/ENTRY_ID" dynamic_title=" [selected_tea]_[organic_or_conventional][date_time][agent_number]"}
<h3>Select and enter data</h3>
{embed="stembeds/.blending_select_tea"}
{!--------Receive data to create value--------}
<input type="hidden" name="title" value="" />
<br /><br />
Organic Or Conventional:
{field:org_con}
Agent Number:
{field:agent_number}
{!-----END Receive data to create lot number-------}
The embed is:
{embed="stembeds/.blending_select_tea"}
This is the code in embed:
<select name="selected_tea">
<option selected="selected" >Select a Tea</option>
{exp:channel:entries channel="tea_herbs" order_by="title" sort="asc"}
<option name={title} id={entry_id} value={url_title}>{title}</option>
{/exp:channel:entries}
</select>
I want to select tea from embed and I want to send the Tea name with safecracker URL:
{exp:safecracker channel="blending_log" dynamic_title="[selected_tea]_[organic_or_conventional][date_time][agent_number]" return="sthome/blending2_nimmi/ENTRY_ID"}
Please help me to solve this issue.
Thank you very much
Make sure you have a text field in your "blending_log" channel with the name "selected_tea" - otherwise SafeCracker won't save that value.
Your embed should look like this:
<select name="selected_tea">
<option value="" selected="selected">Select a Tea</option>
{exp:channel:entries channel="tea_herbs" order_by="title" sort="asc" dynamic="no" disable="member_data|pagination|categories"}
<option value="{url_title}">{title}</option>
{/exp:channel:entries}
</select>