3
votes

Is it possible to run some tests or a logical flow before any suite in Protractor is run?

For example, I want to break up my Protractor tests into a series of suites for testing different aspects of my application. Let's say Jenkins is going to run the entire test suite before deployment, but individual developers will run only the suites pertinent to the work they're doing.

But I want most of my suites to be able to login to the application, and I'd rather not repeat that login logic in every suite. Is there a way to have a pre-suite test that does stuff like logging in before all tests are run?

I thought about just listing this where I'm defining my suites in my Protractor config like

suites: {
  'my-profile': [
    './e2e/login/login-as-customer.e2e-spec.js',
    './e2e/my-profile/my-profile-change-password.e2e-spec.js',
    './e2e/my-profile/my-profile-change-username.e2e-spec.js'
  ],
  'my-contacts': [
    './e2e/login/login-as-customer.e2e-spec.js',
    './e2e/my-contacts/add-contact.e2e-spec.js',
    './e2e/my-contacts/remove-contact.e2e-spec.js'
  ]
}

But the problem is I don't want it to have to relogin each time when we run the entire test suite together, or if a developer wants to run more than one suite together. What I need is a pre-suite step somehow.

Is this possible with Protractor?

1
You can't use beforeAll() in your specs to login? - Gunderson
I think that would still cause the login to occur before every test suite, even when run together. I'm talking about doing this before all test suites, not before all specs. - A. Duff

1 Answers

4
votes

You are looking for the onPrepare() function in your conf file. The function is run before the test suites. Here is an example taken from https://github.com/angular/protractor/blob/master/spec/withLoginConf.js

onPrepare: function() {
    browser.driver.get(env.baseUrl + '/ng1/login.html');

    browser.driver.findElement(by.id('username')).sendKeys('Jane');
    browser.driver.findElement(by.id('password')).sendKeys('1234');
    browser.driver.findElement(by.id('clickme')).click();

    // Login takes some time, so wait until it's done.
    // For the test app's login, we know it's done when it redirects to
    // index.html.
    return browser.driver.wait(function() {
      return browser.driver.getCurrentUrl().then(function(url) {
        return /index/.test(url);
      });
    }, 10000);
  }