量角器+ TypeScript +黄瓜:未定义步骤定义错误

时间:2019-12-25 08:48:08

标签: typescript protractor cucumberjs

我正在学习量角器/ TypeScript /黄瓜,下面是我的第一个脚本。在尝试执行它时,我遇到了一个错误(也在下面提到)并被卡住了。有人可以让我知道我在想什么。

我已经定义了特征步骤,但是仍然得到未定义错误。 这是当前状态的屏幕截图: Project Screenshot

我尝试过的一切:

1)我尝试切换Step def的位置,甚至将其指向step_def.js而不是ste_def.ts。

更新“ require”中的路径时,也会出现以下错误:

  

require: '../../StepDefs/*.ts',

     

“ C:\ Program Files \ nodejs \ node.exe”   C:\ Users *** \ WebstormProjects \ CucumberProject \ node_modules \ protractor \ bin \ protractor   “ C:\ Program Files \ JetBrains \ WebStorm   2018.3.1 \ plugins \ JavaScriptLanguage \ helpers \ protractor-intellij \ lib \ protractor-intellij-config.js“   --intellijOriginalConfigFile = C:\ Users *** \ WebstormProjects \ CucumberProject \ config \ config \ CucumberConfiguration.js   --disableChecks [13:56:40] I / launcher-运行1个WebDriver实例[13:56:40] I / direct-直接使用ChromeDriver ...   [13:56:43] E /启动器-错误:

     
    

C:\ Users *** \ WebstormProjects \ CucumberProject \ StepDefs \ my_steps.ts:1     (函数(导出,需求,模块,__ filename,__ dirname))     {protractor“中的{browser,by,element};

  

SyntaxError:

  

意外令牌{在新脚本(vm.js:74:7)

2)删除并重新安装所有内容

这是代码段:

CucumberConfiguration.ts

exports.config = {
  useAllAngular2AppRoots: true,
  capabilities: {
    browserName: "chrome"
  },
  specs: ["../../FeatureFiles/*.feature"],

  framework: "custom",
  frameworkPath: require.resolve("protractor-cucumber-framework"),

  directConnect: true,
  noGlobals: true,

  cucumberOpts: {
    compiler: [],
    strict: true,
    require: "../StepDefs/*.js",
    // require: '../StepDefs/*.js',
    // require: '../StepDefs/my_steps.ts',

    tags: false,
    // format: ['pretty'],
    profile: false,
    "no-source": true
  }
};

my_steps.ts

import { browser, by, element } from "protractor";

var myStepDefinitionsWrapper = function() {
  this.Given(/^I am on first page$/, async function(callback) {
    browser
      .get(
        "http://www.way2automation.com/angularjs-protractor/registeration/#/login"
      )
      .then(function() {
        browser.sleep(1000);
      });
    await element(browser.model("Auth.user.name")).sendKeys("angular");
    await element(by.id("password")).sendKeys("password");
    await element(by.id("formly_1_input_username_0")).sendKeys("angular");
    await element(by.className("btn btn-danger")).click();
    await element
      .all(by.css("[href*='#/login']"))
      .first()
      .click()
      .then(function() {
        console.log(element(by.id("formly_1_input_username_0")));
      });
  });
};
module.exports = myStepDefinitionsWrapper;  

package.json

{
  "name": "CucumberProject",
  "version": "1.0.0",
  "description": "First Framework of Protractor with Type Script and cucumber",
  "main": "index.js",
  "scripts": {
    "test": "protractor tmp/config/configuration.js",
    "pretest": "tsc",
    "cucumbertest": "protractor config/config/CucumberConfiguration.js",
    "protractor": "./node_modules/protractor/built/cli.js",
    "webdriver-update": "./node_modules/.bin/webdriver-manager update"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/cucumber": "^6.0.0",
    "@types/jasmine": "^3.5.0",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^12.12.21",
    "cucumber": "~6.0.5",
    "jasmine": "^3.5.0",
    "package.json": "^2.0.1",
    "protractor-cucumber-framework": "^6.2.0",
    "ts-node": "^8.5.4",
    "protractor": "^5.4.2",
    "typescript": "3.6.4"
  }
} 

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "inlineSourceMap": true,
    "declaration": false,
    "noImplicitAny": false,
    "outDir": "config",
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules",
    "asyncAwait",
    "plugins.ts"
  ]
} 

2 个答案:

答案 0 :(得分:0)

这是一个打字稿错误。您正在尝试运行打字稿文件,或者您的打字稿类型定义之一丢失。

在您的配置文件中,我看到您指向specs: ["../../FeatureFiles/*.feature"],如果您没有在文件名的末尾指定扩展名,则将使用语法'{'来选择要执行的打字稿文件。错误。尝试在specs: ["../../FeatureFiles/*.feature.js"]之后添加.js。

还要确保在执行测试时正在运行量角器js文件,而不是ts文件。

如果这不起作用,请尝试检查package.json中有关量角器或黄瓜的某些缺少的类型定义。

答案 1 :(得分:0)

我找到了解决方案- 如下创建步骤def解决了该问题-

给定(“我在首页上”,功能(){

以前是-

给出(/ ^我在第一页$ /,异步函数(回调){