Javascript 将列表与2个参数进行比较
我希望你能帮助我 我想写一个“Cypress命令”,它允许测试包含名称和id的列表是否显示良好 模板:Javascript 将列表与2个参数进行比较,javascript,angular-material2,angular6,cypress,Javascript,Angular Material2,Angular6,Cypress,我希望你能帮助我 我想写一个“Cypress命令”,它允许测试包含名称和id的列表是否显示良好 模板: <mat-option *ngFor="let user of filteredUsers$ | async" [value]="user.id" class="option-ldap-user-content" > <span class="ldap-user-name" classToApply="bold" appColo
<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>
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是否显示良好。除了我用两个列表测试它。
我希望只有一个。
有人帮我吗
我测试了类似的东西,但它不起作用:
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]);
});
});
谢谢。我更新了目前使用的命令
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和名称的列表。我的第一个示例已弃用
尽管如此,还是要感谢你的大力宣传,这是一个演示错误。哦,哇,我太离谱了。我删除了我的答案,因为它似乎不准确,而且没有解决问题。
但它不起作用:
你能详细说明一下吗?您收到的错误消息是什么?当我使用这样一个列表时:cy.expectLdapUsersListToBe(expected7LdapUsers)
我有一个错误:TypeError:obj.indexOf不是一个函数。
这将指向索引问题。似乎不能在cypress包装的对象上使用[]
,或者至少在这种情况下不能。在尝试索引之前,包装ldapUserNames
和ldapUserIds
的目的是什么?您的意思可能是expect(item).to.contain(cy.wrap(ldapUserNames[index])
,或者只是expect(item).to.contain(ldapUserNames[index])代码>?目标是拥有更清晰的代码。所以一个有两个参数的列表不同于两个列表。我只希望有一份清单。这就是全部。假设我的列表中有5个参数,我应该列出5个列表来测试我的案例吗?一点也不好。同样,我的问题是如何测试一个有两个参数的列表?谢谢
cy.expectLdapUsersListToBe(expected7LdapUsersName, expected7LdapUsersIds);