问题描述
我正在尝试使用Teaspoon(Jasmine版本)为Rails 3.2应用程序编写Javascript规范.我正在尝试写一个类似这样的规范
describe("Fun", function() {
var page = require('webpage').create() //ERROR
it("should be so much fun", function() {
page.open('/pageToTest/')
expect($('#HereIsTheParty')).not.toBe( undefined );
});
});
但是,即使已安装并可以访问Requirejs gem,require('webpage')也无法运行(错误:尚未为上下文加载模块名称"system" )从Chrome控制台.
我的问题是,我可以轻松地使 require('webpage')使用Rails运行还是应该使用其他东西?到目前为止,我一直在使用
describe "Fun", :type => :feature do
it "should be so much fun" do
visit '/pageToTest/'
expect(page).to have_content 'Success'
end
end
没有任何问题.但是,我更喜欢使用纯Javascript,因为在这种情况下它更方便.你们有什么感想?谢谢!
使用Teaspoon时,规范不会在phantomjs的上下文中运行,而是在浏览器的上下文中加载,因此无法访问phantomjs.只有内部phantomjs驱动程序(在存储库中为runner.js)才具有phantomjs的任何概念,它会在其中加载带有JavaScript的html页面.
听起来您可能正在用phantomjs来缠绕Teaspoon,但这并不是实际发生情况的准确描述. Teaspoon仅将phantomjs用作运行层,并且对待它与Selenium或Capybara Webdriver(也都支持)没有区别.因此,问题类似于询问您如何从JavaScript规范中访问Selenium.
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!
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.
这篇关于使用TeaSpoon编写规范时,直接使用PhantomJS访问页面-Jasmine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!