Capybara无法点击按钮

时间:2015-12-11 16:07:13

标签: javascript unit-testing capybara

我是水豚和恶作剧的新手,但这是我迄今为止所拥有的。

找到按钮但无法点击它。我试过了'execute_script'但由于NotSupportedByDriverError,我无法让它工作。我已经收录了gem' capybara-webkit'但这并没有改变任何事情。

有问题的一行:

assert page.execute_script("$('.js-login-action').click()"), "Failed to click on Login"

这里是完整的test.rb文件

require 'test_helper'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

options = {js_errors: false, js: true}
Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, options)
end

 class LoginTest < ActionDispatch::IntegrationTest

  test "Load login page" do
    visit("/users/sign_in")
    assert page.has_content?('derp'), "Page does not have Derp heading"
  end

  test "Successful load of sign in page" do
    get("/users/sign_in")
    assert_response :success
  end

  test "Login" do
    visit("/users/sign_in")
    fill_in "login-username", with: 'derp'
    fill_in "login-password", with: '0123456789'
    assert find_button("Login"), "Failed to Find Login Button"
    assert page.execute_script("$('.js-login-action').click()"), "Failed to click on Login"
    assert page.has_css?('en-main-header'), "Failed to Login"

  end

end

2 个答案:

答案 0 :(得分:1)

page.execute_script并不意味着点击按钮。

正如您在这个非常有用的capybara cheatsheet中所看到的,您可以通过不同方式单击按钮。例如:

click_button('Save')

find_button('Send').click

find('//table/tr').click

答案 1 :(得分:0)

这里有一些问题:

  1. 您正在设置您的javascript驱动程序,但之后没有告诉您的测试使用javascript驱动程序,因此他们使用的是默认驱动程序,即机架测试,并且不支持javascript all(因此不受驱动程序错误支持)如果您希望所有测试都使用JS支持运行,您可以将Capybara.default_driver设置为:poltergeist,或者查看通过元数据设置驱动程序https://github.com/wojtekmach/minitest-capybara

  2. 正如@fabersky所指出的那样,你应该使用click_button或Node#click点击页面中的项目所以 - Private Sub Worksheet_Change(ByVal Target As Range) On Error Resume Next If Not Intersect(Target, Range("L9:L500")) Is Nothing Then Range("L9").Sort Key1:=Range("L10"), _ Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom End If End Sub - 这样就不需要你在find_button上断言了,因为click_button会出错,如果它无法找到按钮。

  3. 断言find_button并不能很好地阅读,你最好使用像click_button('Login')这样的东西,它会在失败时提供更具描述性的错误信息。你的断言has_css也一样吗?可能是page.assert_selector :button, 'Login' - 如果您的默认选择器设置为:css,则css是可选的 - 这将提供有关查找/丢失内容的更具描述性的错误

  4. 断言execute_script的结果是行不通的,因为根据定义,execute_script不会返回任何内容。如果你真的想检查你想要使用evaluate_script的页面上执行的某些JS的结果