javascript - getText 和 getAttribute 获取 Protractor 中输入字段的空值

标签 javascript protractor

我在自动完成字段中输入了一个值,并且我正在编写一个测试该值是否已正确填充。

但是使用 getText 和 getAttribute 得到的结果要么为 null,要么为空。

下面是我根据其他人回答的问题尝试过的三种方法,但似乎都不起作用。

所有这些要么返回空,要么返回 null。

element(by.name("visitPurposeID")).getText().then(function(visitpurpose) {
            console.log(visitpurpose);
            expect(visitpurpose).toEqual('New Visit');  
          });
          element(by.name("visitPurposeID")).getAttribute('visitpurpose').then(function(visitpurpose){
              console.log(visitpurpose);
          })
        expect(element(by.name("visitPurposeID")).getAttribute('visitpurpose')).toBe('New Visit');

元素的 html

<div class="mat-form-field-infix"><input class="mat-input-element mat-form-field-autofill-control ng-tns-c64-611 cdk-text-field-autofill-monitored ng-pristine ng-valid ng-touched" matinput="" name="visitPurposeID" placeholder="Purpose of Visit" required="" ng-reflect-required="" ng-reflect-autocomplete="[object Object]" ng-reflect-form="[object Object]" ng-reflect-placeholder="Purpose of Visit" autocomplete="off" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-haspopup="true" id="mat-input-203" aria-invalid="false" aria-required="true"><mat-autocomplete class="mat-autocomplete ng-tns-c64-611" ng-reflect-display-with="displayPurposeOfVisitFn(value)"><!----></mat-autocomplete><span class="mat-form-field-label-wrapper"><!--bindings={
  "ng-reflect-ng-if": "true"
}--><label class="mat-form-field-label ng-tns-c29-619 ng-star-inserted" ng-reflect-ng-switch="true" ng-reflect-disabled="false" id="mat-form-field-label-699" for="mat-input-203" aria-owns="mat-input-203"><!--bindings={
  "ng-reflect-ng-switch-case": "false"
}--><!--bindings={
  "ng-reflect-ng-switch-case": "true"
}--><mat-label class="ng-tns-c64-611 ng-star-inserted">Purpose of Visit</mat-label><!----><!--bindings={
  "ng-reflect-ng-if": "true"
}--><span aria-hidden="true" class="mat-placeholder-required mat-form-field-required-marker ng-tns-c29-619 ng-star-inserted"> *</span></label></span></div>

还有其他方法可以捕获输入字段上的文本吗?

最佳答案

尝试下面的方法

expect(element(by.name("visitPurposeID")).getAttribute('value')).toBe('New Visit');

希望对你有帮助

关于javascript - getText 和 getAttribute 获取 Protractor 中输入字段的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57214840/

相关文章:

Protractor 在预期条件下的超时代表什么?

javascript - 如果发生验证错误,Ajax 表单将发送两次

angular - Protractor 测试中的异步/等待逻辑未检测到页面元素

javascript - 在 Protractor angularjs 上指定了无效或非法的选择器错误

javascript - 将剪贴板内容作为字符串变量获取

Protractor 查找并单击元素

java - Selenium 独立版已退出,代码为 null

javascript - JS正则表达式匹配 ' but not\'

javascript - 在 Javascript 中追加多个输入以提交

javascript - Reactjs - 从数据集构建表单,FormElement 不返回渲染函数