paul
paul

Reputation: 4487

Rspec, capybara getting started issues

I have have started with simple example of Rspec,Capybara. I have come across few issues. This issues are probably because I have experience with cucumber and page_object gem, but here I am using capybara and Site_prism gem.

I have tried: my_example_spec.rb require_relative 'Support/spec_helper' require_relative 'pages/login_page'

describe 'My behaviour'  do

  it 'should do something'do
    @login_page = LoginPage.new
    @login_page.load
    @login_page.login('autouser','password')
  end
end

and login_page.rb

class LoginPage < SitePrism::Page

 set_url "/login"

  element :username, "input[id='username']"
  element :password, "input[id='password']"
  element :submit, "input[id='submit']"

  def login(username,password)
   @login_page.username.set username
   @login_page.password.set password
   @login_page.submit.click

  end

end 

Issues are:

  1. When I run my_example_spec.rb it gives error

    Testing started at ...
    Run options: include {:full_description=>/My\ behaviour\ should\ do\ something/}
    
    NoMethodError: undefined method `username' for nil:NilClass
    ./pages/login_page.rb:10:in `login'
    ./my_example_spec.rb:11:in `block (2 levels) in <top (required)>'
    -e:1:in `load'
    -e:1:in `<main>'
    
  2. Shouldn't it be on(LoginPage).login (autouser, password). It should navigate to the page and run login method. It is how it works in page_object gem whats the equivalent of site_prism gem

Upvotes: 0

Views: 120

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49910

The login method in your LoginPage class should be

def login(username,password)
  username.set username
  password.set password
  submit.click
end

@login_page is not an instance variable of the LoginPage class so it is not accessible inside the class. It's also not necessary inside the class since you're already inside the class.

Upvotes: 1

Related Questions