结合 Capybara 和 Minitest 语法
combining Capybara and Minitest syntax
我安装了 Capybara 和 Selenium。我对登录过程进行了三个测试:
- 使用 Minitest 进行测试
- 使用水豚
- 结合 Minitest 和 Capybara 语法。
测试 3 在 Capybara 断言中引发失败,"Not at Dashboard"。
是否可以在同一个测试中结合使用 Minitest 和 Capybara 的语法?如果是,我的测试 3 出了什么问题?
1) 最小测试:
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user_one = users(:dagobert)
end
test "login with minitest" do
# Go to login
get login_path
assert_template 'sessions/new'
# Login
log_in_as(@user_one)
assert is_logged_in?
# Assert redirect to dashboard
assert_redirected_to dashboard_url
assert_template 'dashboard/index'
end
end
2) 水豚:
require "capybara_test_helper"
require 'test_helper'
class UserLoginCapybaraTest < ActionDispatch::IntegrationTest
def setup
@user_one = users(:dagobert)
end
test "login with capybara" do
Capybara.current_driver = :selenium
# Go to login
visit "/login"
assert_equal "/login", current_path
assert page.has_content?("Log in"), "Not Log in"
# Login
fill_in('session_email', :with => @user_one.email)
fill_in('session_password', :with => 'password')
click_button "Log in"
# Assert redirect to dashboard
assert page.has_content?("Dashboard")
end
end
3) Minitest 和 Capybara 组合,失败:
require "capybara_test_helper"
require 'test_helper'
class UserLoginCapybaraTest < ActionDispatch::IntegrationTest
def setup
@user_one = users(:dagobert)
end
test "login with minitest & capybara" do
# Go to login
get login_path
assert_template 'sessions/new'
# Login
log_in_as(@user_one)
assert is_logged_in?
# Assert redirect to dashboard
assert_redirected_to dashboard_url
assert_template 'dashboard/index'
# Check content with Capybara
assert page.has_content?("Dashboard"), "Not at Dashboard"
end
end
您不能在一个测试中结合使用#get 和#page 并期望事情正常工作,因为它们都有自己的请求和页面内容。
Capybara(当使用支持 JS 的驱动程序时)尝试通过 运行 浏览器模拟用户并控制它向应用程序发出请求。 Capybara 然后在浏览器中查询文档。您(在大多数水豚驱动程序中)无法访问响应代码、呈现的模板等内容,因为用户通常不会看到它们,而水豚的目的(再次)是模拟用户。
#get,另一方面,通过直接调用应用程序来获取快捷方式。您可以使用 Capybara provided matchers/asserts 通过使用 Capybara.string.
对通过 #get 获得的响应内容
"fast tracking" 登录在很大程度上取决于您用于身份验证的内容,可能应该作为一个单独的问题询问,并提供有关您的应用程序的更多详细信息
我安装了 Capybara 和 Selenium。我对登录过程进行了三个测试:
- 使用 Minitest 进行测试
- 使用水豚
- 结合 Minitest 和 Capybara 语法。
测试 3 在 Capybara 断言中引发失败,"Not at Dashboard"。
是否可以在同一个测试中结合使用 Minitest 和 Capybara 的语法?如果是,我的测试 3 出了什么问题?
1) 最小测试:
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user_one = users(:dagobert)
end
test "login with minitest" do
# Go to login
get login_path
assert_template 'sessions/new'
# Login
log_in_as(@user_one)
assert is_logged_in?
# Assert redirect to dashboard
assert_redirected_to dashboard_url
assert_template 'dashboard/index'
end
end
2) 水豚:
require "capybara_test_helper"
require 'test_helper'
class UserLoginCapybaraTest < ActionDispatch::IntegrationTest
def setup
@user_one = users(:dagobert)
end
test "login with capybara" do
Capybara.current_driver = :selenium
# Go to login
visit "/login"
assert_equal "/login", current_path
assert page.has_content?("Log in"), "Not Log in"
# Login
fill_in('session_email', :with => @user_one.email)
fill_in('session_password', :with => 'password')
click_button "Log in"
# Assert redirect to dashboard
assert page.has_content?("Dashboard")
end
end
3) Minitest 和 Capybara 组合,失败:
require "capybara_test_helper"
require 'test_helper'
class UserLoginCapybaraTest < ActionDispatch::IntegrationTest
def setup
@user_one = users(:dagobert)
end
test "login with minitest & capybara" do
# Go to login
get login_path
assert_template 'sessions/new'
# Login
log_in_as(@user_one)
assert is_logged_in?
# Assert redirect to dashboard
assert_redirected_to dashboard_url
assert_template 'dashboard/index'
# Check content with Capybara
assert page.has_content?("Dashboard"), "Not at Dashboard"
end
end
您不能在一个测试中结合使用#get 和#page 并期望事情正常工作,因为它们都有自己的请求和页面内容。
Capybara(当使用支持 JS 的驱动程序时)尝试通过 运行 浏览器模拟用户并控制它向应用程序发出请求。 Capybara 然后在浏览器中查询文档。您(在大多数水豚驱动程序中)无法访问响应代码、呈现的模板等内容,因为用户通常不会看到它们,而水豚的目的(再次)是模拟用户。
#get,另一方面,通过直接调用应用程序来获取快捷方式。您可以使用 Capybara provided matchers/asserts 通过使用 Capybara.string.
对通过 #get 获得的响应内容"fast tracking" 登录在很大程度上取决于您用于身份验证的内容,可能应该作为一个单独的问题询问,并提供有关您的应用程序的更多详细信息