简体   繁体   中英

Using Factory Girl with Cucumber and Capybara, how do I populate forms?

I have this:

Scenario: Login
  Given a user exists with first_name: "Fred"
  When I am on the home page
  And I fill in "email" with the user: "Fred"
  And I fill in "password" with the user: "Fred"
  And I submit the form
  Then I should see "Welcome"

The problem is how do I get the email field populated with the Factory Girl-generated user's email? Email must be unique so it's defined as a sequence:

sequence(:email) {|n| "joeschmoe#{n}@example.com"}

I'm also using pickle but there's no step for fill in . How can I create the user via factory and fill out the login form with the same user's info?

You need to add you own step for it.

When /^(?:|I )fill in "([^"]*)" with the #{capture_model}(?: within "([^"]*)")?$/ do |field, a, selector|

  with_scope(selector) do
    fill_in(field, :with => model!(a).send(field))
  end
end

I changed it to this:

Scenario: Login
  Given a user exists with email: "fred@example.com", plain_password: "password", plain_password_confirmation: "password"
  And I am on the home page
  When I login with "fred@example.com", "password"
  And I submit the form
  Then I should see "Welcome"

With the following step definitions:

When(/^I login with "(\S+)", "(.*)"$/) do |email, password|
  fill_in 'username', :with => email
  fill_in 'plain_password', :with => password
end

# Can only be executed with Selenium or Envjs drivers
When /^I submit the form$/ do
  page.evaluate_script("document.forms[0].submit()")
end

And it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM