【问题标题】:Compare list with 2 arguments将列表与 2 个参数进行比较
【发布时间】:2018-07-10 10:05:09
【问题描述】:

希望你能帮帮我..

我想写一个“赛普拉斯命令”,它允许测试包含名称和 id 的列表是否显示良好。

模板:

<mat-option 
  *ngFor="let user of filteredUsers$ | async" 
  [value]="user.id" 
  class="option-ldap-user-content"
>
  <span 
    class="ldap-user-name"
    classToApply="bold"
    appColorSearchedLetters 
    [text]="user.name" 
    [search]="addLdapUserForm.get('userSearchCtrl').value"
  ></span> |
  <small
    class="ldap-user-id"
    classToApply="bold" 
    appColorSearchedLetters 
    [text]="user.id" 
    [search]="addLdapUserForm.get('userSearchCtrl').value"
  ></small>
</mat-option>

commands.js:

Cypress.Commands.add('expectLdapUsersListToBe', (list1, list2) => {
  const ldapUserNames = cy.get(ADD_LDAP_USER_DOM.texts.ldapUserNames);

  ldapUserNames.should('have.length', list1.length);

  ldapUserNames.each(($item, index) => {
    cy.wrap($item).contains(list1[index]);
  });
  const ldapUserIds = cy.get(ADD_LDAP_USER_DOM.texts.ldapUserIds);

  ldapUserIds.should('have.length', list2.length);

  ldapUserIds.each(($item, index) => {
    cy.wrap($item).contains(list2[index]);
  });
});

e2e-spec.js:

it.only(`should display filtered list according to search string`, () => {
  cy.get(ADMINISTRATION_DOM.expPanel.expPanelAddUser).click();

  cy
    .get(ADD_LDAP_USER_DOM.inputs.userSearchCtrl)
    .should('be.empty')
    .type(`e`);

  cy
    .get(`.multi-match`)
    .contains('7 people are matching this search.')
    .should('be.visible');

      // cy.expectLdapUsersListToBe(expected7LdapUsers); NOT WORKS
  cy.expectLdapUsersListToBe(expected7LdapUsersIds, expected7LdapUsersName);

...

  const expected7LdapUsers = [
    ['id1', 'UserName1'],
    ['id2', 'UserName2'],
    ['id3', 'UserName3'],
    ['id4', 'UserName4'],
    ['id5', 'UserName5'],
    ['id6', 'UserName6'],
    ['id7', 'UserName7'],
  ];

  const expected7LdapUsersIds = [
    'id1',
    'id2',
    'id3',
    'id4',
    'id5',
    'id6',
    'id7',
  ];

  const expected7LdapUsersName = [
    'UserName1',
    'UserName2',
    'UserName3',
    'UserName4',
    'UserName5',
    'UserName6',
    'UserName7',
  ];

我已经编写了一个暂时有效的命令,它可以验证名称和 ID 是否显示良好。除了我用 2 个列表测试它。 我希望只有一个。 请问有人帮我吗?

我测试了类似的东西,但它不起作用:

Cypress.Commands.add('expectLdapUsersListToBe', listOptions => {
  const ldapUserNames = cy.get(ADD_LDAP_USER_DOM.texts.ldapUserNames);
  const ldapUserIds = cy.get(ADD_LDAP_USER_DOM.texts.ldapUserIds);

  ldapUserNames.should('have.length', listOptions.length);
  ldapUserIds.should('have.length', listOptions.length);

  listOptions.forEach(($item, index) => {
    const item = cy.wrap($item);

    expect(item).to.contain(cy.wrap(ldapUserNames)[index]);
    expect(item).to.contain(cy.wrap(ldapUserIds)[index]);
  });
});

谢谢。

【问题讨论】:

  • but it's not working : 你能详细说明一下吗?您收到的错误消息是什么?
  • 当我使用这样的列表时:cy.expectLdapUsersListToBe(expected7LdapUsers); 我有这个错误:TypeError: obj.indexOf is not a function.
  • 这表明您的索引存在问题。看来您不能在柏树包装的对象上使用[],或者至少在这种情况下不能使用。在尝试索引它们之前包装 ldapUserNamesldapUserIds 的目的是什么?
  • 您的意思是expect(item).to.contain(cy.wrap(ldapUserNames[index]));,还是只是expect(item).to.contain(ldapUserNames[index]);
  • 目标是有一个更干净的代码。因此,具有 2 个参数的列表与 2 个列表不同。我希望只有一份清单。就这样。假设我的列表中有 5 个参数,我应该列出 5 个列表来测试我的案例吗?这一点都不好。所以,我的问题是如何用 2 个参数测试一个列表?谢谢。

标签: javascript angular-material2 angular6 cypress


【解决方案1】:

我更新了我现在使用的命令。

Cypress.Commands.add(
  'expectLdapUsersListToBe',
  (listUserNames, listUserIds) => {
    const ldapUserNames = cy.get(ADD_LDAP_USER_DOM.texts.ldapUserNames);

    ldapUserNames.should('have.length', listUserNames.length);

    ldapUserNames.each(($item, index) => {
      cy.wrap($item).contains(listUserNames[index]);
    });
    const ldapUserIds = cy.get(ADD_LDAP_USER_DOM.texts.ldapUserIds);

    ldapUserIds.should('have.length', listUserIds.length);

    ldapUserIds.each(($item, index) => {
      cy.wrap($item).contains(listUserIds[index]);
    });
  }
);

测试:

cy.expectLdapUsersListToBe(expected7LdapUsersName, expected7LdapUsersIds);

但正如我在最初的问题中所说,我希望有一个列表而不是 2 个。 这意味着需要一个带有 id 和 name 的列表。
我的第一个示例已弃用。

非常感谢大家提供这个助推器,这是一个演示错误..

【讨论】:

  • 哦,哇,我走得太远了。除了没有解决问题之外,我已经删除了我的答案,因为它似乎不准确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 2018-11-04
相关资源
最近更新 更多