Cucumber 中的 http 基本身份验证测试

发布于 2024-12-05 18:00:24 字数 526 浏览 1 评论 0原文

我正在尝试通过 http basic 测试登录,但不断收到错误,我已上传代码 这里

我有http_basic_authenticate_with name:“name”,password:“password” 在我的应用程序控制器中,

我的登录步骤定义是;

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

但它不起作用,我收到错误

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

有人可以告诉我如何让黄瓜/水豚登录吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-12-12 18:00:24

我让它使用两种不同方法之一;随意选择:

### 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

我的测试响应 browser_basic_authorize

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

我在搜索中的多个位置找到的两者。

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.

最终幸福 2024-12-12 18:00:24

使用 selenium Web 驱动程序并通过 page.driver.visit("https://username:[电子邮件受保护]")

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文