ami*_*it 1 javascript cypress enotfound-error
使用cy.request()
链接 URL“http://www.alitots.co.uk/”失败并出现以下错误:
请求失败且没有响应
我们尝试向此 URL 发出 http 请求,但请求失败且没有响应。
我们在网络级别收到此错误:
错误:getaddrinfo ENOTFOUND www.alitots.co.uk我们发送的请求是:
方法:GET
URL:http://www.alitots.co.uk/
我该如何处理这个问题并继续检查其他链接?
这是完整的代码:
cy.get("a:not([href*='mailto:'])").each(($link, index) => {
const href=$link.prop('href')
cy.log(href)
if (href) {
cy.request({
url: href,
failOnStatusCode:false,
followRedirect:false,
failOnNetworkError:false
})
.then((response)=>{
if(response.status >= 400 || response.status == '(failed)') {
cy.log(` *** link ${index +1} is Broken Link ***`)
//brokenLinks++
} else {
cy.log(` *** link ${index+1} is Active Link ***`)
}
})
}
})
Run Code Online (Sandbox Code Playgroud)
错误代码ENOTFOUND
表示服务器未运行。
您目前正在测试的是response
,但没有服务器给出响应。
您需要添加 ping。如果您在终端中手动执行此操作ping http://www.alitots.co.uk
:
Ping 请求找不到主机http://www.alitots.co.uk。请检查名称并重试。
这个问题为您提供了一种从测试中 ping 服务器的方法
result.stdout
包含与上面相同的消息。
cy.get("a:not([href*='mailto:'])").each(($link, index) => {
const href = $link.prop('href')
if (!href) return
const url = new URL(href)
const host = url.hostname
cy.exec(`ping ${host}`, {failOnNonZeroExit:false}).then(reply => {
if (result.code !== 0) {
cy.log(`Could not ping "${href}", result.stdout`)
} else {
// now check response
cy.request({url: href, failOnStatusCode:false, followRedirect:false})
.then(response => {
if(response.status >= 400 || response.status == '(failed)') {
cy.log(` *** link ${index +1} is Broken Link ***`)
//brokenLinks++
} else {
cy.log(` *** link ${index+1} is Active Link ***`)
}
})
}
})
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
104 次 |
最近记录: |