3
votes

I'm trying to test logging in via http basic but am continually getting errors, I've uploaded the code here

I have http_basic_authenticate_with name: "name", password: "password" in my application controller

My step definition for logging in is;

Given /^I login as admin$/ do
  authorize "name", "password"
end

but it doesn't work and I get the error

    expected there to be content "Posts" in "HTTP Basic: Access denied.\n"
 (RSpec::Expectations::ExpectationNotMetError)

can someone tell me how to get cucumber/capybara to log in?

2

2 Answers

0
votes

I got it to work with one of two different approaches; take your pick:

### Following works ONLY if performed first before even going to a page!!!
if page.driver.respond_to?(:basic_auth)
  puts 'Responds to basic_auth'
  page.driver.basic_auth(username, password)
elsif page.driver.respond_to?(:basic_authorize)
  puts 'Responds to basic_authorize'
  page.driver.basic_authorize(username, password)
elsif page.driver.respond_to?(:browser) && page.driver.browser.respond_to?(:basic_authorize)
  puts 'Responds to browser_basic_authorize'
  page.driver.browser.basic_authorize(username, password)
else
  raise "I don't know how to log in!"
end

My tests responded to browser_basic_authorize

or

encoded_login = ["#{username}:#{password}"].pack("m*")
page.driver.header 'Authorization', "Basic #{encoded_login}"

both of which I found at several places in my search.

0
votes

Use the selenium web driver and visit the website with page.driver.visit("https://username:[email protected]")