1

I'm starting to learn typescript and Playwright, and I need to make a GET call inside a Playwright test.

This call is for creating a session and will return a json with a SESSION_ID that I'll include later in the URL.

Thanks, Jose.

I've tried by adding jquery dependences: npm i --save-dev @types/jquery, but running this code:

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

test('test', async ({ page }) => {
  var settings = {
    "url": "http://xxxxxx.yyyyyy.com:8080/ords/hero_ords/heroapps/v1/mysession",
    "method": "GET",
    "timeout": 0,
  };

  $.ajax(settings).done(function (response) {
    console.log(response);
  });
});

I get this error: ReferenceError: $ is not defined

Thanks, Jose.

  • 1
    `$.ajax` is a jQuery call. JavaScript and TypeScript have the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#uploading_json_data) that does the same thing without requiring extra libraries, eg `var resp=await fetch("http://....");` – Panagiotis Kanavos Mar 29 '23 at 08:58

1 Answers1

0

$.ajax is a jQuery call. JavaScript and TypeScript have the Fetch API that does the same thing without requiring extra libraries and works with `await:

var response=await fetch(url);
var data=await response.json();

Running on Node

Node.js versions before 18 didn't support the Fetch API. Trying to use it results in ReferenceError: fetch is not defined. The solution, described in this SO question is to either use a newer Node version or install the node-fetch package with:

npm install node-fetch

The best option is to use Node 18. By now, Node 18 is the only version in active support. It's an LTS version supported until 2025. Node 17 is already out of support and the previous LTS version, 16, is in Security support which expires in September 2023

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Thanks a lot for your quick support. Using fetch I get "ReferenceError: fetch is not defined" – JoseAróstegui Mar 29 '23 at 09:15
  • How are you running the test? `fetch` is a built-in JavaScript function but Node.js versions before 18 didn't support it. There's a related [SO question](https://stackoverflow.com/questions/48433783/referenceerror-fetch-is-not-defined). The solution is to either use a recent Node version or add the `node-fetch` package – Panagiotis Kanavos Mar 29 '23 at 09:28
  • Upgraded from node 14 to node 18.15.0 solved my issue. Thanks a lot! – JoseAróstegui Mar 29 '23 at 09:40
  • How do I mark this thread as answered by you? (I'm pretty newbie with StackOverflow) – JoseAróstegui Mar 29 '23 at 09:41