Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor and Angular: How to test two pages in an app, one after the other?

I would like to run Protractor tests on two separate pages in my Angular app: /dashboard and /articles.

The complication is that I have to log in to the app manually.

Currently I have this setup:

var LoginPage = function() {
  ptor = protractor.getInstance();
  this.login = function(url) {
    ptor.get(url);
    ptor.findElement(protractor.By.model('email')).sendKeys(config.LOGIN_EMAIL);
    ptor.findElement(protractor.By.model('password')).sendKeys(config.LOGIN_PASS);
    ptor.findElement(protractor.By.tagName('button')).click();
  };
};

describe('The dashboard', function() {
  console.log('logging in');
  var loginPage = new LoginPage();
  loginPage.login(config.DASHBOARD_URL);
  console.log('logged in');
  it('has a heading', function() {
    console.log('testing dashboard 1');
    heading = ptor.findElement(protractor.By.tagName('h1'));
    expect(heading.getText()).toEqual(config.DASHBOARD_HEADING);
  });
});

describe('The article widget', function() {
  console.log('logging in');
  var loginPage = new LoginPage();
  loginPage.login(config.ARTICLE_URL);
  console.log('logged in');
  it('has a heading', function() {
    console.log('testing article 1');
    heading = ptor.findElement(protractor.By.tagName('h1'));
    expect(heading.getText()).toEqual(config.ARTICLES_HEADING);
  });
});

This gives me the following output:

Selenium standalone server started at http://192.168.2.9:56791/wd/hub
logging in
LoginPage
logged in
logging in
LoginPage
logged in
testing dashboard 1
Ftesting article 1

It looks as though both the describe sections are kicking off in parallel. How can I force the following sequence of events, while still structuring the code in a sensible way?

  1. Load dashboard page
  2. Log in
  3. Run dashboard tests
  4. Load article page (Assume we are already logged in)
  5. Run article tests
like image 292
Richard Avatar asked Nov 25 '13 17:11

Richard


3 Answers

You can move the login to another file.

Then, in your protractor configuration file do this:

exports.config = {
  specs: [
    'spec/login.js',
    'spec/dashboard_test.js',
    'spec/article_test.js'
  ],
  ...
};

Login will run before the other tests

like image 171
Andres D Avatar answered Oct 15 '22 23:10

Andres D


describe('my app', function(){
    beforeEach(function(){
        login()...
    })
    describe('dashboard');
    describe('the article widget')
});
like image 42
user2167582 Avatar answered Oct 15 '22 22:10

user2167582


The Protractor documentation recommends

put your log-in code into an onPrepare function, which will be run once before any of your tests.

For example in your protractor.conf

onPrepare: function() {
    browser.driver.get('http://localhost/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);
}
like image 20
Tamlyn Avatar answered Oct 16 '22 00:10

Tamlyn