简体   繁体   English

如何通过AngularJS / Karma e2e测试解决动态添加的表单字段?

[英]How to address dynamically added form fields from an AngularJS / Karma e2e test?

How can I address dynamically added form fields from an AngularJS / Karma e2e test? 如何处理来自AngularJS / Karma e2e测试的动态添加的表单字段? Which selector can I use to target these? 我可以使用哪个选择器来定位这些?

I will use the AngularJS form example as a reference. 我将使用AngularJS表单示例作为参考。

While the static form fields, like 而静态表单字段就像

<input type="text" ng-model="form.name" required/>

can be targeted like 可以像

 input('form.name').enter('change');

how would I target the dynamically added fields? 如何定位动态添加的字段? I refer to the fields added by clicking "add" in this section: 我指的是在本节中单击“添加”添加的字段:

<label>Contacts:</label>
    [ <a href="" ng-click="addContact()">add</a> ]
    <div ng-repeat="contact in form.contacts">
      <select ng-model="contact.type">
        <option>email</option>
        <option>phone</option>
        <option>pager</option>
        <option>IM</option>
      </select>
      <input type="text" ng-model="contact.value" required/>
       [ <a href="" ng-click="removeContact(contact)">X</a> ]
    </div>

EDIT: Entering the first contact details is straightforward. 编辑:输入第一个联系方式很简单。 Simulating the click on addContact is straightforward. 模拟addContact的点击非常简单。 But how do I enter the next contact details, in the added fields? 但是,如何在添加的字段中输入下一个联系方式? I can't really use the same selector again... The following changes both contact.value 我真的不能再次使用相同的选择器...以下内容更改了两个contact.value

input('contact.value').enter('abc@mail.com');

Like you said, if you only need to enter the same value you can you your code: 就像您说的那样,如果您只需要输入相同的值,则可以编写代码:

input('contact.value').enter('abc@mail.com');

If you need to input different values, you need to change your html in order to add classes to the relevant divs: 如果需要输入其他值,则需要更改html以便将类添加到相关的div:

<label>Contacts:</label>
[ <a href="" ng-click="addContact()">add</a> ]
<div class="contacts-holder" ng-repeat="contact in form.contacts">
  <select ng-model="contact.type">
    <option>email</option>
    <option>phone</option>
    <option>pager</option>
    <option>IM</option>
  </select>
  <input class="contact-input" type="text" ng-model="contact.value" required/>
   [ <a href="" ng-click="removeContact(contact)">X</a> ]
</div>

Then in your JS code you can reference those using: 然后,在您的JS代码中,您可以使用以下代码引用这些代码:

element('.contacts-holder').query(function (contacts, done) {
   //contacts is now a jquery lite element
   angular.forEach(contacts.find('.contact-input'), function( contact) {
         element(contact).input( 'whatever you want...');
   }
}

Read more about AngularJs E2e Here 在此处阅读有关AngularJs E2e的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM