Mpasw
Mpasw

Reputation: 25

Can't get test through the end with protractor

I have simple test doing login and trying to check if it's success:

describe('My Angular App', function () {

  describe('visiting the main homepage', function () {

    beforeEach(function () {
      browser.get('/');
      element(By.id("siteLogin")).click();
    });

    it('should login successfully', function() {

      element(By.name("login_username")).sendKeys("[email protected]");
      element(By.name("login_password")).sendKeys("testpass");

      element(By.id("formLoginButton")).click().then(function() {

        browser.getCurrentUrl().then(function(url){
          expect(url).toContain("profile");
        });

      });

    });

  });

});

It goes well until that last part where I'm checking URL, and in Selenium Server I get:

INFO - Executing: [execute async script: try { return (function (rootSelector, callback) {
  var el = document.querySelector(rootSelector);

  try {
    if (window.getAngularTestability) {
      window.getAngularTestability(el).whenStable(callback);
      return;
    }
    if (!window.angular) {
      throw new Error('angular could not be found on the window');
    }
    if (angular.getTestability) {
      angular.getTestability(el).whenStable(callback);
    } else {
      if (!angular.element(el).injector()) {
        throw new Error('root element (' + rootSelector + ') has no injector.' +
           ' this may mean it is not inside ng-app.');
      }
      angular.element(el).injector().get('$browser').
          notifyWhenNoOutstandingRequests(callback);
    }
  } catch (err) {
    callback(err.message);
  }
}).apply(this, arguments); }
catch(e) { throw (e instanceof Error) ? e : new Error(e); }, [body]])

and also I get:

Failures:
1) My Angular App visiting the main homepage should login successfully
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

My protractor-conf.js:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  baseUrl: 'http://localhost:8080/Mysite',

  capabilities: {
    'browserName': 'firefox' // muste use firefox because I can't get .click() to work in Chrome
  },

  specs: [
    'spec-e2e/*.js'
  ],

  framework: 'jasmine2',

  jasmineNodeOpts: {
    isVerbose: true,
    showColors: true,
    defaultTimeoutInterval: 30000
  }
};

I appreciate any help on this one. Thanks

Upvotes: 1

Views: 506

Answers (1)

alecxe
alecxe

Reputation: 473863

Looks like there is a non-Angular page opened after a click. If this is the case, you need to turn the synchronization between Protractor and Angular off:

afterEach(function () {
  browser.ignoreSynchronization = false;
});

it('should login successfully', function() {
  element(By.name("login_username")).sendKeys("[email protected]");
  element(By.name("login_password")).sendKeys("testpass");

  browser.ignoreSynchronization = true;
  element(By.id("formLoginButton")).click().then(function() {
    expect(browser.getCurrentUrl()).toContain("profile");
  });
});

Note that you don't need to explicitly resolve the promise returned by getCurrentUrl and can let the expect() do that for you implicitly.

You may also need to wait for the URL to change:

var urlChanged = function(desiredUrl) {
  return browser.getCurrentUrl().then(function(url) {
    return url == desiredUrl;
  });
};

browser.wait(urlChanged("my desired url"), 5000);

Upvotes: 1

Related Questions