Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
how to fetch json from API using vanilla JS through Protractor

I am trying to download distance between 2 locations from tomtom api.

Protractor will not let me use

*fetch - fetch is not defined - please use import

*import - Cannot use import statement outside of module

*when I add

{
  type: module
}

to package.json - protractor stops working, as no entire code is a module of ES

*browser.get - opens http with json data, but I cannot extract it.

Is there any other way? I tried to import json to a different file and export response.data, but the module error stops me from doing that also.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I did not manage to run node-fetch on my script as Protractor kept rejecting the import. I managed to to sort it out with require 'https'

const https = require('https');

let measureDistance = async function(pickup, dropoff) {
    let url = `https://api.tomtom.com/routing/1/calculateRoute/${pickup[0]}%2C${pickup[1]}%3A${dropoff[0]}%2C${dropoff[1]}/json?routeType=shortest&avoid=unpavedRoads&key=uwbU08nKLNQTyNrOrrQs5SsRXtdm4CXM`;
    await https.get(url, res => {
        let body = '';
          
        res.on('data', chunk => {
            body += chunk;
        });
            
        res.on("end", () => {
            try {
                let json = JSON.parse(body);
                howFar = json.routes[0].summary.lengthInMeters;
            } catch (error) {
                console.error(error.message);
            }
        }).on("error", (error) => {
            console.error(error.message);
        });
    });
};
Also I used to put require on top of the file like in Ruby, which seemed to be another issue.

about 4 years ago · Juan Pablo Isaza Report

0

Protractor is for testing angular webpages, but you can have the browser execute arbitrary javascript, but to use fetch, you need to use window

function getTomTomData() {
  //replace this with your tomtom api call, and transform the response
  return window.fetch(TOM_TOM_URL);
}

browser.executeScript(getTomTomData).then(res=> {
  //do something with the response
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!