【问题标题】:Unable to run protractor test script using typescript [duplicate]无法使用打字稿运行量角器测试脚本[重复]
【发布时间】:2018-07-11 22:50:56
【问题描述】:

我正在尝试运行一个用打字稿编写的量角器测试脚本。但是当我这样做时,我会得到 'ManagedPromise' 作为回报。它没有显示任何错误,只是返回“ManagedPromise”

我的 Typescript 测试:

   //import {describe, it} from "selenium-webdriver/testing";
import {browser, by, element} from "protractor";

describe("For testing purpose", () => {
    it('should pass', () => {
        browser.get("https://angularjs.org");
        element(by.model('todoList.todoText')).sendKeys('write first 
        protractor test');
        element(by.css('[value="add"]')).click();
        let title = browser.getTitle();
        console.log(title);
    });
});

我的 conf.js 文件:

  import {browser} from "protractor";

  exports.config = {

    directConnect: true,
    capabilities: {
        'browserName': 'chrome'
    },

    framework: 'jasmine2',

    specs: ['./JSfiles/appExample.js'],

    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 9000
        /* getPageTimeout: 3000,
         allScriptsTimeout: 2000*/
    },

    onPreapre: () => {
        browser.driver.manage().window().maximize();
    }
};

我的 package.json 文件:

    {
  "name": "typescript-dev-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "@types/jasminewd2": "^2.0.3",
    "jasmine": "^3.1.0",
    "jasminewd2": "^2.2.0",
    "protractor": "^5.3.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

我的 tsconfig.json 文件:

"compilerOptions": {
/* Basic Options */
"target": "es5",                          
"module": "commonjs",
"esModuleInterop": true,
"strict": true,
"outDir": "./JSfiles",

我尝试运行测试时得到的响应:

   Started
ManagedPromise {
  flow_:
   ControlFlow {
     propagateUnhandledRejections_: true,
     activeQueue_:
      TaskQueue {
        name_: 'TaskQueue::101',
        flow_: [Circular],
        tasks_: [Array],
        interrupts_: null,
        pending_: null,
        subQ_: null,
        state_: 'new',
        unhandledRejections_: Set {} },
     taskQueues_: Set { [TaskQueue] },
     shutdownTask_: null,
     hold_:
      Timeout {
        _called: false,
        _idleTimeout: 2147483647,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 1694,
        _onTimeout: [Function],
        _timerArgs: undefined,
        _repeat: 2147483647,
        _destroyed: false,
        [Symbol(unrefed)]: false,
        [Symbol(asyncId)]: 109,
        [Symbol(triggerId)]: 104 } },
  stack_: null,
  parent_:
   ManagedPromise {
     flow_:
      ControlFlow {
        propagateUnhandledRejections_: true,
        activeQueue_: [TaskQueue],
        taskQueues_: [Set],
        shutdownTask_: null,
        hold_: [Timeout] },
     stack_: null,
     parent_:
      ManagedPromise {
        flow_: [ControlFlow],
        stack_: null,
        parent_: [ManagedPromise],
        callbacks_: [Array],
        state_: 'pending',
        handled_: true,
        value_: undefined,
        queue_: null },
     callbacks_: [ [Task] ],
     state_: 'pending',
     handled_: true,
     value_: undefined,
     queue_: null },
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  value_: undefined,
  queue_: null }

【问题讨论】:

  • 你是控制台。记录一个承诺。你需要解决它。
  • @Gunderson :是的,和这个类似。我试着搜索这个问题。但我没有得到这个问题。所以这就是我自己发帖的原因。

标签: typescript promise webdriver protractor


【解决方案1】:

正如 Gunderson 在 cmets 中所说:You are console.logging a promise. You need to resolve it
这发生在这个代码段:

let title = browser.getTitle();
console.log(title);

为了解决这个问题,你可以这样做

browser.getTitle().then(function(text) {
  console.log(text);
});

Protractor console log有更深入的解释。

如何使用异步执行:

it('should pass', async() => {
  //other codez here
  let title = await browser.getTitle();
  console.log(title);
}

【讨论】:

  • 也可以考虑将 async 放在 () 之前,然后说 let title = await browser.getTitle();
  • @BenMohorc:是的,你是对的。这是因为我使用 console.log 的方式。将其放入函数中后,它就起作用了。谢谢!!
  • @JeremyKahan:是的,我没有放置异步。我会把它放在'browser.getTitle'中。谢谢!!
  • await 在 browser.getTitle() 之前。 async 在“function”之前(实际上并没有在箭头符号中拼出)。大概这就是你的意思。如果这样做,则不需要整个 .then 结构(这不是对当前答案的批评,只是一种替代方法)
  • @JeremyKahan 我添加了使用异步执行相同操作的方法。感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 2019-08-28
  • 1970-01-01
相关资源
最近更新 更多