How to inject a Javascript file to the page before the @javascript scenarios?

251 views
Skip to first unread message

planon

unread,
Nov 22, 2012, 5:30:19 PM11/22/12
to cu...@googlegroups.com
I have been trying to do something that's eluded my understanding so far, and that's why I am turning to this list for help. What I want to do is to inject a javascript file to a page loaded by a Cucumber step after it's been loaded by Cucumber. This file would include some stubs for an ajax call to an external service, so it has to be executed after all the scripts on the page have finished loading. I understand I can use `page.evaluate_script('4 + 4')`, but I am trying to figure out I could use that to evaluate the script in a Cucumber hook, and make sure it's not loaded before the scripts on that page are finished loading. 

Thanks!

Aslak Hellesøy

unread,
Nov 22, 2012, 5:41:26 PM11/22/12
to cu...@googlegroups.com

On Thursday, 22 November 2012 at 22:30, planon wrote:

I have been trying to do something that's eluded my understanding so far, and that's why I am turning to this list for help. What I want to do is to inject a javascript file to a page loaded by a Cucumber step after it's been loaded by Cucumber.
I think you are confusing Cucumber with Capybara, which has its own forum.

Aslak 
This file would include some stubs for an ajax call to an external service, so it has to be executed after all the scripts on the page have finished loading. I understand I can use `page.evaluate_script('4 + 4')`, but I am trying to figure out I could use that to evaluate the script in a Cucumber hook, and make sure it's not loaded before the scripts on that page are finished loading. 

Thanks!

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
 
 

Matt Wynne

unread,
Nov 22, 2012, 5:51:10 PM11/22/12
to cu...@googlegroups.com
On 22 Nov 2012, at 22:30, planon wrote:

I have been trying to do something that's eluded my understanding so far, and that's why I am turning to this list for help. What I want to do is to inject a javascript file to a page loaded by a Cucumber step after it's been loaded by Cucumber. This file would include some stubs for an ajax call to an external service, so it has to be executed after all the scripts on the page have finished loading. I understand I can use `page.evaluate_script('4 + 4')`, but I am trying to figure out I could use that to evaluate the script in a Cucumber hook, and make sure it's not loaded before the scripts on that page are finished loading. 

There isn't a cucumber hook that will run based on events for your page loading. You could override Capybara's visit method to create one for yourself:

module VisitWithAfterHook
  def visit(*args)
    super
    after_visit(*args)
  end
end

World(VisitWithAfterHook)

Ensuring that the script is evaluated after the DOM has loaded is another question. I'd ask about that on the capybara list.


Thanks!

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
 
 

planon

unread,
Nov 23, 2012, 12:48:06 PM11/23/12
to cu...@googlegroups.com
Sweet. Thanks!
Reply all
Reply to author
Forward
0 new messages