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

Intercept initial query requests with playwright #66

Open
krechtern opened this issue Jul 16, 2024 · 0 comments
Open

Intercept initial query requests with playwright #66

krechtern opened this issue Jul 16, 2024 · 0 comments

Comments

@krechtern
Copy link

Hey,

I have a scenario that my application directly triggers some graphql query requests when opening the page. I would like to intercept these requests with laika, but I can't find a way to get access to the laika window property before the requests are triggered. I'm using playwright for my e2e/ui tests.

My test has the following structure

test.beforeEach(async ({ page }) => {
  await page.goto('http://localhost:4200');
  await page.waitForURL(/.*\/login/);
  await page.waitForFunction(() => !!window.laika);
});

test('should display the correct data', async ({ page }) => {`
  const laikaHandle = await page.evaluateHandle(() => window.laika);
  const interceptorHandle = await laikaHandle.evaluateHandle((laika) =>
    laika.intercept({
      clientName: 'fe-client',
      operationName: 'GetData',
    })
  );
  
  await interceptorHandle.evaluate(
    (getDataInterceptor, data) => {
      getDataInterceptor.mockResult({
        result: {
          data,
        },
      });
    },
    {
      testData: [
        ...
      ],
    }
  );

...
}

The problem is now that by the time I have the laika object and can call laika.intercept, the requests is already completed and I don't receive the mocked data.

The only way I found to intercept these initial queries is to intercept them inside the onLaikaReady callback funtion:

createLazyLoadableLaikaLink({
  clientName: 'fe-client',
  startLoggingImmediately: true,
  onLaikaReady: (laika) => {
    const interceptor = laika.intercept({
      clientName: 'fe-client',
      operationName: 'GetData',
    });

    interceptor.mockResult({
      result: {
        data: {
          testData: [
            ...
          ],
        },
      },
    });
  },
}),

But this approach moves the interception logic into the actual application and I can't override this mock results from within the test file. Is there a way to set the onLaikaReady callback from within the tests?

Thanks!

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

1 participant