问题
I'm trying to write a Javascript spec for my Rails 3.2 application using Teaspoon (the Jasmine version). I'm trying to write a spec that does something like this
describe("Fun", function() {
var page = require('webpage').create() //ERROR
it("should be so much fun", function() {
page.open('/pageToTest/')
expect($('#HereIsTheParty')).not.toBe( undefined );
});
});
However, require('webpage') doesn't run (Error: Module name "system" has not been loaded yet for context) even though the Requirejs gem has been installed and can be accessed from the Chrome console.
My question is, can I easily get require('webpage') to run using Rails or should I be using something else? Is it maybe easier to just use Capybara since so far I've been using
describe "Fun", :type => :feature do
it "should be so much fun" do
visit '/pageToTest/'
expect(page).to have_content 'Success'
end
end
without any problems. I would however prefer using pure Javascript since in this case it's more convenient. What do you guys think? Thanks!
回答1:
With Teaspoon the specs are not run within the context of phantomjs, they are loaded within the context of the browser, and so don't have access to phantomjs. Only the internal phantomjs driver (runner.js in the repo) has any concept of phantomjs, which loads an html page with your javascripts in it.
It sounds like you may be convoluting Teaspoon with phantomjs, which is not an accurate picture of what's actually happening. Teaspoon only uses phantomjs as a runner layer and treats it no differently than Selenium or Capybara Webdriver (both also supported). So the question is similar to asking how you would access Selenium from within your javascript specs.
来源:https://stackoverflow.com/questions/28968231/visit-pages-using-phantomjs-directly-when-writing-specs-with-teaspoon-jasmine