用于测试 ExtJS 4.1 应用程序的 CasperJS - Ext.getCmp() 始终 returns 未定义
CasperJS to test ExtJS 4.1 application - Ext.getCmp() always returns undefined
我正在使用 capserjs 来测试我的 ExtJS 4.1 应用程序。这就是我参考 ext.js 文件
的方式
casperjs test --include=ext-all.js testFile.js
如果我在 chrome 开发人员工具栏上打开控制台选项卡并键入 Ext.getCmp('id of component');
,我会取回该组件。
但是如果我在我的 casperjs 测试中做同样的事情,我总是得到未定义的。
我最初尝试使用 Ext.getCmp()
获取一个组合框,它返回 undefined
,之后我尝试使用 Ext.getCmp()
查找文本框和标签,但每次它都返回未定义。
我也试过使用组件查询,但还是没用。
我也查看了这个 link 寻求帮助,但我无法产生预期的结果。
.then(function(){
this.wait(5000, function(){
this.capture('c:\temp\cmb.png');
console.log('-----' + Ext);
var sqCombo = Ext.getCmp('country-ddl'); // returns the ComboBox components
sqCombo.setValue('UK'); // set the value
sqCombo.fireEvent('select'); // because setValue() doesn't trigger the event
})
})
用injectJs()
casper.start(baseUrl, function() {
this.echo("TITLE : ====> " + this.getTitle());
})
casper.then(function(){
var inject = this.page.injectJs('ext-all.js');
if(inject){
console.log('injected');
}else{
console.log('cant inject');
}
console.log('evaluating');
this.evaluate(function(){
console.log(Ext);
var v = Ext.getCmp('shadowUser').text;
this.echo('++++++++++++++++++++' + v);
});
})
在 运行 脚本之后,我在控制台上看到了这个:
TITLE : ====> Test App
injected
evaluating
Console: [object Object]
Error: TypeError: 'undefined' is not an object (evaluating 'Ext.getCmp('shadowUser').text')
Error: Error: Ext.Loader is not enabled, so dependencies cannot be resolved dynamically. Missing required classes: myApp.view.OptionsView, myApp.view.HelpView, myApp.view.SupportView, myApp.view.AdminView
PASS Title is correct
TESTS COMPLETED
没有--include=<script>
选项。它叫做 --includes=<scripts>
,但这对你没有帮助,因为它会将 extjs 注入到无法访问页面的外部上下文中。
您需要 inject Extjs 脚本:
casper.then(function(){
this.page.injectJs('ext-all.js');
...
});
如果页面中已经包含 Extjs(根据可用的 Ext 组件判断),那么您不需要注入任何东西。然后您应该能够从页面上下文中使用它:
.then(function(){
this.wait(5000, function(){
this.capture('c:\temp\cmb.png');
console.log('-----' + Ext);
this.evaluate(function(){
var sqCombo = Ext.getCmp('country-ddl'); // returns the ComboBox components
sqCombo.setValue('UK'); // set the value
sqCombo.fireEvent('select'); // because setValue() doesn't trigger the event
});
})
.wait(5000, function(){
this.capture('c:\temp\cmb2.png');
});
})
请记住,Ext
仅在可通过沙盒 evaluate()
函数访问的页面上下文中可用。
我正在使用 capserjs 来测试我的 ExtJS 4.1 应用程序。这就是我参考 ext.js 文件
的方式casperjs test --include=ext-all.js testFile.js
如果我在 chrome 开发人员工具栏上打开控制台选项卡并键入 Ext.getCmp('id of component');
,我会取回该组件。
但是如果我在我的 casperjs 测试中做同样的事情,我总是得到未定义的。
我最初尝试使用 Ext.getCmp()
获取一个组合框,它返回 undefined
,之后我尝试使用 Ext.getCmp()
查找文本框和标签,但每次它都返回未定义。
我也试过使用组件查询,但还是没用。
我也查看了这个 link 寻求帮助,但我无法产生预期的结果。
.then(function(){
this.wait(5000, function(){
this.capture('c:\temp\cmb.png');
console.log('-----' + Ext);
var sqCombo = Ext.getCmp('country-ddl'); // returns the ComboBox components
sqCombo.setValue('UK'); // set the value
sqCombo.fireEvent('select'); // because setValue() doesn't trigger the event
})
})
用injectJs()
casper.start(baseUrl, function() {
this.echo("TITLE : ====> " + this.getTitle());
})
casper.then(function(){
var inject = this.page.injectJs('ext-all.js');
if(inject){
console.log('injected');
}else{
console.log('cant inject');
}
console.log('evaluating');
this.evaluate(function(){
console.log(Ext);
var v = Ext.getCmp('shadowUser').text;
this.echo('++++++++++++++++++++' + v);
});
})
在 运行 脚本之后,我在控制台上看到了这个:
TITLE : ====> Test App
injected
evaluating
Console: [object Object]
Error: TypeError: 'undefined' is not an object (evaluating 'Ext.getCmp('shadowUser').text')
Error: Error: Ext.Loader is not enabled, so dependencies cannot be resolved dynamically. Missing required classes: myApp.view.OptionsView, myApp.view.HelpView, myApp.view.SupportView, myApp.view.AdminView
PASS Title is correct
TESTS COMPLETED
没有--include=<script>
选项。它叫做 --includes=<scripts>
,但这对你没有帮助,因为它会将 extjs 注入到无法访问页面的外部上下文中。
您需要 inject Extjs 脚本:
casper.then(function(){
this.page.injectJs('ext-all.js');
...
});
如果页面中已经包含 Extjs(根据可用的 Ext 组件判断),那么您不需要注入任何东西。然后您应该能够从页面上下文中使用它:
.then(function(){
this.wait(5000, function(){
this.capture('c:\temp\cmb.png');
console.log('-----' + Ext);
this.evaluate(function(){
var sqCombo = Ext.getCmp('country-ddl'); // returns the ComboBox components
sqCombo.setValue('UK'); // set the value
sqCombo.fireEvent('select'); // because setValue() doesn't trigger the event
});
})
.wait(5000, function(){
this.capture('c:\temp\cmb2.png');
});
})
请记住,Ext
仅在可通过沙盒 evaluate()
函数访问的页面上下文中可用。