0

I try to refactor my code. I know that if I have several expectations they should be isolate in 'it'. I try to understand how I can write instead this:

describe('my scenario should make', function () {

  var config = browser.params;
  var url = config.listOfReferencesUrl,
      grid,
      numberField;

  it('test1', function () {

    browser.get(url);
    browser.executeScript("icms.go('WEB_INQ_PROC', 'InquiryList', null, 0)");

    grid = psGrid(by.css("table[class='n-grid']"));
    numberField = grid.getQuickFilter(1);
    numberField.click().sendKeys("Hello!");

    since('fail1').expect(numberField.getInputText()).toEqual("");
  });

  it('test2', function () {
    since('fail2').expect(numberField.getInputText()).toEqual("Hello!");
  });

});

Something like this:

    describe('my scenario should make', function () {

      var config = browser.params;
      var url = config.listOfReferencesUrl,
          grid,
          numberField;

 *********Make this part of code ONES before all tests in spec ****
    browser.get(url);
    browser.executeScript("icms.go('WEB_INQ_PROC', 'InquiryList', null, 0)");

    grid = psGrid(by.css("table[class='n-grid']"));
    numberField = grid.getQuickFilter(1);
    numberField.click().sendKeys("Hello!"); 
*******************************************************************   
      it('test1', function () {
        since('fail1').expect(numberField.getInputText()).toEqual("");
      });

      it('test2', function () {
        since('fail2').expect(numberField.getInputText()).toEqual("Hello!");
      });

    });

Maybe somebody have an idea how I can do this?

giri-sh
  • 6,934
  • 2
  • 25
  • 50

1 Answers1

2

To answer your question, if you want to run your code once before all tests then use beforeAll() function available in Jasmine 2. Here's a sample -

beforeAll(function(){
    //Write your code here that you need to run once before all specs
});

You can use beforeEach() function available in Jasmine to run it each time before a test spec. Here's a sample -

beforeEach(function(){
    //Write your code here that you need to run everytime before each spec
});

If you are facing issue in getting these functions to work, then update your plugins to latest version and then try running it. Also use the framework: 'jasmine2' in your conf.js file

Hope this helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • 2
    The purpose was to run it once before all test. Not before each test. – Pierre-Alexandre Moller Aug 27 '15 at 12:36
  • Thanks @Pierre-AlexandreMoller . Updated answer to include the proper code to the question. – giri-sh Aug 27 '15 at 12:39
  • Thank you, @Girish Sortur. I think it is solution. But when I try to use it I have the error: ReferenceError: beforeAll is not defined. My Jasmine version is 2.3.1. And I read that BeforeAll should be included in it... – Лилия Сапурина Aug 27 '15 at 12:50
  • @ЛилияСапурина , there must be an issue with your Jasmine installation. Have you assigned the `framework: 'jasmine2'` in conf.js file? If yes, then update your protractor or all plugins to latest version and then try. – giri-sh Aug 27 '15 at 13:00
  • @Girish Sortur I updated protractor, and I have this property https://github.com/angular/protractor/commit/694a755b95aca067665bd17fae69797d370546ab in source protractors code. But beforeAll still not working :( – Лилия Сапурина Aug 27 '15 at 13:14
  • When I write: framework: 'jasmine 2' all my tests always completes successful without log info. – Лилия Сапурина Aug 27 '15 at 14:36
  • @ЛилияСапурина So, your tests are running now? You would need a protractor reporter for logging your tests. Check `jasmine-spec-reporter` in npm. – giri-sh Aug 27 '15 at 14:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88161/discussion-between---and-girish-sortur). – Лилия Сапурина Aug 28 '15 at 07:27