1 回答
TA贡献1874条经验 获得超12个赞
虽然这可能不是每个人的解决方案。有人建议我创建一个任务 ( cy.task()) 并使用 Node js 中的代码来处理请求。对于我的请求,我使用 axios 包来处理获取请求并在内容与值匹配时返回布尔值以及状态代码。
编辑:但是,我仍然看到断言由于列表中途或接近尾声的超时而失败。几天后我在 Steam 网站上再次测试了这个,现在我的测试运行可以在 1.5 分钟内完成而没有问题。我确实创建了一种替代方法来一次执行对所有链接的请求,而不是通过迭代。(完成时间至少快了 10 秒。)
编辑:(旧方法):在 cypress/plugins/index.js 中找到的任务:
const axios = require('axios').default;
// use axios
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('task', {
//Work on request and return, learn what gets returned
getURLBodyResponseContains: ({href, target}) => {
return axios.get(href)
.then( (response) => {
//console.log('target: '+ target);
if (response.data.match(target)) {
return { answer: true, status: response.status };
} else {
return { answer: false, status: response.status };
}
})
.catch((error) => console.log(error));
}
})
测试代码:
it.only('Test Lanuage list', () => {
const steamHeader = new SteamGlobalHeader(cy);
steamHeader.getActionItem('span', 'language').click();
steamHeader.getActionSection().get('#language_dropdown')
.within(() => {
const langList = /^schinese|tchinese|japanese|koreana|thai|bulgarian|czech|danish|german|spanish|latam|greek|french|italian|hungarian|dutch|norwegian|polish|portuguese|brazilian|romanian|russian|finnish|swedish|turkish|vietnamese|Steam Translation Server/;
steamHeader.get('a[class="popup_menu_item tight"]').as('languageLinks');
steamHeader.get('@languageLinks').should('have.length', 28);
steamHeader.get('@languageLinks').each(($a) => {
steamHeader.getCy().task('getURLBodyResponseContains', {href: $a.prop('href'), target: langList}, {timeout: 100000}).as('returnValue');
steamHeader.get('@returnValue').its('status').should('equal', 200);
steamHeader.get('@returnValue').its('answer').should('be.true');
});
});
});
新方法:几天后我重新测试了我的测试代码,现在我没有高频率地遇到超时问题。在尝试找出替代方案时,我想到了一种同时执行所有请求而不是一个一个执行请求的方法。请求全部完成后,将结果作为一个数组返回,然后执行 forEach 循环,包装对象的属性并运行您的一组断言。使用这种方法,我注意到测试时间快了 10 秒。
任务代码:
const axios = require('axios').default;
// use axios
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('task', {
//Work on request and return, learn what gets returned
getURLBodyResponseContains: ({href, target}) => {
return axios.get(href)
.then( (response) => {
//console.log('target: '+ target);
if (response.data.match(target)) {
return { answer: true, status: response.status };
} else {
return { answer: false, status: response.status };
}
})
.catch((error) => console.log(error));
},
getURLListBodyResponseContains: ({hrefList, target}) => {
return Promise.all(hrefList.map((href) => {
// Would have wanted to reuse the task from above, but not sure you could reuse a task within a task.
return checkIfRequestContains(href, target);
}))
.then((result) => {
return result;
})
.catch((error) => console.log(error));
}
})
}
function checkIfRequestContains(url, target){
return axios.get(url)
.then( (response) => {
//console.log('target: '+ target);
if (response.data.match(target)) {
return { answer: true, status: response.status };
} else {
return { answer: false, status: response.status };
}
})
.catch((error) => console.log(error));
}
测试代码:
it('Test Language list', () => {
const steamHeader = new SteamGlobalHeader(cy);
steamHeader.getActionItem('span', 'language').click();
steamHeader.getActionSection().get('#language_dropdown')
.within(() => {
const langList = /^schinese|tchinese|japanese|koreana|thai|bulgarian|czech|danish|german|spanish|latam|greek|french|italian|hungarian|dutch|norwegian|polish|portuguese|brazilian|romanian|russian|finnish|swedish|turkish|vietnamese|Steam Translation Server/;
steamHeader.get('a[class="popup_menu_item tight"]').as('languageLinks');
steamHeader.get('@languageLinks').should('have.length', 28);
//This using task on whole array.
let urlList = [];
steamHeader.get('@languageLinks').each(($a) => {
urlList.push($a.prop('href'));
});
steamHeader.getCy().task('getURLListBodyResponseContains', {hrefList: urlList, target: langList}, {timeout: 100000}).as('returnValue');
steamHeader.get('@returnValue').then(($value) => {
console.log($value);
$value.forEach((val) => {
console.log(val);
steamHeader.getCy().wrap(val).its('status').should('eq', 200);
steamHeader.getCy().wrap(val).its('answer').should('be.true');
})
});
//This approach using each loop
/* steamHeader.get('@languageLinks').each(($a) => {
steamHeader.getCy().task('getURLBodyResponseContains', {href: $a.prop('href'), target: langList}, {timeout: 100000}).as('returnValue');
steamHeader.get('@returnValue').its('status').should('equal', 200);
steamHeader.get('@returnValue').its('answer').should('be.true');
}); */
});
})
添加回答
举报