3

I am writing protractor test suites, and need to test something that is uses pages generated from another suite. I can only figure out one way to write the test, but it doesn't seem like a good way to write it:

var someStuffToSave = [];

describe('description of first thing', function(){
    it('should generate some things', function(){
        someStuffToSave.push(generateSomeThings());
    });
    it('should generate some things', function(){
        someStuffToSave.push(generateSomeThings());
    });
    it('should generate some things', function(){
        someStuffToSave.push(generateSomeThings());
        element(by.id("something")).getText().then(function(){
          for(var i = 0; i < someStuffToSave.length; i++) (function(){
             var idx = i;
             describe('thing analysis' + idx, function(){
                it('should be something', function(){
                   expect(someStuffToSave[idx]).toEqual(true);
                });
             });
          })();
      });
    }); 
});

I would much rather do something like this, but the problem is the internals of the second describe execute right away, and do not wait for the first describe to finish.

var someStuffToSave = [];
describe('description of some thing', function(){
    it('should generate some things', function(){
        someStuffToSave.push(generateSomeThings());
    });
    it('should generate some things', function(){
        someStuffToSave.push(generateSomeThings());
    });
    it('should generate some things', function(){
        someStuffToSave.push(generateSomeThings);
    }); 
});
describe('analyzing things', function(){
    for(var i = 0; i < someStuffToSave.length; i++)function(){
        var idx = i;
        describe('thing ' + idx, function(){
            it('should be something', function(){
                expect(someStuffToSave[idx]).toEqual(true);
            });
        });
     }
});

is there some way to write this without the 'then' inside the it in the first describe?

Eric Wooley
  • 646
  • 4
  • 17
  • Have you seen [this Protractor issue](https://github.com/angular/protractor/issues/592)? – glepretre Mar 17 '14 at 13:49
  • I had not, but I tried nesting both of them in a describe. I will play around with this more a little later today. – Eric Wooley Mar 20 '14 at 16:28
  • Are you still trying to work on this? I think I may be able to help if I can better understand what you are trying to do. Could you provide a more concrete example? In your examples above, the initial describe block has three specs (it...) however none of them assert anything, rather they seem to be doing something. Normally you do things in a before block and then, in the it blocks, assert that what was done 'before' produced the expected output or side effects. Your example is too abstract for me to understand why you are not following that pattern. – Kenneth Baltrinic May 03 '14 at 02:55
  • The actual test is over a few thousand lines, so I tried to shorten it to an example. In the real program, in the initial it statements, there are plenty of assertions, and at the same time they are gathering information about the site for a later test to test. What I ended up doing was nesting the second test into one of the promises that resolved at the end of the first test. It's not very pretty IMO, but it works. – Eric Wooley May 14 '14 at 16:38

1 Answers1

1

In general, you should never do any setup login in Jasmine inside a 'describe' block. The code there is run while Jasmine is setting up your tests, not in the correct order. Your second method should work if you just put the for loop inside an 'it' block.

Jmr
  • 12,078
  • 4
  • 39
  • 33