NicholasJay
NicholasJay

Reputation: 37

Acceptance Test (Capybara) can't find my Submit Button

Question:

How can I get my feature test to recognize my button?

Explanation:

I'm writing an acceptance test with Capybara for "User can add a product to a shopping list". In my setup I log in as a user, I am able to click on a current shopping list and be directed to the page with the search input for a new product.

I then ask to click the button with click_button "Search"

Search is the name for my button and is what appears on the page. When I run the actual program everything works, but when I am running the test it keeps telling me that it can't find the Search button.

What I've Tried:

I've tried putting a value instead of a name on my submit button.
I've also tried passing in the id of the button
Instead of clicking on the button I tried adding \n to the input field to hit enter

My Code:

The form:

<form method="GET" action="products/show" />
    <label for="search_input">Search Products:</label>
    <input type="text" name="search_input" id="search_input_field" />
    <input type="submit" value="Search" id="submit_search"/>
</form>  

The Testing Code:

require 'spec_helper'

describe "a user can add a product to their shopping list" do 
    let!(:user_one) { FactoryGirl.create(:user) }
    let!(:households) do 
        user_one.shopping_lists.create!({
            title: "households",
            date: Date.today
        })
    end

    it "will add product to the list" do 
        login(user_one)
        click_link "View Shopping Lists"
        click_link "households"
        fill_in "search_input_field", with: "toothpaste"
        # save_and_open_page
        click_button "Search"
        # expect(page).to     
        # first(:button, "Add Item").click

        # expect(page).to have_button "Delete"
    end

    def login(user)
        visit root_path
        fill_in :email, with: user.email
        fill_in :password, with: user.password
        click_button "Login!"
    end

end

Upvotes: 0

Views: 555

Answers (1)

P.Downing
P.Downing

Reputation: 23

I believe the main issue is that Capybara is looking for an html tag rather than the input as a button.

In your test for the button portion try using

click_on 'Login'

Upvotes: 0

Related Questions