MaciekR
MaciekR

Reputation: 292

Rails + Minitest + Capybara: how to assert_select with span id

in my view i have:

<span class = "info">
    <span id="<%= account.id %>" data-account_id="<%= account.id %>" data-activated="<%= account.starts_at? %>">
        <%= t(".account_activated") %>
    </span>
</span>

What is the best way to test (system test with use of capybara) if there is "account has been activated" text displayed inside ?

I tried:

assert_selector "span##{acount.id}", text: "account has been activated"

but get Selenium::WebDriver::Error::InvalidSelectorError: invalid selector: An invalid or illegal selector was specified

Upvotes: 0

Views: 1442

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49910

In HTML5 the definitions were changed to allow ids to start with numbers, but CSS requires leading numbers to be escaped correctly (#\31 234 would match an id of '1234'). Because of this if you need to match an element by CSS with an id that may start with a number it's best to use the id option (supported by all Capybara selectors) and let Capybara merge it into the CSS

assert_selector "span", id: account.id, text: "account has been activated"

which will correctly escape and apply it to the CSS. If you just want to specify the id and not the element type(since the id should be unique on the page) you can use the :id selector type (rather than the default CSS selector type)

assert_selector :id, account.id, text: "account has been activated"

Upvotes: 1

MaciekR
MaciekR

Reputation: 292

I've solved this issue.

This is because ID's should not begin with numbers (see here)

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

View code should be changed as follows:

<span class = "info">
    <span id="**account_**<%= account.id %>" data-account_id="<%= account.id %>" data-activated="<%= account.starts_at? %>">
        <%= t(".account_activated") %>
    </span>
</span>

and

assert_selector "#account_#{account.id}", text: "account has been activated"

Upvotes: 0

Related Questions