Integration Testing HTML Special Characters

2019-08-31 12:37发布

问题:

Kind of a strange one, but in my views I have a tick (✔) and a cross (×) used as links (in lieu of images). Is there any way of finding these elements and testing them using RSpec and Capybara-webkit, or should I try and target say the title attribute instead and ignore this route?

My test in question looks like this:

context "casting a vote", js: true do
  before do
    sign_in user
    click_link '✔'
    sleep 0.2
  end

  it { should have_content("Vote cast!") }
end

The failure message I get is (predictably):

Failure/Error: click_link "raw('✔')"
Capybara::ElementNotFound:
Unable to find link "raw('✔')"

Thanks in advance for your help.

回答1:

Capybara doesn't see the HTML, it runs thru the DOM, which then sees the actual values those things encode. You must send the raw code as a UTF-{8,16} string containing the code point itself.

Most languages would present an HTML '✔' as "\u10004", so try that.