impregnable fiend
impregnable fiend

Reputation: 289

How to write beforeSomeTests in Protractor?

Learning JS, wondering is it possible in Protractor to do something like

before Test1,3,5,7,9,11 etc(function(){
  browser.get('http://192.168.1.117:7777/#/route1');
});

and

before Test2,4,6,8,10 etc(function(){
  browser.get('http://192.168.1.117:7777/#/route2');
});

Or the only way to do before is

beforeEach(function(){
  browser.get('http://192.168.1.117:7777/#/route');
}); 

Don't wanna write browser.get every time, but can't use beforeEach cause it's 2 routes, not 1. Excuse me, if my question is ordinary, thanks in advance :)

Upvotes: 1

Views: 70

Answers (2)

Optimworks
Optimworks

Reputation: 2547

What i have understood from above question is that you have two different baseUrls and need to use one for a group of test cases and other for remaining test cases.

If I'm right, then answer is "You DON'T have such option". You can do either of following alternative solutions:

  1. Divide the test cases into Groups(say describe block here). The test cases which needs to run one first baseUrl, keep them all in one describe block

    describe('group of test cases which uses route1 url', function(){
        //you can use beforeEach and/or beforeAll
        beforeEach(function(){
            browser.get("http://baseUrl1/route1");   
        }) 
    
        beforeAll(function() {
            browser.get("http://baseUrl1/route1");   
        }) 
    
        it('test case1', function(){
    
        });
    
        it('test case2', function(){
    
        });
    
        it('test case3', function(){
    
        });
    
    });
    

    Then another suite, could be in same file:

    describe('group of test cases which uses route2 url',function(){
        //you can use beforeEach and/or beforeAll
        beforeEach(function(){
            browser.get("http://baseUrl2/route2");   
        }) 
    
        beforeAll(function() {
            browser.get("http://baseUrl2/route2");   
        })
    
        it('test case1', function(){
    
        });
    
        it('test case2', function(){
    
        });
    
        it('test case3', function(){
    
        });
    
    
    });
    
  2. Try with "Using Multiple Browsers in the Same Test" http://www.protractortest.org/#/browser-setup

Upvotes: 3

Segg3r
Segg3r

Reputation: 466

You should just probably organize your tests in specs accordingly:

describe('all tests', function() {
  describe('route 1 tests', function() {
    beforeEach(function() {
      browser.get('route1');
    });
    ...
  });

  describe('route 2 tests', function() {
    beforeEach(function() {
      browser.get('route2');
    });
    ...
  });
});

Upvotes: 2

Related Questions