Question

In Capybara + Webkit + Ruby 1.9.3, I have the following HTML

simple_form generates the following when there's an error on a specific field. I'm having trouble "getting at" the error-text span and knowing I definitely have the one associated with "Title" rather than another field that may also have an error.

  <div class="input-wrapper string required error">
    <div class="input-label">
      <label class="string required" for="event_title">
        <abbr title="required">*</abbr>
        Title
      </label>
      <span class="error-text">can't be blank</span>
      <input id="event_title" class="string required" type="text" value="" size="50" name="event[title]">
  </div>

(plus many other fields, of course!)

I want to test that Title is, in fact required and when the user leaves blank and clicks the submit button, the error message "can't be blank" exists. Short of tweaking simple_form to wrap around the whole thing, is there a way to simply find the label and then go to next element in the DOM to see if its a span with "can't be blank" content?

Was it helpful?

Solution

I'm not sure you really need to do that. The test case should such that there is only one error message on the page. You can have other tests that check for multiple error messages but there should be one specific to this scenario. Doing that then a test that checks to see if the span.error-text is on the page and that it contains the text can't be blank should be sufficient.

page.should have_selector('span.error-text', text: "can't be blank")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top