【问题标题】:getText and getAttribute getting null value for input field in protractorgetText 和 getAttribute 为量角器中的输入字段获取空值
【发布时间】:2023-03-12 18:10:02
【问题描述】:

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

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

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

所有都返回空或空返回给我。

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>

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

【问题讨论】:

  • 你能得到元素吗?同时分享 html,以便我提出更好的解决方案。
  • @Zohair 我能够获取发送键的元素并选择建议,只是无法取回输入中输入的内容。我已更新问题以包含元素的 html
  • 您的HTML sn-p 上没有任何价值,您能否在DOM 中添加预期的文本。

标签: javascript protractor


【解决方案1】:

试试下面这个

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

希望对你有帮助

【讨论】:

  • 谢谢@Madhan。有用。那么这是否意味着 getAttribute('value') 是一个修复代码,而不是假设将 'value' 更改为其他东西?在我之前的代码中,我将名称更改为我可以轻松引用的名称
  • 如果我想连续做验证怎么办?似乎只有第一个给出正确的值。下一个期望是给 null。我是否对所有 getAttribute 使用“值”?
  • @hafizan 是的,适用于所有地方。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
相关资源
最近更新 更多