How Can We Help?

< All Topics

As Imagium can be integrated with any automation tool on the planet. Microsoft Playwright is a no exception.

There are various ways to make API calls in Node.js. We are using Axios library in this example(However you can use your preferred library if you have one). Axios is promise based HTTP client for the browser and node.js

npm i axios

The below code use Playwright to convert screenshot into Base64 image then send Post request using Axios

const { chromium } = require("playwright");
const axios = require('axios');

var testNameImagium = "Playwright Integration";
var projectKeyImagium = '7991bf3d-060d-481d-9da4-4b885373f58b';
var uidEndPointImagium = 'http://192.168.10.13:80/api/GetUID';
var validateEndPointImagium = 'http://192.168.10.13:80/api/Validate';
var testUID;

(async()=>{

 //Using axios to make API Call
 //Get unique test ID from Imagium project
 await  axios.post(uidEndPointImagium, {TestName: testNameImagium, ProjectKey: projectKeyImagium })
    .then((res) => {
        console.log(`Status: ${res.status}`);   
        //Store unique test ID
        testUID = res.data;
        console.log('Body: ', res.data);
    }).catch((err) => {
        console.error(err);
    });

//Playwright methods to navigate and get screenshot as a Base64  
const browser = await chromium.launch({headless:false});
const page = await browser.newPage();
await page.goto("http://google.com");
const buffer = await page.screenshot({ path: 'screenshot.png', fullPage: true });

//Using axios to make API Call
//Validate the page screenshot inside Imagium
await  axios.post(validateEndPointImagium, {StepName: 'Step 1', TestRunID: testUID, ImageBase64:  buffer.toString('base64')})
.then((res) => {
    console.log(`Status: ${res.status}`);
    console.log('Body: ', res.data);
}).catch((err) => {
    console.error(err);
});

await browser.close();

})();