I am trying get the text value of table header with a classname in Capybara. I getting the node element in Capybara but I am not able to get the text content of the class when I use the text attribute. How can I get the text of the Capybara node element here?
html.erb
<% #tests.each do |test| %>
<th class="test_name_header"><%= test.name %></th>
<% end %>
capybara_test.rb
all(:css, '.test_name_header', :visible => false).each do |el|
puts el.text
end
This code does not print the text of the element. Nothing is printed but when I puts el in the above code it prints the node element.
The docs for #text - https://www.rubydoc.info/github/teamcapybara/capybara/Capybara/Node/Element#text-instance_method - state that by default it only gets visible text. Since you're specifying visible: false in your all call I am assuming these headers are not actually visible on the page. If that is the case then, as mentioned in the docs, you'd need to do
el.text(:all)
to get non-visible text.
Related
I've got a search form that returns our products. However, if a user inputs a string that contains certain words (in this instance, 'color'), it returns far too many products. I'm trying to remove the string 'color' from the query that is searched on the backend, but maintain the original query's string as #unfiltered_query so I can reference the #unfiltered_query on the front-end template.
if query.include? "color"
#unfiltered_query = query
end
query.slice! "color"
values = query.split
binding.pry
It was not working, so I ran pry to see what was going on. In the form, I searched "Red paint color". When I call #unfiltered_query in pry, it outputs "Red paint", even though I create the method before .slice! is called?!
What am I missing?
Thank you!
p.s. the HTML template that I'm using to reference the instance is:
<div class="search-input"><h2>
<% if #unfiltered_query.present? %>
<%= #unfiltered_query.titleize %>
<% else %>
<%= query.titlelize %>
<% end %>
</h2></div>
Can you try like this :
if query.include? "color"
#unfiltered_query = query.dup
end
query.slice! "color"
values = query.split
binding.pry
This could be due to passing by reference.
My HAML reads:
%table.screenshots
%thead
%trow
%td{:colspan => 12} Screenshots for #{element.name}
%tbody
- screenshots.each do |set|
%tr
- set[1].each do |shot|
- if shot == :blank_cell
%td{:colspan => set[0]}.twelfth
- else
%td{:colspan => set[0]}.twelfth
= image_tag(shot[1]) # <= ERROR APPEARS HERE
- if #redacted
%h1.blur
%span Image blurred in
%br
%span demo report only
%p #{shot[0]}
There are no invisible spaces or tabs after .twelfth.
Why, then, do I get this error?
Illegal nesting: content can't be both given on the same line as %td and nested within it.
BTW, I get the same exception when I run:
haml --debug print.html.haml
The class and id identifiers (. and #) must come after the tag name, and before any attribute hash.
In your code the problem is the line:
%td{:colspan => set[0]}.twelfth
This is interpreted as a td element with a colspan attribute, containing the content .twelfth, which would look like this when rendered, if it were by itself:
<td colspan='7'>.twelfth</td>
However this line also has content nested below, which Haml doesn’t allow.
You can fix this by using an explicit class entry in the attribute hash as you have in your answer, or by moving the .twelth class specifier in front of the attribute hash, like this:
%th.twelfth{:colspan => set[0]}
Fixed it by changing the offending line to:
%td{:colspan => set[0], :class => "twelfth"}
Looks like there's a bug in the HAML interpreter
I'm storing some HTML content in my database and I would like to be able to perform a search using Sunspot while omitting HTML from the hit output and if possible the search itself.
My model:
class Article < ActiveRecord::Base
attr_accessible :caption
searchable do
text :content, :stored => true
end
end
Search action:
def find
#search = Article.search do
fulltext params[:search] do
highlight :name
end
end
end
Template:
- #search.each_hit_with_result do |hit, article|
- unless hit.highlight(:content).nil?
%p= hit.highlight(:content).format { |word| "<span class=\"highlight\">#{strip_tags(word)}</span>"}.html_safe
Some sample content that is being search might be something like:
<h1>Hello world</h1>
<p> Search for me me!</p>
Link
Notice that I'm marking the output as html_safe? This is because I would like the wrap the search text that gets hit with a highlight span, however besides that everything else I want to be stripped completely from the returned text that gets hit. Is this even possible?
What ended up working for me was stripping the content that gets indexed by solr. To do so I had to make the following change inside of my model:
include ActionView::Helpers::SanitizeHelper
searchable do
text :content, :stored => true do
strip_tags(content)
end
end
Adding those changes and running rake sunspot:solr:reindex worked like a charm!
I have a failing rspec view test but the code works - I probably have a variable incorrectly setup but can't figure out what it is.
When I display the contents of #incident_report (pp #incident_report) in my spec, it properly displays the record created by FactoryGirl.
When I display the actual rendered content (puts rendered), it shows the values from the the record I created with FactoryGirl...
But the "rendered.should contain(work_order)" spec fails with:
1) incident_reports/show.html displays the work order number on the incident
Failure/Error: rendered.should contain(work_order)
expected the following element's content to include "54785":
and none of the data is displayed, only the HTML template
spec/views/incident_report/show.html.haml_spec.rb code
require 'spec_helper'
describe "incident_reports/show.html" do
before(:each) do
#incident_report = Factory(:incident_report)
end
it "displays the work order number on the incident" do
work_order = #incident_report.work_order
pp #incident_report #displays an incident_report, id => 1
assign(:incident_report, #incident_report)
render
puts rendered #this DOES have the content from #incident_report
rendered.should contain("Work Order:")
rendered.should contain(work_order)
end
end
show.html.haml code
%h1 Display Incident Report
.navigation
= link_to 'Edit', edit_incident_report_path(#incident_report)
|
\#{link_to 'Back', incident_reports_path}
= render 'form'
.navigation
= link_to 'Edit', edit_incident_report_path(#incident_report)
|
\#{link_to 'Back', incident_reports_path}
Gotta be something really simple I'm overlooking.
Turns out it's because I was using simple_form and when simple_form displays for a "show" action, it puts the field values into the html as a 'value="54785"' attribute. If you display it in a browser, the labels and values all show up correctly, but rspec can't see them.
I had to add
rendered.should have_tag 'input', :with => { :value => "54765", :name => 'incident_report[work_order]' }
to my example to get it to work.
Seems like there should be a better solution but at least now I can continue testing.
I need to render binary content(images) on web page. I'm saving images in the database with datatype binary. Now I need to iterate available images from the database and render on webpage.
Please check the below code that I'm doing. Icon is the image column name in material.
// iterating all materials
<% #materials.each do |material| %>
// for each material
<span><%= image_tag(material.icon) %></span>
<% end %>
Any help would be greatly appreciated..
You need to add an action to your controller along these lines (cribbed from here):
def image
#material = Material.find(params[:id])
send_data #material.icon, :type => 'image/png',:disposition => 'inline'
end
Then call the path to that action in your image_tag. You obviously need to make sure the :type field has the right MIME type, add a route, etc.