user1735921
user1735921

Reputation: 1389

Testing ruby with rails, Element not found

I get the error:

Capybara::ElementNotFound:
       Unable to find field "user_email"

And this is the test code:

feature 'User' do
    given!(:user) { User.new(email: '[email protected]', encrypted_password: 'test') }
    scenario 'opens sign_up page' do
        visit new_user_session_path
        expect(page).to have_content 'unique text on the page'
    end
    scenario 'signs in with invalid email' do
        visit new_user_session_path
            fill_in('user_email',with: 'ssd')
        expect(page).to have_content 'unique text on the page'
    end
end

My HTML file consists of this code literally:

unique text on the page
<br>
<input type="text" id="user_email">

So this proves that the path is correct because my first scenario runs correctly. It is visiting the right page. But still I get this error for second scenario in fill_in. I have also tried element = page.find("user_email"), it gives same error. What am I possibly doing wrong? I have been scratching my head like hell.

Upvotes: 0

Views: 246

Answers (2)

Thomas Walpole
Thomas Walpole

Reputation: 49890

Usually the reason for this is that the input isn't actually visible on the page. You can verify this by doing

fill_in('user_email', with: 'ssd', visible: false)

If that succeeds in finding the element, then you need to change your test to first perform whatever actions make the field visible before attempting to fill it in.

Upvotes: 1

Rokibul Hasan
Rokibul Hasan

Reputation: 4156

Your code seems right. Maybe you are visiting wrong url or you have used user_email id once more. But you can give a try with alternative syntax like following :

find("input[id$='user_email']").set "ssd"

Upvotes: 0

Related Questions