我们的网站是安全的,Chrome也显示“安全”锁图标。但是,当我运行赛普拉斯自动化测试时,赛普拉斯窗口将显示“不安全”。所以我在控制台上收到以下错误,页面未加载。
SecurityError:可能无法从通过HTTPS加载的页面启动不安全的SockJS连接
如何解决此问题
注意:我们有sockJs客户。因此,sockjs-client会引发此错误。
sockjs-client / lib / main.js:79
if (loc.protocol === 'https:' && !secure) {
throw new Error('SecurityError: An insecure SockJS connection may not be initiated from a page loaded over HTTPS');
}
[如果您只是想解决此错误(例如,首先不要在本地主机上抛出错误,则可以像这样将其静音。
将其放入您的cypress/support/index.js
:
// prevent cypress from failing
before(() => {
cy.on('uncaught:exception', err => {
if ( err && err.message.includes('SecurityError: An insecure SockJS') ) {
return false;
} else {
throw err;
}
});
});
// OPTIONAL: prevent your application from throwing, and thus logging to
// console.
// Why is this not enough to prevent Cypress from failing? Likely because
// Cypress binds its own error event handler and fails regardless if we
// prevent it in our app, or not.
Cypress.on('window:before:load', win => {
win.addEventListener('error', ev => {
if ( ev && ev.message.includes('SecurityError: An insecure SockJS') ) {
ev.preventDefault();
}
});
});