Skip to content

Instantly share code, notes, and snippets.

@devhijazi
Last active December 26, 2024 14:12
  • Select an option

Select an option

Revisions

  1. devhijazi revised this gist Dec 26, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion example.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // SCRIPT 1
    // CONECTADO COM A API DO 4DEVS GERA PESSOAS E VEICULOS ALEATORIO E DEFINE NO JSON ANTES DO REQUEST PARA GERAR DADOS ALEATÓRIOS
    const moment = require('moment');
    const cheerio = require('cheerio');
    @@ -99,7 +100,7 @@ const setOtherVariables = async () => {

    setOtherVariables();


    // SCRIPT 2
    // ACAPTURAR O ACCCESS TOKEN E USERID AUTOMATICAMENTE E DEFINIR EM COLLECTIONVARIABLES
    if (pm.response.code === 200 || pm.response.code === 201) {
    try {
  2. devhijazi created this gist Dec 26, 2024.
    122 changes: 122 additions & 0 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    // CONECTADO COM A API DO 4DEVS GERA PESSOAS E VEICULOS ALEATORIO E DEFINE NO JSON ANTES DO REQUEST PARA GERAR DADOS ALEATÓRIOS
    const moment = require('moment');
    const cheerio = require('cheerio');

    const currentDate = moment().toISOString();
    pm.collectionVariables.set("inspectionDate", currentDate);

    const fourDevsBaseUrl = "https://www.4devs.com.br/ferramentas_online.php";

    const generateRandomName = async () => {
    return new Promise((resolve, reject) => {
    pm.sendRequest({
    url: `${fourDevsBaseUrl}`,
    method: "POST",
    header: {
    "Content-Type": "application/x-www-form-urlencoded"
    },
    body: {
    mode: "urlencoded",
    urlencoded: [
    { key: "acao", value: "gerar_pessoa", disabled: false },
    { key: "sexo", value: "I", disabled: false },
    { key: "pontuacao", value: "S", disabled: false },
    { key: "idade", value: "26", disabled: false },
    { key: "cep_estado", value: "MS", disabled: false },
    { key: "txt_qtde", value: "1", disabled: false },
    { key: "cep_cidade", value: "4106", disabled: false }
    ] ao gerar nome
    }
    }, (err, response) => {
    if (err) {
    console.error("Erro:", err);
    reject(err);
    } else {
    try {
    const persons = JSON.parse(response.text());
    pm.collectionVariables.set("driver", persons[0].nome);
    resolve();
    } catch (e) {
    console.error("Erro", e);
    reject(e);
    }
    }
    });
    });
    };

    const generateVehicleData = async () => {
    return new Promise((resolve, reject) => {
    pm.sendRequest({
    url: `${fourDevsBaseUrl}`,
    method: "POST",
    header: {
    "Content-Type": "application/x-www-form-urlencoded"
    },
    body: {
    mode: "urlencoded",
    urlencoded: [
    { key: "acao", value: "gerar_veiculo", disabled: false },
    { key: "pontuacao", value: "S", disabled: false },
    { key: "estado", value: "MS", disabled: false },
    { key: "fipe_codigo_marca", value: "", disabled: false }
    ]
    }
    }, (err, response) => {
    if (err) {
    console.error("Erro:", err);
    reject(err);
    } else {
    try {
    const $ = cheerio.load(response.text());
    const licensePlate = $('#placa_veiculo').val();
    const brandModel = $('#marca').val();
    const yearModel = $('#ano').val();
    const color = $('#cor').val();

    pm.collectionVariables.set("licensePlate", licensePlate);
    pm.collectionVariables.set("brandModel", brandModel);
    pm.collectionVariables.set("yearModel", yearModel);
    pm.collectionVariables.set("color", color);
    resolve();

    } catch (e) {
    console.error("Erro:", e);
    reject(e);
    }
    }
    });
    });
    };

    const setOtherVariables = async () => {
    await generateRandomName();
    await generateVehicleData();
    pm.collectionVariables.set("inspectorSignature", "signature-hash-789");
    pm.collectionVariables.set("driverAcceptanceDate", moment().add(2, 'hours').toISOString());
    pm.collectionVariables.set("driverSignatureDate", moment().add(2.5, 'hours').toISOString());
    };

    setOtherVariables();


    // ACAPTURAR O ACCCESS TOKEN E USERID AUTOMATICAMENTE E DEFINIR EM COLLECTIONVARIABLES
    if (pm.response.code === 200 || pm.response.code === 201) {
    try {
    const response = pm.response.json();

    const accessToken = response.access_token;

    if (accessToken) {
    pm.collectionVariables.set("access_token", accessToken);
    console.log("Access token salvo na Collection com sucesso:", accessToken);
    } else {
    console.error("O campo 'access_token' não foi encontrado na resposta.");
    }
    } catch (error) {
    console.error("Erro ao processar a resposta:", error);
    }
    } else {
    console.error("A requisição falhou. Código:", pm.response.code);
    console.error("Mensagem:", pm.response.text());
    }