How can I include javascript with javascript_include_tag at the top of the page for a capybara test

一个人想着一个人 提交于 2019-12-23 23:16:26

问题


I am trying to test one javascript class in a spec test with capybara. I have static page player.haml in which I am loading the javascript class definition with javascript_include_tag. It looks like that

  .container
    = javascript_include_tag "../player"
    #player

My test looks like that

  it 'can autoplay a video' do
    visit player_path
    video = Fabricate(:video)
    options = { autoplay: true, ... }
    page.execute_script("var options = '#{options}';
    videoId = '#{video.id}'; var player = new Player(videoId, options);")
    expect(page).to have_content(video.title)
  end

The problem is that the javascript_include_tag is including the script at the bottom of the page and after the execute_script is invoke. I tried with sleep after visiting the page, but the result was the same. How can I load the class definition before to use execute_script? Eventual bottleneck is the fact that the file with player definition is .coffee.erb. I can changed because it uses some settings from the application.

来源:https://stackoverflow.com/questions/36683421/how-can-i-include-javascript-with-javascript-include-tag-at-the-top-of-the-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!