迁移到 TypeScript.为什么我的功能无法识别? [英] Migrating to TypeScript. Why is my function not recognised?

查看:48
本文介绍了迁移到 TypeScript.为什么我的功能无法识别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个有效的 JavaScript 项目迁移到 TypeScript.项目使用 nightwatch.js

I am trying to migrate a working JavaScript project to TypeScript. The project uses nightwatch.js

这是我的主要测试类:

declare function require(path: string): any;

import * as dotenv from "dotenv";
import signinPage = require("../built/pages/signinPage.js");
import instancesPage = require("../built/pages/instancesPage.js");
dotenv.config();

module.exports = {
    'User can sign in'(client) {

        console.log("Sign in as email: " + process.env.EMAIL);

        signinPage
            //   .navigate()
            .signin(process.env.EMAIL, process.env.PASSWORD);

        // give it time to load

        client.pause(5000);

        instancesPage.expect.element('@homepageWelcomeTitle').text.to.contain('Welcome to the CJDocs Home!');

        client.end();
    }
}

这是 pageObject,包含有问题的函数:

This is the pageObject, containing the offending function:

  module.exports = {  
  signin: function(email, password) {
    return this
      .waitForElementVisible('@emailInput')
      .setValue('@emailInput', email)
      .setValue('@passwordInput', password)
      .waitForElementVisible('@signinButton')
      .click('@signinButton')
  },
  elements: {
    emailInput: {
      selector: 'input[type=email]'
    },
    passwordInput: {
      selector: 'input[name=password]'
    },
    signinButton: {
      selector: 'button[type=submit]'
    }
  }
};

当我运行这个(使用 NPM 测试从终端运行)时,出现错误:

When I run this (running from terminal using NPM test), I get an error:

TypeError: signinPage.signin is not a function

据我所知,signinPage.signin 是一个函数.

As far as I can see, signinPage.signin IS a function.

为什么它不识别我的功能?

Why is it not recognizing my function?

推荐答案

这段代码仍然非常 JS/ES5 而不是 TS.尝试将 import signinPage 更改为 import * as signinPage.也许这会有所帮助.

this code is still very much JS/ES5 and not so much TS. Try to change import signinPage into import * as signinPage. Maybe that helps.

但我真的建议查找 ES6/TS 功能,如类.所以制作signinPage.ts:

But really I recommend looking up ES6 / TS functionalities like classes. So make signinPage.ts:

export default class signinPage {
  signin(email, password) {
    return this
      .waitForElementVisible('@emailInput')
      .setValue('@emailInput', email)
      .setValue('@passwordInput', password)
      .waitForElementVisible('@signinButton')
      .click('@signinButton')
  }
  get elements() {
    return {
      emailInput: {
        selector: 'input[type=email]'
      },
      passwordInput: {
        selector: 'input[name=password]'
      },
      signinButton: {
        selector: 'button[type=submit]'
      }
    }
  }
}

然后将您的导入更改为 import singinPage from "../src/pages/signinPage"

and then change your import to import singinPage from "../src/pages/signinPage"

所以您从 TS 文件指向另一个 TS 文件,但您不需要 .ts 扩展名.

so you're pointing from TS file to another TS file, but you dont need the .ts extention.

这篇关于迁移到 TypeScript.为什么我的功能无法识别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆