【发布时间】: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. -
这表明您的索引存在问题。看来您不能在柏树包装的对象上使用
[],或者至少在这种情况下不能使用。在尝试索引它们之前包装ldapUserNames和ldapUserIds的目的是什么? -
您的意思是
expect(item).to.contain(cy.wrap(ldapUserNames[index]));,还是只是expect(item).to.contain(ldapUserNames[index]);? -
目标是有一个更干净的代码。因此,具有 2 个参数的列表与 2 个列表不同。我希望只有一份清单。就这样。假设我的列表中有 5 个参数,我应该列出 5 个列表来测试我的案例吗?这一点都不好。所以,我的问题是如何用 2 个参数测试一个列表?谢谢。
标签: javascript angular-material2 angular6 cypress