1

I would like to mock the window.open function in testcafe. so that if my app calls window.open rather than hitting actual one we can use mock

something like this would be better

onBeforeLoad: (window) => {
   cy.stub(window, 'open');
}
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
questionar
  • 274
  • 2
  • 18

1 Answers1

7

To achieve this goal, use the 'Inject Scripts into Tested Pages' feature.

const mockWindowOpen = "window.open = function () { };";
test
   ('My test', async t => { /* ... */ })
   .clientScripts({ content: mockWindowOpen });
  • can we achieve the same with clientFunction? – Abhinaba Jul 24 '20 at 20:44
  • Using the 'Client Scripts' feature is the recommended way for this task. 'ClientFucntion' may work incorrectly in this scenario (it depends on the client code of your web application). – mlosev Jul 27 '20 at 08:13