Cypress-添加Handlexpection代码,但仍然可以';无法捕获应用程序异常
我正在尝试测试这个站点,例如,当我选择一个地区(美国)并重定向到此url时:我在控制台中收到一个应用程序错误,cypress无法继续 我添加此代码是为了在测试开始之前处理异常,但它没有捕获异常:Cypress-添加Handlexpection代码,但仍然可以';无法捕获应用程序异常,cypress,Cypress,我正在尝试测试这个站点,例如,当我选择一个地区(美国)并重定向到此url时:我在控制台中收到一个应用程序错误,cypress无法继续 我添加此代码是为了在测试开始之前处理异常,但它没有捕获异常: cy.on('uncaught:exception', () => { return false }) 完整代码: describe('Purchase a device from Google Store', () => { const
cy.on('uncaught:exception', () => {
return false
})
完整代码:
describe('Purchase a device from Google Store', () => {
const LOCATORS = {
devicesMenu: '[id="desktop-products"]',
headerContainer: '.header-container',
regionTitle: '.region-title',
dialog: '[role="dialog"]',
button: '[role="button"]',
}
before(() => {
cy.visit('/')
})
beforeEach(() => {
Cypress.on('uncaught:exception', () => {
return false
})
})
it('Purchase device', () => {
let region = 'United States'
let urlToVerify = 'us/?hl=en-US®ionRedirect=true'
cy.contains(LOCATORS.regionTitle, region)
.click().then(() => {
cy.get(LOCATORS.dialog).within(() => {
cy.contains('Continue')
.click({ force: true }).then(() => {
cy.url()
.should('include', urlToVerify)
})
})
})
})
})
您可以在事件处理程序中测试
URL:changed
事件的URL
基本问题似乎是不可能将Cypress带到https://store.google.com
,浏览器以URLabout:blank
结束
但通过向url:changed
添加侦听器,您至少可以验证是否尝试了正确的url
it('区域选择后重定向',()=>{
常量区域=‘美国’
const urltovify='us/?hl=en-us®ionRedirect=true'
cy.on('url:changed',(url)=>{
expect(url).to.include(urltovify)//使用chai断言
})
cy.contains(LOCATORS.regionTitle,region)。单击()
cy.get(LOCATORS.dialog).in(()=>{
cy.contains('Continue')。单击({force:true})
})
})
谢谢你的回答@Steve,但这没有帮助。我可以看到异常确实被捕获了,但这是在被重定向到屏幕并与其元素交互后发生的。更新了问题并添加了屏幕快照,请检查。基本url是我尝试过的,但没有帮助。我在support/index.js中也有Cypress.on(…)作为一个全局解决方案,但这对您也没有帮助,Steve,但是验证url只是一个简化问题的示例。在url更改后,我需要在屏幕上进行多次验证。你认为这是cypress无法处理所有应用程序异常的限制吗?第一个异常“无法读取null的属性名”只是cypress运行程序说它无法显示shapshot,与测试无关。尝试访问shop.google,你会发现你无法访问,页面上的某些内容阻止了它。