How to create a class in TypeScript

I am new to TypeScript, but experience in C #. I am trying to make something very simple, and I am missing something fundamental. I use Protractor to write custom tests on the Angular interface. I am using a page object template, and I want to save the page object in a separate file from my user tests so that I can share the page object among several user test files.

I keep getting an error saying that the page object class is not a function when I try to create a new class. What am I doing wrong? Here is my code that does not work with 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");
        };
    }
}

Prototype Test File: dashboard.test1.spec.ts Name

/// <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 answer about internal modules. I removed the module keyword from the page object file, made the setupPage method public, and added an import statement to the specification file. Reading about external modules made me realize that Node treats each file as a module already. This message (also on basarat) led me to discover this feature. In addition, I installed RequireJS. Here is my final code.

Page Object File:

export class DashboardPage {
    public setupPage = function () {
        console.log("setupPage function");
    };
}
Run codeHide result

Test file prototype:

/// <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!');
    });
});
Run codeHide result

I thank the people who responded!

+4
2

GitHub, Typescript Protractor . , Typescript JavaScript .

:

  • dashboard.test1.spec.ts --outFile:

    tsc dashboard.test1.spec.ts --outFile dashboard.test1.spec.out.js
    
  • dashboard.test1.spec.out.js Protractor.

, dashboard.test1.spec.out.js , ( ).

:

  • --outFile, (/// <reference path="file-name.ts" />), JavaScript Typescript ( ).
  • --out --outFile. --out --outFile (. Typescript ), .
+2

Source: https://habr.com/ru/post/1616134/


All Articles