Rubio
Rubio

Reputation: 1095

Calling a GET API in Playwright fails (ECONNREFUSED)

New to Playwright, trying a simple "Hello, World" for testing a REST API. The example tests work just fine out of the box. I added an API test that calls a GET endpoint but this always results in an error.

Error: apiRequestContext.get: connect ECONNREFUSED 2606:4700:20::681a:93b:443

My example.spec.ts looks like this.

import { test, expect } from '@playwright/test';

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Click the get started link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

test('API Get Request', async ({request}) => {
  const res = await request.get('https://api.myip.com');
  expect(res.status()).toBe(200);
  const body = await res.json();
});

Any ideas why this is not working?

Upvotes: 2

Views: 448

Answers (2)

Rubio
Rubio

Reputation: 1095

I tried calling another endpoint in the domain network and that works. Even though I haven't been able to verify this, my strong suspicion is that Windows Defender Firewall is blocking Node.js JavaScript Runtime in public networks. I'm unable to test if disabling this firewall rule fixes the issue as this is controlled by a group policy. The reason page.goto works is because it's executed in the browser context.

I don't see a rule for Node.js JavaScript Runtime in the Outbound rules which confuses me. For this reason I'm not entirely sure if my suspicion is correct. In any case, if you encounter this issue, it's worth checking the Windows Defender Firewall settings.

EDIT The other possible cause that comes to mind is a proxy server. Again, I cannot test this because it's controlled by a group policy.

Upvotes: 0

Graffhyrum
Graffhyrum

Reputation: 59

ECONNREFUSED is not a playwright-specific error. For whatever reason, api.myip.com refused your request. I tried your code on my system and got a response without issue, so the endpoint is reachable with Playwright.

I'd recommend checking your playwright.config.ts and ensure tracing is enabled, rerun the test, then use npx playwright show-report to load the HTML report and open the tracefile. The trace will give you all of the network connection details, as if you had caught the request with devtools. See https://playwright.dev/docs/trace-viewer-intro for more.

Upvotes: 0

Related Questions