I'm new to TypeScript, but experienced in C#. I'm trying to do something very simple, and I'm missing something fundamental. I'm using Protractor to write User Tests on an Angular front-end. I'm using the Page Object pattern, and I want to keep the Page Object in a separate file from my user tests so I can share the Page Object among several user test files.
I keep getting an error saying the page object class is not a function when I try to new up the class. What am I doing wrong? Here's my code that is failing with the error:
TypeError: UserTests.DashboardPage is not a function.
Page Object File: named dashboard.pageobject.ts
module UserTests {
export class DashboardPage {
setupPage = function () {
console.log("setupPage function");
};
}
}
Protractor Test File: name dashboard.test1.spec.ts
/// <reference path="../../../typings/jasmine.d.ts" />
/// <reference path="../../../typings/angular-protractor.d.ts" />
/// <reference path="../../../typings/selenium-webdriver.d.ts" />
/// <reference path="dashboard.pageobject.ts" />
module UserTests {
describe('When testing the Dashboard page', function() {
it('Should pass', function () {
var page = new UserTests.DashboardPage();
});
});
}
The error occurs on the line: var page = new UserTests.DashboardPage();
UPDATE (11/17/15):
I finally put all the pieces together after reading basarat's reply about internal modules. I removed the module keyword from the page object file, made the method setupPage public, and added an import statement in the spec file. Reading about external modules made me realize Node considers each file a module already. This post (also by basarat) drove the point home for me to make the function public. Also, I installed RequireJS. Here's my final code.
Page Object File:
export class DashboardPage {
public setupPage = function () {
console.log("setupPage function");
};
}
Protractor Test File:
/// <reference path="../../../typings/jasmine.d.ts" />
/// <reference path="../../../typings/angular-protractor.d.ts" />
/// <reference path="../../../typings/selenium-webdriver.d.ts" />
import pageObj = require("./dashboard.pageobject");
describe('When testing the Dashboard page', function() {
it('Should pass', function () {
var page = new pageObj.DashboardPage();
page.setupPage();
console.log('I made it!');
});
});
My thanks goes out to the people who responded!!