在我正在开发的验收测试套件中(以堆栈顶部的mocha,sinon和chai为特色),我能够在phantomjs中加载页面并执行各种操作根据网络上的例子。
唯一让我失望的是检索页面启动过程中产生的JS变量。我在这里附上一个完整的示例,说明变量app
如何无法测试,而jQuery
可以。
两者之间的唯一区别是app
$(document).ready(function() {... create var app ...})
我得到的错误是maximum call stack exceeded
(?!?!?!?)
如果可用的话,我该怎么办才能检查应用?也许使用promises的东西?我无法清楚地看到这一切(**)。
这是错误堆栈:
19:29:02.918 INFO [14] org.openqa.selenium.remote.server.DriverServlet - Executing: [execute script: return app, []] at URL: /session/73437950-3c14-4392-81ed-c6bd83c3f3fb/execute)
19:29:06.481 WARN [14] org.openqa.selenium.remote.server.DriverServlet - Exception thrown
org.openqa.selenium.WebDriverException: {"errorMessage":"Maximum call stack size exceeded.","request":{"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"33","Content-Type":"application/json; charset=utf-8","Host":"localhost:1693"},"httpVersion":"1.1","method":"POST","post":"{\"args\":[],\"script\":\"return app\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/9ca480b0-3c34-11e4-b1c7-0d43d6ab90ff/execute"}}
Command duration or timeout: 3.56 seconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'vagrant-xxx-yyyy', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-60-generic', java.version: '1.7.0_65'
Session ID: 9ca480b0-3c34-11e4-b1c7-0d43d6ab90ff
Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver
Capabilities [{platform=LINUX, acceptSslCerts=false, javascriptEnabled=true, browserName=phantomjs, rotatable=false, driverVersion=1.1.0, locationContextEnabled=false, version=1.9.7, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=false, browserConnectionEnabled=false, webStorageEnabled=false, nativeEvents=true, proxy={proxyType=direct}, applicationCacheEnabled=false, driverName=ghostdriver, takesScreenshot=true}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
以下是测试:getTitle
,getTagName
,getElementSize
和execute('return jQuery')
成功; execute('return app')
失败。
describe('AAA Test acceptance XXXXX', function(){
var client = {}
before(function(done) {
client = webdriverjs.remote({
desiredCapabilities: {
browserName: 'phantomjs'
},
});
client.init(done) // starts session and opens the browser
});
it('The merchant button',function(done){
client.url('http://www.dev.xxxx.com/api/purchase/index.html')
.getTitle(function(err,title){
expect(err).to.be.null
expect(title).to.have.string('Mobile Payment');
})
.getTagName('.merchant-div',function(err,tagName){
expect(err).to.be.null
expect(tagName).to.be.equal('div')
})
.getElementSize('#ms-input',function(err,size){
expect(err).to.be.null
expect(size.width).to.be.equal(184)
})
.execute('return jQuery', function(err,jquery) {
expect(err).to.be.null
expect(jquery).not.to.be.undefined
expect(jquery).not.to.be.null
})
.execute('return app', function(err,appInstance) {
expect(err).to.be.null
expect(appInstance).not.to.be.undefined
expect(appInstance).not.to.be.null
})
.call(done);
})
after(function(done) {
client.end(done); // ends session and closes the browser
})
});
(**)我已经按照Selenium WebDriver JS - Explicit Wait中的说明操作,但错误是Object #<WebdriverIO> has no method 'wait'
(这很有道理......)