我正在编写一个自动化程序,它基本上从 Excel 工作表中读取 url 列表,并根据我的响应检查每个 url 的状态是否成功(200)、重定向(301/302)或未找到(404)正在使用每个网址的状态更新工作表。
我能够涵盖几乎所有条件,但很难处理那些域名无法访问或无法访问的网址,因为这是一个网络错误,因此每当我的代码尝试访问这些网址时,测试就会在该特定阶段终止,并且整个自动化失败了。相反,我想跳过这些网址并使用“无法访问”或类似的状态更新工作表。
我无法找到正确的方法来涵盖这个案例,作为柏树的初学者,我尽了最大努力。
这里我添加我尝试过的内容。
const XLSX = require('xlsx');
describe('Website Test', () => {
it('should open websites from Excel and update status', () => {
// Read Excel file
cy.readFile('/Link.xlsx', 'binary').then((fileContent) => {
const workbook = XLSX.read(fileContent, { type: 'binary' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
// Get all URLs from the Excel sheet
const urls = XLSX.utils.sheet_to_json(worksheet, { header: 1, range: 1 });
// Iterate through each URL
urls.forEach((url, rowIndex) => {
const websiteUrl = url[1]; // Assuming the URL is in the first column
// Check if the websiteUrl is empty, and terminate the test if true
if (!websiteUrl) {
cy.log(`Skipping empty row at index ${rowIndex}`);
return;
}
// Use cy.request to get the response status, follow redirects
cy.request({
url: websiteUrl,
followRedirect: false,
failOnStatusCode: false,
}).then((response) => {
// Update status in Excel based on the final status
let status;
let result;
if (response.status === 302 || response.status === 301) {
status = `redirected to ${response.redirectedToUrl}`;
} else if (response.status === 200) {
status = 'opened';
} else {
status = 'failed';
}
XLSX.utils.sheet_add_aoa(worksheet, [[status]], { origin: `C${rowIndex + 2}` });
XLSX.utils.sheet_add_aoa(worksheet, [[result]], { origin: `D${rowIndex + 2}` });
// Save the updated Excel file after each request
const updatedFileContent = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });
cy.writeFile('Link.xlsx', updatedFileContent, 'binary');
});
});
});
使用此方法,对于不可访问的域,测试在 cy.request() 处失败,并显示错误消息: 错误:getaddrinfo ENOTFOUND bvicameroon.com
我使用 cy.intercept()尝试了另一种方法。
cy.intercept('GET', websiteUrl, (req) => {
console.log(req)
req.continue((res) => {
console.log(res)
})
}).as('websiteUrlRequest');
cy.wait('@websiteUrlRequest', { timeout: 10000 }).then((interception) => {
console.log(interception)
});
首先使用这种方法,我无法记录拦截的请求/解析结果。它正在 cy.wait 内记录拦截对象,但仅适用于有效域 对于不可访问的域,它在 cy.wait() 上失败并显示错误消息:
赛普拉斯错误 10000ms后超时重试:cy.wait()超时 等待 10000 毫秒等待第一个路由请求:websiteUrlRequest。不 曾经发生过请求
任何人都可以建议我可能的方法来覆盖不可访问域的条件(网络错误)吗?
检查 URL 是否可访问的方法是使用
ping
。总的来说,您在
cy.request()
之前进行该检查,与空 URL 检查相同,但它是异步调用,因此它将在 .then()
回调内部进行检查。
// Check if the websiteUrl is empty
if (!websiteUrl) {
cy.log(`Skipping empty row at index ${rowIndex}`);
return;
}
// Check if the websiteUrl is reachable
cy.exec(`ping ${websiteUrl}`).then(reply => {
if (reply.code !== 0) {
cy.log(`Skipping unreachable URL at index ${rowIndex}`);
return;
} else {
cy.request({
...
如果提取代码将XL更新为函数,也可以在
ping
失败后更新。
function updateXL({status, result}) {
XLSX.utils.sheet_add_aoa(worksheet, [[status]], { origin: `C${rowIndex + 2}` });
XLSX.utils.sheet_add_aoa(worksheet, [[result]], { origin: `D${rowIndex + 2}` });
// Save the updated Excel file after each request
const updatedFileContent = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });
cy.writeFile('Link.xlsx', updatedFileContent, 'binary');
}
...
// Check if the websiteUrl is reachable
cy.exec(`ping ${websiteUrl}`).then(reply => {
if (reply.code !== 0) {
cy.log(`Skipping unreachable URL at index ${rowIndex}`);
updateXL({status: 'unknown', result: 'unreachable'})
return;
} else {
cy.request({
...