Selenium不执行javascript

Selenium不执行javascript,javascript,jquery,ruby-on-rails,selenium,capybara,Javascript,Jquery,Ruby On Rails,Selenium,Capybara,我正在使用水豚和硒作为它的驱动程序。我试图单击一个元素,当单击它时,它将显示一个div,但是单击永远不会调用javascript来实现这一点 下面是我的代码 scenario 'currently used transport mode cannot be re-selected' do expect(page).to have_css("h2.summary") expect(find('h2.summary').text).to eq("Single event") expect(page

我正在使用水豚和硒作为它的驱动程序。我试图单击一个元素,当单击它时,它将显示一个div,但是单击永远不会调用javascript来实现这一点

下面是我的代码

scenario 'currently used transport mode cannot be re-selected' do

expect(page).to have_css("h2.summary")
expect(find('h2.summary').text).to eq("Single event")
expect(page).to have_content("Change journey")
page.click_link("Change journey")
expect(find('#travel-times-preview').visible?).to be_truthy # FAILS here because of previous step not working

end
错误消息

Capybara::ElementNotFound:找不到css“#旅行时间预览”

html

数据库清理器设置

config.before(:suite) do
    if config.use_transactional_fixtures?

      raise(<<-MSG)
        Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
        (or set it to false) to prevent uncommitted transactions being used in
        JavaScript-dependent specs.
        During testing, the Ruby app server that the JavaScript browser driver
        connects to uses a different database connection to the database connection
        used by the spec.

        This Ruby app server database connection would not be able to see data that
        has been setup by the spec's database connection inside an uncommitted
        transaction.
        Disabling the use_transactional_fixtures setting helps avoid uncommitted
        transactions in JavaScript-dependent specs, meaning that the Ruby app server
        database connection can see any data set up by the specs.
      MSG

    end
  end

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, type: :feature) do
    # :rack_test driver's Rack app under test shares database connection
    # with the specs, so we can use transaction strategy for speed.
    driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test

    if driver_shares_db_connection_with_specs
      DatabaseCleaner.strategy = :transaction
    else
      # Non-:rack_test driver is probably a driver for a JavaScript browser
      # with a Rack app under test that does *not* share a database
      # connection with the specs, so we must use truncation strategy.
      DatabaseCleaner.strategy = :truncation
    end
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
config.before(:suite)do
如果config.use\u事务性\u装置?

raise(假设您在打开的firefox窗口中查看了开发控制台,并且没有JS错误,那么您看到的行为有一些潜在的原因,这些原因都与
e.preventDefault()
无关(当您听到蹄子想马,而不是斑马时)

  • 您显示的链接没有
    数据预览目标
    属性,因此单击处理程序无法读取和附加到
    数据预览切换=
    ,因此无法切换任何内容。数据可能是由应用程序中的其他JS添加的,因此设置为属性,而不是HTML中的属性(或者您只是选择将该细节从显示的元素中删除)移动到#2

  • 您的
    .on
    JS代码在元素存在之前运行,因此没有附加。当它将所有JS连接到一个文件中时,这可能会在测试模式下显示,因为JS的加载速度比在开发模式下发出多个请求时快。这将显示为链接文本,因为单击处理程序没有在测试模式下运行如果是这种情况,则延迟附加侦听器,直到加载DOM

  • 这是您正在编写的第一个支持JS的测试,隐藏/显示的元素是从数据库记录生成的,并且您没有正确禁用事务测试和/或配置database_cleaner。这会导致您在测试中创建的对象在您的应用程序中实际不可见,因此您希望使用的元素页面实际上不在那里。您可以通过暂停测试并查看HTML来验证它是否在页面上

  • 根据您提供的错误,这不是问题的原因,只需添加以下内容以确保答案的完整性:
    单击链接
    单击链接并立即返回。这意味着测试将继续运行,而单击触发的操作将继续。这可能会导致代码中出现争用情况(如果你有
    Capybara.ignore_hidden_elements=false
    set-坏主意),你的代码在改变元素之前找到元素并检查其可见性。因此,你的最后一步应该写如下,因为它将等待/重试元素变为可见

    expect(page).to have_css('#travel-times-preview', visible: true)
    
  • 另外,通过使用Capybara提供的特性,您的测试代码可以得到改进和加速

    scenario 'currently used transport mode cannot be re-selected' do
    
      expect(page).to have_css("h2.summary", text: "Single event")  # do it in one query
      page.click_link("Change journey") # click_link will find the content anyway so no need to check for it before
      expect(page).to have_css('#travel-times-preview') # By default Capybara only finds visible elements so checking visibility is pointeless, if you've changed that default see #4
    
    end
    

    你确定你点击了正确的元素吗?如果没有
    HTML
    @Andersson,这段代码对我们来说毫无意义。我添加了HTML链接。请注意,如果不使用测试环境并手动测试,它就可以工作。e.preventDefault()似乎有错误不触发-单击该链接时会重新加载页面。您应该将此问题标记为“水豚”还有,因为这是你正在使用的-不是直接的seleniumHey Thomas谢谢你的详细回答,我想回复,因为罪犯还没有找到。我会回答你提出的每一点。1.你是对的,我省略了数据预览切换。2.整个函数包装在另一个函数中,该函数在“TurboLink:load”上调用事件.3.是的,这是我有史以来的第一次验收测试-我将编辑帖子并添加数据库清理器配置-但是使用命令
    save_and_open_page
    或在打开浏览器时使用selenium,我可以在那里看到记录。4.您提供的代码仍然没有起作用。@PetrosKyriakou您的数据库清理器config after块应在数据库\u cleaner docs-中所示的\u after后追加。这不会导致您当前的问题,但会使您的测试变得不稳定。在单击\u链接后暂停测试时,您确认firefox控制台中没有错误吗?您使用的是什么版本的TurboLink?此外,您没有显示scenario被标记为
    js:true
    -我假设它在树的上面(或者运行测试时firefox不会打开),嘿,我在后台使用
    Capybara.current\u driver=:selenium
    (:all)此规范的块-没有其他内容,它打开了firefox。是的,检查控制台中没有错误。我使用的是Rails 5,认为它的turbolinks 5(Rails 5附带的任何内容)。顺便说一句,与您提供的代码略有不同
    期望找到css“#旅行时间预览”,但没有匹配项。也找到“”,它与选择器匹配,但不是所有筛选器都匹配。
    感谢数据库清理器提示,但我们将检查它。是否可能是因为turbolinks事件?我认为它是e.preventDefault()的原因etc没有触发its,因为当我点击链接后,
    welcome
    flash消息消失时,页面会明显刷新。编辑:command
    rake assets:clobber
    完成了这个技巧,非常感谢您的帮助!
    expect(page).to have_css('#travel-times-preview', visible: true)
    
    scenario 'currently used transport mode cannot be re-selected' do
    
      expect(page).to have_css("h2.summary", text: "Single event")  # do it in one query
      page.click_link("Change journey") # click_link will find the content anyway so no need to check for it before
      expect(page).to have_css('#travel-times-preview') # By default Capybara only finds visible elements so checking visibility is pointeless, if you've changed that default see #4
    
    end