Testing for italics with Capybara - capybara

Could anyone suggest the best way to look for italics on a page with capybara. I have a feature test that searches for a specific piece of text within a page. This text is in italics and is also a different colour from everything else on the page. I thought looking for italics would be preferable to searching for a colour value but i am unsure how to do either.
<div class="text-control">
<p class="body">Test text is here and then<i>italics</i> are there</p>
</div>
The feature test is written as follows:
Then(/^the word should be highlighted in the example sentence$/) do
end

It sounds like just need to check that the sentence, the p element, contains the word, the i element. To do this you can do:
# Get the element containing the sentence
sentence_element = find('p.body')
# Check that the sentence includes the italics element with a specific text
expect(sentence_element).to have_css('i', text: 'italics')

Assert a text is present in the particular element, sounds reasonable... you can do that using the suggesting by Justin...
Test a text is in italic... that's not a test you want to add in this layer of testing or in any layer in fact...

Related

How to display part of text bold/underlined that is from database

I have an event page, when I create a new record I also add Event Details and save it to my database. However there are some parts in the text that I would like them to be underlined or Bold
when Displaying the data I use <textarea>
<textarea readonly="readonly" class="NoBorder ">#(Model.TE_DESCRIPTION)</textarea>
so is there a hack to achieve this? I hoped placing <b> or <u> tags to the text to work but it does not.
any help or suggestion is much appreaciated
Edit
it is not important if I use a or or to dipslay data..
My issue is I fetch the text from database, let's say that following is the string I saved to the database
< b > This part of the text is bold the rest are not
the above text is displayed as it is written above however I want it to be displayed like
This part of the text is bold the rest are not
I'm sorry for the bad english

ruby on rails: text filed formatting

I tried to copy and past from Word document to text field using Ruby on Rails.
But all formatting( spaces, bold and other) are disappeared on text filed.
I've just got the simple lines of text without any formatting.
I've read that need to use Simple format tool... but I want that a user be able just to copy and past a text to text field without doing any adjustments.
I mean, I want make all adjustment in advance and the user could just copy and past the text and got all formatting, the same as in Word doc.
The link to file with text field as below.
https://gist.github.com/tatyana12/2f9d39c2f6e4f8fabea5e70e11eaf310
Also I have Application.html.erb file:
https://gist.github.com/tatyana12/15c27d542091b04f3c3adfdfd252b7f4
How to initialize editor if I don't have id = "edit" right now?
How to put some code extra style to this file?
Have a go at wrapping your field in your show.html.erb or wherever you want to display it with simple_format, for example:
<%= simple_format(#object.description) %>
See http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format for more info.
I've solved this problem by implementing SKEditor.
There is a lot of tutorial how to implement this editor.
I (as a lot of other users) have problem that my text wasn't formatted because this editor is not compatible with turbo links.
So, I disable turbo links in some files, and as result I have the text formatted.

Test if text does not contain any HTML

I want to test whether some content does not contain any HTML. What is a simple and clean way to do so?
page.find(".description").should_not have_content /\<.*\>/
Does not work properly, since it fails on <strong>Lorem but passes on <strong>Lorem. Probably due to the way capybara helps its user with escaping HTML.
Solving with xpaths works, but leaves me wondering if there is not a much simpler solution.
page.should_not have_selector(:xpath, "//div[#class="description"]/*")
Is there a built-in way to detect wether some text has been stripped of HTML in Capybara?
Capybara's has_content? (and thus have_content) method aims to inspect text rendered to the user, not text in html source of an element as you expect.
Thus, I think Capybara's behavior is correct:
If html source of .description is <strong>Lorem, user sees <strong>Lorem. Capybara searches this text for /\<.*\>/ and finds it.
If html source of .description is <strong>Lorem, user sees Lorem. Capybara searches this text for /\<.*\>/ and doesn't find it.
If you want to inspect source html of an element, you can use javascript's function innerHTML:
source_text = page.evaluate_script("document.querySelector('.description').innerHTML")
source_text.should_not =~ /\<.*\>/

Rails]Using Markdown Markup Language

In my rails application, people are supposed to submit "posts." However, in the default scaffolding, there are some problems in the text input: not allowed HTML code, changing the line doesn't work, etc. From what I've learned, I need to use a markdown-markup language to solve this issue. Is there a guide for me to follow to apply such language to solve my problem?
UPDATE: Here are my problems.
1) Every sentence is combined into one line even if I put a line space.
first line
second line
becomes
first line second line
2) I can't make text bold, italicized, or hyperlink. Like in stackoverflow, user should easily put <b> and make bold text, ** to make italicized, etc. And URL address should automatically be translated to href link.
To do these, I thought I had to use markdown library. I could be mistaken, so I needed someone to guide me through. Railscasts on Markdown
Well, yes, new lines in HTML have no meaning. You need to replace line breaks with <br> to preserve them in HTML. To automatically highlight links, you need to look for links in the text and wrap them in appropriate <a> tags. Finally, if you're not filtering HTML tags, they should still be in there. It all depends on what you're doing. Markdown is something entirely different, a special markup language that enables you to do the above while being easier to write than HTML. It depends on what you want to use.

Why does the simple_format helper seem to ignore double new lines in ruby on rails?

I have a micropost feature and was testing the way it formats text that has been posted when displaying back to the user.
I pasted the following text like this:
and this was displayed back to me:
I'm using "simple_format h(content)". When I remove the helper the text is displayed with out a new line from the word "In". It displays as one big paragraph so I assume the helper is working but for some reason my double new lines are being ignored.
Any idea what is going on? Am I missing something?
By seeing it back, do you mean inside a textarea, or on the page? If it's on the page, all whitespace is compressed to one space each. If it's the latter, simply use the css rule:
white-space:pre;
On the proper selector.
However, if it is in a textarea (which preserves whitespace by default), there must be something stripping the extra space when you save it into the database. You might want to debug down your stack in the model & controller, to see where this might be happening. I have to admit i haven't used the the simple_format method.
Thanks to chrome developer tools as per usual. I realised that each text separated by 2 new lines were wrapped with p tags so I just added a bottom margin of 5px using css to p. Works perfectly.

Resources