0

I'm working on UI testing an ExtJS web-app, and I'm a beginner. I am trying to test the ExtJS widgets by using CasperJS/PhantomJS tool. Also, I generate the required CasperJs script using Resurrectio and by making necessary changes to it.

Since ExtJs generates unique ids dynamically for the DOM elements that it creates, I want to know how to provide those ids in CasperJs script for testing.

For example, The following Casper Script was generated by Resurrectio:

   casper.waitForSelector("#ext-gen1142 .x-tree-icon.x-tree-icon-parent",
       function success() {
           test.assertExists("#ext-gen1142 .x-tree-icon.x-tree-icon-parent");
           this.click("#ext-gen1142 .x-tree-icon.x-tree-icon-parent");
       },
       function fail() {
           test.assertExists("#ext-gen1142 .x-tree-icon.x-tree-icon-parent");
   });
   casper.waitForSelector("#gridview-1038",
       function success() {
           test.assertExists("#gridview-1038");
           this.click("#gridview-1038");
       },
       function fail() {
           test.assertExists("#gridview-1038");
   });

Here #ext-gen1142 and #gridview-1038 are the ids dynamically created. How should one provide data in the tests? Is there any stub or mocking tools which works with ExtJs in the code to provide these ids at runtime during tests?

I came across SinonJS. Can it be used or Do I need to used CSS or XPath Locators as mentioned in this answer? How reliable it is to use CSS or Xpath Locators?

Thanks in advance!

Community
  • 1
  • 1
ProgrammingPanda
  • 143
  • 2
  • 10

2 Answers2

6

Not so easy to answer this, but here a few thoughts...

  1. Don't rely on generated IDs. Never. They'll change in moments you won't like and if you have luck very much earlier.

  2. Your best friends will probably be pseudo CSS classes you attach to your components. You could also use IDs, but this is only reasonable when you have elements which occur only once in your page. If that is the case, they are very good anchors to start with selections/queries.

  3. XPath with ExtJS is possible, but you have to carefully choose the elements. ExtJS is so verbose in generating little things so your paths can be quite complicated. And when Sencha drops support for problematic browsers (IE < 8) maybe they change their templates and your XPath doesn't find anything.

  4. SinonJS is great. But it won't help you much in DOM problems. But sure you can use it in your tests. I suppose it will payoff most in testing parts of your controllers or non-trivial models.

  5. Model your test components after your real UI components and screen sections. Don't just record a script. Test code should be engineered like production code. If you create reusable components of test code and logic, you don't have to fear changes. In the best case the changes in one component will only touch the testing code of that particular component.

  6. I know you have ExtJS. But take some time to look at AngularJS and see how easy it can be to test all parts of a JavaScript web application. I'm not saying you should switch to AngularJS, but you can learn a lot. Have a look at Deft JS as it has many concepts which enhance testability of ExtJS applications.

hgoebl
  • 11,224
  • 7
  • 39
  • 67
1

I use Siesta for my ExtJs testing. It works amazingly good for all JavaScript (jQuery based and others), but is specifically designed for ExtJS/Sencha Touch.

It has the feature to combine CSSquery and ComponentQuery to select your elements I think that will fix a lot of problems for you.

In the paid version there is even a test recorder to record scenario's and use them for your tests.

Here's a demo

Here's some sample code:

StartTest(function(t) {
    t.chain(
        { waitFor : 'CQ', args : 'gridpanel' },

        function(next, grids) {
            var userGrid = grids[0];

            t.willFireNTimes(userGrid.store, 'write', 1);
            next();
        },

        { waitFor : 'rowsVisible', args : 'gridpanel' },

        { action : 'doubleclick', target : 'gridpanel => .x-grid-cell' },

        // waiting for popup window to appear
        { waitFor : 'CQ', args : 'useredit' },

        // When using target, >> specifies a Component Query
        { action : 'click', target : '>>field[name=firstname]'},

        function(next) {
            // Manually clear text field
            t.cq1('field[name=firstname]').setValue();
            next();
        },

        { action : 'type', target : '>>field[name=firstname]', text : 'foo' },

        { action : 'click', target : '>>useredit button[text=Save]'},

        function(next) {
            t.matchGridCellContent(t.cq1('gridpanel'), 0, 0, 'foo Spencer', 'Updated name found in grid');
        }
    );
})    
VDP
  • 6,128
  • 4
  • 27
  • 52
  • Thanks! Yes, I do know about Siesta. I just wish there was an opensource tool though. – ProgrammingPanda Mar 13 '14 at 23:29
  • 1
    It's free for use if you don't mind running it in the browser. But if you want real automation and some extra features, you have to pay for it indeed... although it runs via node too, so you probably get it automated using node. But I have no experience with that... for larger projects it's really worth it... – VDP Mar 14 '14 at 08:30