I am a beginner to cucumber. I am trying to write a test to click a button. Here is my code
</div >
<%= link_to like_user_user_path(user),:class => 'users-button like-button', :id => 'like1', :method => :post do %>
<span class="fa fa-thumbs-up hvr-bounce-in" aria-hidden="true"></span>
<% end %>
How can I write code to test when I click to specific button like this?
Technically what you're showing is a link that is styled to look like a button, so it will not work with Capybaras click_button. Because of that you need to use #click_link, #click_link_or_button, or just find the element some other way and call #click on it. As a full cucumber step that could be written something like
When /I click on the "(.+)" link/ do |locator|
page.click_link locator
end
and then called like
When I click on the "like1" link
Another option would be to do something like
When /I like the user/ do
find(:css, 'a span.fa-thumbs-up').click
end
Related
This is my code in view file:
<%= f.label t('city'), class: "form-control-label" %><i>(<%= t('select-a-city', :default => 'Select a city') %>)</i>
<%= f.select :city, options_for_select(['city1', 'city2', 'city3']), {}, class: "form-control" %>
This is cucumber step definition:
Then(/^I select "([^"]*)" from the "([^"]*)" dropdown list$/) do |param1, city|
select(param1, :from => I18n.t(city))
end
This throws this error:
Unable to find select box "City" (Capybara::ElementNotFound)
./features/support/step_definitions/home_page_steps.rb:27:in `/^I select "city1" from the "City" dropdown list$/'
But if I modify the view file like this:
<%= f.label :city, class: "form-control-label" %><i>(<%= t('select-a-city', :default => 'Select a city') %>)</i>
the test passes successfully.
So, my first clue was, may be cucumber can't retrieve the translation. I searched a lot, tried some suggestions like using :locale from the ENV etc.
But nothing works.
NB: searching content works with or without the translation (I18n).
Any help would be much appreciated.
You can only use element selector and option value as selected dropdown options. The option will be selected depending on the value.
Example:
# When I select option "My Test Inc Account" from ".accounts-dropdown"
When(/^I select option "(.*?)" from element "(.*?)"$/) do |option,
selector|
all(selector).last.find(:option, option).select_option
end
You can read more in this article, it explains different types of steps you can write in cucumber.
What is the best practice using hidden fields in html views?
In my html.erb I need to check if a value exists, if it does then u want to ensure a button remains disabled.
Can I set a hidden field in ruby code like this ;
<% if #is_draft %>
<input type="hidden" id="isdraft "value="true"><% end %>
Then can I access this in my jquery code?
Is this accepted practice? Note that the javascript is in its own file, the script is not in the html.erb file
It is regarded as pretty poor practice most of the time.
Instead it is usually better to render the button as disabled on the server side after checking the condition.
Also make sure to sanitize the requests made by this form (button is clicked although your condition does not permit it). Users could un-disable the button and click it.
If you find that you have to save some information in HTML for whatever reason, I would resort to data-*-attributes!
Hope that helps!
EDIT pseudocode example:
# MyView.html.erb
<% unless #is_draft %>
<%= render partial: 'my_checkbox_button_enabling', format: [:js] %>
<% end %>
<%= button_tag "My nice button", disabled: true, id: "my-button" %>
I am working in Spree, and I am trying to use Deface to change this
<% if order.has_step?("delivery") %>
<div class="columns alpha four" data-hook="order-ship-address">
<h6><%= Spree.t(:shipping_address) %> <%= link_to "(#{Spree.t(:edit)})", checkout_state_path(:address) unless #order.completed? %></h6>
<%= render :partial => 'spree/shared/address', :locals => { :address => order.ship_address } %>
</div>
<% end %>
<% if #order.has_step?("delivery") %>
<div class="columns alpha four">
<h6><%= Spree.t(:shipments) %> <%= link_to "(#{Spree.t(:edit)})", checkout_state_path(:delivery) unless #order.completed? %></h6>
<div class="delivery">
<% order.shipments.each do |shipment| %>
<div>
<i class='fa fa-truck'></i>
<%= Spree.t(:shipment_details, :stock_location => shipment.stock_location.name, :shipping_method => shipment.selected_shipping_rate.name) %>
</div>
<% end %>
</div>
<%= render(:partial => 'spree/shared/shipment_tracking', :locals => {:order => #order}) if #order.shipped? %>
</div>
<% end %>
..into this.
<div class="columns alpha four" data-hook="order-ship-address">
<h6><%= Spree.t(:shipping_address) %> <%= link_to "(#{Spree.t(:edit)})", checkout_state_path(:address) unless #order.completed? %></h6>
<%= render :partial => 'spree/shared/address', :locals => { :address => order.ship_address } %>
</div>
I've already submitted a pull request about the redundant if statement and adding a data-hook to the second if #order.has_step?("delivery"), but in the meantime I need to write a deface override that will change the page to how I need it to look. I might be able to remove the first if #order.has_step?("delivery") since I think Deface will target the first instance of what I'm talking about on the page if I write
:remove => "erb[silent]:contains('if order.has_step?(\"delivery\"')")
although to be honest the documentation is not very good on that point. Anyway, how can I remove the entirety of the second if statement? There's no data-hook to target it, and using
:remove_content => "erb[silent]:contains('if order.has_step?(\"delivery\"')")
just removes the content of the first if statement. I can't target the first div in the second if statement since there's already a div class="columns alpha four" in the first if statement. I don't want to leave an empty div on the page, so what can I do?
The two if statements in the referenced code do not refer to the same variable. The first if refers to order, and the second to #order.
When using deface to remove existing sections of erb, the string passed in to contains must exactly match the code you want to move in order for the override to properly locate it. Since the second if statement uses #order, and your matcher doesn't include the # symbol, it won't remove that particular if statement.
Based on the rspec tests in the deface repo, it doesn't appear that you can currently use multiple matching strings with one override in the remove action. Instead, you'll have to use a second override to mach the second if statement and remove it.
The pull request I posted to Spree made this whole issue moot (which you can find here if you're interested: https://github.com/spree/spree/pull/5692 ). Also, it turns out that #order and order function in exactly the same way in that document, and all references to #order are in the process of getting removed from Spree anyway.
I cannot for the life of me figure this out. I have a calendar link that displays the previous (and next) month.
This was built right off of screencast #213 with Ryan Bates as a starting point.
I am trying to have the previous and next links display as buttons. Here is the code for the link to "previous" month ("next" month is the same)
<%= link_to "Previous", date: #date.prev_month %>
now there is also a new event path which I did turn into a button, code below.
<%= link_to 'New Event', new_event_path, class: "btn btn-small btn-info" %>
Adding the same button code does not work on the "previous" and "next" links. I am relatively new to this stuff but it seems it has to do with the "date:" part and how the link is being created to previous and next month.
Can anyone make a suggestion how to get this to display as a button while still functioning the same, or how to achieve the same functionality while displaying as a button?
I think you need to add curly brackets, since the link_to helper expects 2 hashes after the name. This should work:
<%= link_to "Previous", { date: #date.prev_month }, { class: "btn btn-small btn-info" %>
Here's a resource that should be able to help you with this issue: http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
I would play around with your next/previous links and check out the HTML that gets generated. Also, you can right click the browser page and select "inspect element" in chrome. That will help you debug the CSS.
Hope this helps!
I've been having this annoying problem in which I click over a select dropdown input of my custom form, styled with Zurb Foundation 4 in my Rails application, and the list won't show its elements.
I thought at a start that was a problem with simple form, but I changed the f.association for f.collection_select, my code looks like this:
<h2><%= I18n.t(".sign_up") %></h2>
<%= simple_form_for(resource, :html => {:class => "custom"}, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<%= devise_error_messages! %>
<%= f.input :rut %>
<%= f.input :name %>
<%= f.input :email %>
<div>
<%= f.label :supplier_type_id %>
<%= f.collection_select :supplier_type_id, SupplierType.all, :id, :name %>
</div>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.error :base %>
<%= f.submit I18n.t(".sign_up"), :class => "button" %>
<% end %>
<%= render "devise/shared/links" %>
The most strange thing is that sometimes I'm able to see the items when refreshing the page, but when I get to the page navigating from other view of the app then it won't work. I've also noticed this when using hints for forms (i.e: If I have two hints in the same form, in different inputs, only one would show, but the same one message displays in both inputs when each message should show in their respective input. When reloading the page sometimes it shows one hint, and sometimes the other)
The styling looks good, so I think that it might be a foundation javascript problem.
Another thing I've noticed is that when I load the page the styling does a kind of "blinking" when using custom forms. This blinking it looks likes foundation takes a while to load the styling, I've also noticed this on their own custom form documentation site. This may indicate that is a form styled with javascript events or something similar, so this might mean that javascript is working well.
In addition, the checkboxes are having a similar problem, they only can be checked just when you reload the page, it might have some relation with this problem.
I'm very lost, some help would become very handy. Thanks!
--edit: Foundation 5 doesn't include custom forms and works better--
You might need to refresh dropdowns on each page:change event. Try something like this:
$(document).on("page:change", function() {
// SELECTOR_TO_CUSTOM_DROPDOWNS should select any Zurb custom dropdowns you
// are using.
$(SELECTOR_TO_CUSTOM_DROPDOWNS).trigger("change");
});
That's from the documentation on Zurb custom form JS.
Yes, this is caused by turbolinks. It stops $(document).ready from firing on page load, which is required by foundation's custom forms.
Using ssorallen's answer and to be more unobtrusive than the OPs, add this to application.js:
$(function(){ $(document).foundation(); });
$(document).on("page:change", function() {
if ($('form.custom').length > 0) {
$(document).foundation('forms');
}
});
Also, if you have jquery/coffeescript that relies on document ready being fired, add jquery turbolinks to your Gemfile.
Ok, so I just figured out that I have repeated asset load on my browser. This is causing a javascript error. It appears that deleting in the manifest
//= require turbolinks
Solves the problem.
I solved my multiple asset problem changing
<%= javascript_include_tag "application" %>
To the head in my application layout.
After watching the turbolinks railscast I noticed that besides my multiple asset loading, turbolinks and foundation 4 may not be compatible, it might be a solution on this post. But still doesn't work perfect for me.
I also noticed that navbar is also affected by turbolinks.
I think that this is rather a turbolinks problem and not an specific foundation dropdown. I will close this question and open a new on turbolinks and foundation.
Thanks to some Nick Reed insights I found out that the foundation gem was initializing foundation in application.js like this:
$(function(){ $(document).foundation(); });
So I checked the docs and I used this:
<script>
$(document).foundation();
</script>
After the "/body" tag in the application layout and everything seems to be working like a charm!