【问题标题】:Typescript: function is not a function in JavaScript to Typescript conversion?Typescript:函数不是JavaScript到Typescript转换中的函数吗?
【发布时间】:2018-10-10 22:27:38
【问题描述】:

我正在尝试将现有的、正在运行的 JavaScript 项目转换为 TypeScript,并且我特别想使用 PageObject 模式。

运行 npm test 时出现此错误:TypeError: client.page.signinPage is not a function。

这就是我所说的:

import * as dotenv from "dotenv";

dotenv.config();

module.exports = {
'User can sign in'(client: any) {
    const signinPage = client.page.signinPage();

我的功能在这里:

const signinCommands = {  
  signin(email: String, password: String) {
    return this
      .waitForElementVisible('@emailInput')
      .setValue('@emailInput', email)
      .setValue('@passwordInput', password)
      .waitForElementVisible('@signinButton')
      .click('@signinButton')
  }
};

module.exports = {  
  url: 'https://cjdocs.herokuapp.com/auth/signin',
  commands: [signinCommands],
  elements: {
    emailInput: {
      selector: 'input[type=email]'
    },
    passwordInput: {
      selector: 'input[name=password]'
    },
    signinButton: {
      selector: 'button[type=submit]'
    }
  }
};

我的 page_object_path 是在 nightwatch.json 文件中设置的。

那么,它怎么不认为我的函数是函数呢?

整个项目可以从:https://github.com/hellfireSteve/nightwatch-typescript-pageobject下载

【问题讨论】:

  • signinPage 是您使用 new 关键字实例化的类。然后你有一个所述类的对象,你可以在其上调用函数。请查看打字稿教程,因为这是非常基本的东西......
  • 顺便说一句...类应该以大写字母开头。
  • @Wernerson 我现在正在上 TypeScript 课程。
  • 不要运行测试,因为您使用的是 TypeScript,请查看 TypeScript 给您的错误。
  • 正如我在the previous question 上所说的,我认为问题在于您没有正确遵循documentation 中的“使用页面对象”说明。你的项目的文件结构是什么?您是否设置了page_objects_path 选项?我们可以尝试帮助您进行故障排除,但我们可能无法解决问题,除非您努力拼凑一个不依赖于您公司网络的可重现示例。

标签: typescript


【解决方案1】:

我能够重现 client.page.signinPage is not a function 错误。当我将 page_objects_pathpages 更改为 built/pages 时(因为我认为 Nightwatch 正在寻找 JavaScript 文件而不是 TypeScript 文件),该错误消失了,我得到了一个不同的错误,我相信这是因为我没有t 已正确安装浏览器驱动程序。尝试更改page_objects_path,希望您的测试能够成功。

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 2015-12-24
    • 2018-06-26
    • 1970-01-01
    • 2013-02-21
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多