I’m working on a testing project where I use Cypress. I would like to intercept a network request for a javascript file and make a small change before I send the response back to the application. When I run my cypress test file (UI mode), I can see in the browser dev tools that the network request is made successfully.
But when I try to intercept that request with Cypress it doesn’t seem to work.
Note: I notice that I can successfully intercept requests to localhost .js files, but not to an external domain.
By the way, this request takes place during the initialization in the beforeEach method, thus I have not included any test method, since they are not relevant.
Here’s my test file:
describe("Smoke test", () => {
beforeEach(() => {
cy.intercept('GET', '/aws-config/*',{
statusCode: 200,
body: awsConfig
}).as('Manifest')
** cy.intercept({method: 'GET', url: 'https://login.salesforce.com/support/api/53.0/lightning/opencti_min.js'},(req) => {
console.log(`Request intercepted by Cypress: ${req.url}`)
})**
cy.visit(mainUrl, {
onBeforeLoad(win){
win.parent.addEventListener('message', (e) => { handlePostMessagesFromInGenius(e) })
cy.spy(win, 'postMessage').as('postMessage');
}});
});
it("Should load app", () => {
cy.visit(mainUrl).wait('@Manifest')
cy.get('@Manifest').its('request.url').should('contain', 'aws-config')
});
it("Should connect the monitor", () => {
cy.get('[data-tst-loc="ExtensionFieldInput"]').type('2215');
cy.get('[data-tst-loc="StartMonitorBtn"]').click()
cy.get('[data-tst-loc="ExtensionFieldInput"]').should('not.be.visible');
})
});
On my cypress.config.ts file:
import { defineConfig } from "cypress";
export default defineConfig({
chromeWebSecurity: false,
defaultCommandTimeout: 6000,
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});