Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting browser path in angular e2e test causes an exception

Tags:

angularjs

I have a simple e2e test to verify that route redirection works

runner.html

<!doctype html>
<html lang="en">
  <head>
    <title>End2end Test Runner</title>
    <script src="../client/components/angular-scenario/angular-scenario.js" ng-autotest></script>
    <script src="e2e/scenarios.js"></script>
  </head>
  <body>
  </body>
</html>

scenarios.js

'use strict';

describe('e2e', function() {

  beforeEach(function() {
    browser().navigateTo('../../client/index.html');
  });

  it('should redirect to the main application home page with / is accessed', function() {
    browser().navigateTo('#!/');
    expect(browser().location().path()).toBe('/app');
  });
});

karma.conf.js

*snip*
files = [
  ANGULAR_SCENARIO,
  ANGULAR_SCENARIO_ADAPTER,
 './test/e2e/**/*.js',
];
*snip*

When this gets run, browser().location().path() will raise an exception:

TypeError: 'undefined' is not a function (evaluating '$document.injector()')

I've determined it's the .path() at the end that's causing the issue since if I do browser().location() no exception is raised.

However in the browsers console this will return a angular.scenario.Future as expected.

Why is an exception being raised?

like image 319
nathasm Avatar asked Mar 26 '13 18:03

nathasm


1 Answers

From the top of my head, here are the top reasons for AngularJS E2E tests not working

  1. The AngularJS E2E tests need you to have the ng-app defined in the HTML in your application. If you are bootstrapping it manually, you will need to add it yourself - Google Groups Discussion
  2. Ensure that your proxy is correctly defined, and make sure you can navigate to it directly. Add a pause() in your test right after the browser.navigateTo() and ensure that the app is actually loaded in the AngularJS Scenario Runner in the browser.
  3. Final step of debugging, add a sleep for 2 to three seconds before you get the browser location. It might be that the angularJS injector is not able to communicate properly to your AngularJS app, which causes it to not wait for the required amount of time.

Hope one of these does the trick for you!

like image 75
Shyam Seshadri Avatar answered Nov 05 '22 21:11

Shyam Seshadri