1

I am using capserjs to test my ExtJS 4.1 application. This is how I have made reference to ext.js file

casperjs test --include=ext-all.js testFile.js

If I open console tab on chrome developer toolbar & type Ext.getCmp('id of component'); I get the component back.

But If I do the same thing with in my casperjs test, I always gets undefined.

I was initially trying to get a combobox using Ext.getCmp() which returned undefined, after that I tried finding textbox, labels using Ext.getCmp() and every-time it returned undefined.

I have also tried to use Component query and even that did not worked.

I also looked at this link for help but I am unable to produce desired result.

.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
     })

   })

with 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);
     });
  })

After running the script, I see this on console:

   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
Community
  • 1
  • 1
SharpCoder
  • 15,708
  • 34
  • 126
  • 225
  • @ArtjomB.: Updated the question to show what I am trying to do. I added selenium tag because I am sure lot of people must have faced the same issue. – SharpCoder Dec 17 '15 at 16:46
  • I am basically trying to find a combo-box and want to set some value and then fire the select event. Since ext combo-box is not rendered as `select`, I am struggling to find the component. I have included included extjs using `--include` (as shown in my question) option while running the test case. After this I was able to access `Ext` object and I was thinking, I would be able to access all the underlying methods. I wonder what options do I have now to access the combobox? – SharpCoder Dec 17 '15 at 16:57

1 Answers1

1

There is no such thing as --include=<script> option. It's called --includes=<scripts>, but this doesn't help you, because it would inject extjs into the outer context which doesn't has access to the page.

You would need to inject the Extjs script when you need it:

casper.then(function(){
    this.page.injectJs('ext-all.js');
    ...
});

If Extjs is already included in the page (judging by available Ext components) then you don't need to inject anything. You should then be able to use it from the page context:

.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');
    });
})

Keep in mind that Ext is only available in the page context which can be accessed through the sandboxed evaluate() function.

Artjom B.
  • 58,311
  • 24
  • 111
  • 196
  • Thank you for taking time out and adding the code. Now I understand why `--include` option will not help in my case. I used the code you showed and it looks like, system is not executing `this.evaluate()`. under this function, I have included console.log() and I do not see anything on the console. Am I missing anything ? – SharpCoder Dec 17 '15 at 17:39
  • If you're using PhantomJS 2.0.0, then there is a bug which hides errors. Use PhantomJS 1.9.8 or 1.9.7 to see if there are errors. Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-2_caspererrors-js)). Maybe there are errors. – Artjom B. Dec 17 '15 at 17:41
  • I am now using phantomjs version 1.9.8. I have registered hooks for all the errors. I have also updated the question to include all the details about errors I am getting after including the suggested changes !! – SharpCoder Dec 17 '15 at 18:45
  • Of course, it can't find `Ext`, because it is injected in the page context. Use `evaluate()` like I showed it in my answer. – Artjom B. Dec 17 '15 at 18:49
  • My apologies for posting wrong code. I have updated the question again !! – SharpCoder Dec 17 '15 at 19:21
  • No idea. Does the screenshot show the element that you're interested in? If not, then you should `waitFor()` it. Btw, if it's an Extjs application, then you really don't need to inject Ext again into the page. – Artjom B. Dec 17 '15 at 19:23
  • Firstly, Thank you for spending so much time and energy to answer. Yes, the application is extjs application, but if I directly try to access Ext object, the test case always throws exception saying `Ext` is `undefined`. If I explicitly make reference to extjs using `injectJs()` Ext.loader is throwing error. Ext loader automatically loads all the dependency & in my application I am setting its value to true. The control I am trying to access is available. I can confirm that as i have taken screenshot. If I try to take screenshot inside `evaluate()` nothing happens. – SharpCoder Dec 17 '15 at 19:37
  • You can't take screenshots inside of the page context. `casper` is not available there. – Artjom B. Dec 17 '15 at 19:38