How can I include javascript with javascript_inclu

2019-07-08 17:11发布

问题:

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.