【问题标题】:Angular E2E Testing Script TimeoutAngular E2E 测试脚本超时
【发布时间】:2021-02-09 09:36:04
【问题描述】:

我是 Angular 的新手 我用它创建了一个项目,到目前为止运行良好。但现在我想对其添加一些测试。

很遗憾,我无法让它工作。

所以我的应用是一个基本的待办事项应用。

在测试中我想检查是否创建了任务。

这是我的 task-form.po.ts

import { by, element, ElementArrayFinder, ElementFinder } from 'protractor';

export class TaskForm {

  get taskCreateButton(): ElementFinder {
    return element(by.css('#task-create-btn'));
  }

  get taskListItems(): ElementArrayFinder {
    return element.all(by.css('mat-list-item'));
  }

  get taskForm(): ElementFinder {
    return element(by.id('task-form'));
  }

  get taskFormInput(): ElementFinder {
    return element(by.id('mat-input-0'));
  }
}

app.e2e-spec.ts

...

describe('TaskForm', () => {
  let taskForm: TaskForm;
  let startCount: number;
  let createBtn: ElementFinder;
  let taskListItems: ElementArrayFinder;
  let taskFormNewInput: ElementFinder;
  let newTaskItemsNum: number;


  beforeEach( () => {
    taskForm = new TaskForm();
    createBtn = taskForm.taskCreateButton;
    taskListItems = taskForm.taskListItems;
    taskFormNewInput = taskForm.taskFormInput;
    taskListItems.count().then((originalCount: number) => {
      startCount = originalCount;
    });
  });

  it('should enter test task to task form', async () => {
    await taskFormNewInput.clear().then(() => {
      taskFormNewInput.sendKeys('This is a test task');
    });

    taskFormNewInput.getAttribute('value').then(taskInput => {
      expect(taskInput).toEqual('This is a test task');
    });
  });

  it('should add a task', async () => {
    await taskFormNewInput.clear().then(() => {
      taskFormNewInput.sendKeys('This is a test task');
    });

    await createBtn.click();
    newTaskItemsNum = await taskForm.taskListItems.count();
    expect(newTaskItemsNum).toBe(startCount);
  });
});

该任务将被添加,但随后它在脚本超时中运行。

我尝试了几件事,但要么无法创建任务,要么在脚本超时中运行。

【问题讨论】:

  • 不要混用.then()和await

标签: angular typescript testing protractor e2e-testing


【解决方案1】:

https://stackoverflow.com/a/66072132/6793637

阅读上面的堆栈溢出知道如何使用protractor-jasmine(javascript不是类型脚本)

但方法和设计保持不变。

使用 Type 脚本时使用:使用 ES2017,因为此版本中引入了 await

https://blog.logrocket.com/async-await-in-typescript/

还有一些好的做法:

  1. 如果规范(It 块)花费更长的 Jasmine.Default_timeout,则将测试用例拆分为更小的用例
  2. 如果您确实需要更多时间,请在配置文件中或根据规范进行设置:

从配置中设置:

https://www.protractortest.org/#/timeouts

为所有脚本超时增加 config.allScriptsTimeout:

 allScriptsTimeout: timeout_in_millis

对于默认超时错误,请在 config.jasmine.Opts 中使用它

  jasmineNodeOpts: {defaultTimeoutInterval: timeout_in_millis}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2019-07-24
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多