Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playwright example #367

Open
mattoni opened this issue Oct 19, 2022 · 3 comments
Open

Playwright example #367

mattoni opened this issue Oct 19, 2022 · 3 comments

Comments

@mattoni
Copy link

mattoni commented Oct 19, 2022

Any examples on how to get this set up with Playwright? Having a hard time finding any way to mock websockets there.

@Atrue
Copy link
Collaborator

Atrue commented Nov 8, 2022

Once you create a Server instance the window.WebSocket object has been mocked by default. Usually the best place for it is beforeEach hook.
If it's not working for you it's better to check the documentation of the library. https://playwright.dev/docs/mock#creating-mocks
For example you can try mocking it manually like this:

import { WebSocket } from 'mock-socket';

test.beforeEach(async ({ page }) => {
  await page.addInitScript(() => {
    window.WebSocket = WebSocket;
  });
});

and create a server with mock: false option later:


test('...', async ({ page }) => {
  const server = new Server('ws://localhost:8080', { mock: false });
});

@mattoni
Copy link
Author

mattoni commented Nov 8, 2022

Hey @Atrue thanks for the response. The issue I had was the WebSocket definition doesn't get passed through the addInitScript(), so the definition is never overwritten. Is there a workaround for that you're aware of? It looks like the stuff in the init script is completely isolated.

@Atrue
Copy link
Collaborator

Atrue commented Nov 9, 2022

You need to try the examples in the documentation and make sure they works, if so you can try mocking the WebSocket the same way, you can even try it without this library, but there is no difference.
As mocking getBattery is the same as the mocking WebSocket it should work in both cases. But if the playwright has difference there you can also try using Object.defineProperty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants