Search code examples
angularprotractorhttp-status-codes

Allow 4xx HTTP status codes in protractor tests


I am developing a frontend with angular and started writing protractor e2e tests. The corresponding backend API might return 401, 404 or other client errors in some cases which is expected.

Here is a snippet where the login is tested:

it('should block unregistered users', () => {
  page.navigateTo()
  expect(page.showsLogin()).toBe(true);
  page.login("test", "test"); // http request against API happens here
  expect(page.showsLogin()).toBe(true);
});

The test fails because the api returns 404, but that is expected.

✗ should block unregistered users
  - Expected [ Entry({ level: SEVERE, message: 'http://localhost:4200/api/v1/auth - Failed to load resource: the server responded with a status of 404 (Not Found)', timestamp: 1587332452811, type: '' }) ] not to contain <jasmine.objectContaining(Object({ level: SEVERE }))>.

How can I make the test work? (I do not want to call the API directly from the test)

Or am I supposed to design my API and tests to only have 200s?


Solution

  • Sometimes reading is hard. So I started with the default e2e test from angular and it also includes the following piece of code:

    afterEach(async () => {
      // Assert that there are no errors emitted from the browser
      const logs = await browser.manage().logs().get(logging.Type.BROWSER);
      expect(logs).not.toContain(jasmine.objectContaining({
          level: logging.Level.SEVERE,
      } as logging.Entry));
    });
    

    I assume any client or server error codes are classified as severe logs. Uncommenting this turns the test green :)