angular - formControlName 的 Protractor 定位器

标签 angular protractor

我正在使用基于模型的表单,我的表单如下所示:

<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
  <input type="text" formControlName="username">
  <input type="password" formControlName="password">
  <button type="submit" [disabled]="!form.valid">Submit</button>
</form>

我想写一个 Protractor 规范来测试登录。我喜欢在我的规范中做类似下面的事情:

    element(by.formControlName('username')).sendKeys('test@example.com');

这显然行不通,那么有没有一种方法可以通过表单控件进行定位,或者我是否不得不接受必须在输入字段上放置类或 ID 的事实?

最佳答案

您可以编写一个相当简单的 CSS 选择器:

element(by.css("input[formControlName=username]")).sendKeys('test@example.com');

请注意,如果您需要经常这样做,您总是可以定义一个可重用的 custom locator :

by.addLocator('formControlName', function(value, opt_parentElement, opt_rootSelector) {
  var using = opt_parentElement || document;

  return using.querySelectorAll('[formControlName="' + value +'"]');
});

用法:

element(by.formControlName('username')).sendKeys('test@example.com');

关于angular - formControlName 的 Protractor 定位器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39918114/

相关文章:

angular - 如何在状态由 ngrx-forms 管理的情况下动态地将 formgroup 控件添加到 Angular 的 formarray 中?

angular - 用于下载媒体文件和调度进度的 NgRX 效果,直到调度完成操作

javascript - Protractor :计算所有超链接并在页面上测试它们

javascript - 如何从 Protractor 返回对象

angular - 蒙哥错误: user is not allowed to do action

angular - 在 Primeng 的表格中使用复选框时会自动选择所有行

javascript - 使用 Protractor 进行数据驱动测试

angularjs - 如何在 Protractor 测试运行之前设置浏览器大小?

javascript - 使用页面对象支持的 Protractor 测试多个浏览器

html - 将 svg 图像转换为按钮