Last active
December 26, 2024 14:12
-
-
Save devhijazi/6c4ff3e51957bff6bc661f28cb1ee4cd to your computer and use it in GitHub Desktop.
Scripts para Postman PRÉ-REQUEST E POST-REQUEST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
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(); | |
// SCRIPT 2 | |
// 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()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment