Search code examples
javascriptparsingload-testingk6

How can i parse img tags with k6/loadimpact?


i'm using k6 loadtesting for my work now and have a problem. How can i parse links from site? (already use official examples for href links, but dont understand how to mutate this to work with images)

for example i'm trying with this site - top-tuning.ru (this is one of examples in my task at work). i need the script to parse img links and hrefs. I'm already trying the official exaples and can parse hrefs, head titles, langAttr, but there is no way for me to do the same with img. this structures works pretty well:

const res = http.get("top-tuning.ru/"); 
const doc = parseHTML(res); 
const pageTitle = doc.find('head title').text(); 
const langAttr = doc.find('html').attr('lang');
doc.find("body").toArray().forEach(function (item) {
console.log(item.attr("href"));});

Solution

  • With the following script I get your images:

    import http from "k6/http";
    import { parseHTML } from "k6/html";
    
    export default function() {
        const res = http.get("https://top-tuning.ru/"); 
        const doc = parseHTML(res.body); 
        const pageTitle = doc.find('head title').text(); 
        const langAttr = doc.find('html').attr('lang');
        doc.find("img").toArray().forEach(function (idx) {
            console.log(idx.attr("src"));
        });
    }
    

    The import parts that I need to change are:

    1. you need protocol (https in this case) to make the request
    2. you need to provide the body not the response object to parseHTML

    After that - everything worked :)

    I hope this helps you, you can also use console.log, Object.keys(object) and JSON.stringify(object) when you don't know what is happening