我希望创建一个数组来存储多个条目的搜索栏,例如输入邮政编码到搜索栏,但和数组,存储说5-10个邮政编码
我知道如何使用.get和.click检索搜索栏以允许文本输入,但不确定如何创建和使用数组来使测试遍历数组中的所有元素
发布于 2021-08-26 01:36:24
Cypress测试是用javascript编写的--你可以创建一个数组,然后遍历它:
const zips = ['00000','12345','99999'];
zips.forEach( (zip) => {
cy.get('#yourElement).type(zip)';
} );发布于 2021-08-26 01:34:29
我用谷歌搜索栏试过这段代码。我只是使用vanilla javascript遍历了数组:
const searchTerms = ['cypress', 'puppeteer', 'cucumber'];
describe('Solving stackoverflow question', () => {
beforeEach(() => {
cy.visit('https://www.google.com/');
})
it('Google search', () => {
for (const term of searchTerms) {
cy.get('[aria-label="Search"]').clear()
.type(`${term}{enter}`);
cy.log(`Searched ${term}`);
}
});
});发布于 2021-08-26 04:44:19
从fixtures管理测试数据始终是一个很好的实践。
首先,在fixtures文件夹zip.json下创建一个文件。这个文件将包含我们的邮政编码数组。
["12345", "434343", "232525"]然后在你的测试中,你可以直接写:
cy.fixture('zip.json').then(testdata => {
testdata.forEach(zipCode => {
cy.get('element').type(zipCode)
})
})https://stackoverflow.com/questions/68929217
复制相似问题