Rendering plain text from the server is not plain text in the browser

(Everyday Code – instead of keeping our knowledge in a let’s share it with the internet)

One thing that is good to know:

If you render plain text from a web framework like Rails you will not have plain text in the browser. You will still have HTML in the browser.

This bite us with one of the RSpec scenarios that we were running and I thought I should share with the team and the whole community.


The real life scenario is the following.

In the Rails controller we do

class DigestMessagesController < ApplicationController
    def show
        render plain: "Text"

What we would expect to see in the browser is a simple “Text” text. If fact this is exactly what is returned by the server – you can see in the “Response” tab.

you can see that the response from the server is just “Text”

But if you now Inspect the content of the page in the browser you will see that there is HTML inside.

 <pre style="word-wrap: break-word; white-space: pre-wrap;">Text</pre>

This HTML was not returned by the framework. This HTML was added by the browser. This is not good because an RSpec system scenario with Capybara will pass with rack_test driver, but will fail with a Selenium driver. rack_test driver will not add this HTML while all the major browsers will add it.

scenario 'for text' do
      visit "/digest_messages/#{}?preview=plain"
      expect(page.body.to_s).to include("Text")
      # This expect here will fail for Selenium and will pass for rack_test driver.
      expect(page.body.to_s).not_to include("html")

I hope this is helpful and could save you a couple of minutes debugging.