{"id":11142,"date":"2024-06-13T07:10:58","date_gmt":"2024-06-13T11:10:58","guid":{"rendered":"https:\/\/www.redline13.com\/blog\/?p=11142"},"modified":"2024-06-13T07:10:59","modified_gmt":"2024-06-13T11:10:59","slug":"dynamically-parse-html-with-k6","status":"publish","type":"post","link":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/","title":{"rendered":"Dynamically Parse HTML with k6"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11143\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/06\/word-image-11142-1.png\" alt=\"Dynamically Parse HTML with k6\" width=\"288\" height=\"216\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/06\/word-image-11142-1.png 400w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/06\/word-image-11142-1-300x225.png 300w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><\/p>\n<p><a id=\"post-11142-_nmjd8q27rprj\"><\/a>For certain <a href=\"https:\/\/k6.io\/\">k6<\/a> tests, we may want to vary the action taken based on content presented on the target test page.\u00a0 In order to accomplish this, we can use HTML elements to drive conditional logic within a k6 test plan.\u00a0 In this brief post, we will show you how to use k6 to dynamically evaluate <a href=\"https:\/\/en.wikipedia.org\/wiki\/HTML_element\" target=\"_blank\" rel=\"noopener\">HTML elements<\/a> on a target test page.<\/p>\n<h3>Importing Required Modules<\/h3>\n<p>To read content from the page, we will need to import the\u00a0<a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/examples\/parse-html\/\" target=\"_blank\" rel=\"noopener\"><code>parseHTML()<\/code><\/a> function from the built-in <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/javascript-api\/#k6html\" target=\"_blank\" rel=\"noopener\"><code>k6\/html<\/code><\/a> module:<\/p>\n<pre><span style=\"color: #800000;\"><strong>import { parseHTML } from 'k6\/html';<\/strong><\/span><\/pre>\n<h3>Parsing HTML Elements<\/h3>\n<p>In order for us to obtain the HTML content from the target test page we will use the <code>parseHTML()<\/code> function to evaluate the response body:<\/p>\n<pre><span style=\"color: #800000;\"><strong>const response = http.get('https:\/\/k6.io');\n<\/strong><\/span><span style=\"color: #800000;\"><strong>const document = parseHTML(response.body);<\/strong><\/span><\/pre>\n<p>Using the <a href=\"https:\/\/grafana.com\/docs\/k6\/latest\/javascript-api\/k6-html\/selection\/selection-find\/\" target=\"_blank\" rel=\"noopener\"><code>find()<\/code><\/a> method, we can select the page title element from the parsed HTML content:<\/p>\n<pre><span style=\"color: #800000;\"><strong>const title = parseHTML(document).find('head title').text();<\/strong><\/span><\/pre>\n<p>At this point, the variable \u201c<code>title<\/code>\u201d contains the title of the target test page.\u00a0 We can use further conditional logic to evaluate next steps for our test based on the title content.<\/p>\n<hr \/>\n<p>Did you know that RedLine13 offers a full featured, time limited free trial?\u00a0 <a href=\"https:\/\/www.redline13.com\/Service\" target=\"_blank\" rel=\"noopener\">Sign up now<\/a>, and start testing today!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For certain k6 tests, we may want to vary the action taken based on content presented on the target test page.\u00a0 In order to accomplish this, we can use HTML elements to drive conditional logic within a k6 test plan.\u00a0 In this brief post, we will show you how to use k6 to dynamically evaluate HTML elements on a target test page. Importing Required Modules To read content from the page, we will need to import<a class=\"more-link\" href=\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":11,"featured_media":11143,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[714,596,318,716,715,424,426],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-dkoziel","4":"post-11142","6":"format-standard","7":"has-post-thumbnail","8":"category-blog","9":"category-uncategorized","10":"post_tag-html","11":"post_tag-k6","12":"post_tag-load-testing","13":"post_tag-page-content","14":"post_tag-parse-html","15":"post_tag-redline13","16":"post_tag-redline13-customer"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Dynamically Parse HTML with k6 - RedLine13<\/title>\n<meta name=\"description\" content=\"In this brief post, we will show you how to use k6 to dynamically parse HTML elements on a target test page.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dynamically Parse HTML with k6 - RedLine13\" \/>\n<meta property=\"og:description\" content=\"In this brief post, we will show you how to use k6 to dynamically parse HTML elements on a target test page.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\" \/>\n<meta property=\"og:site_name\" content=\"RedLine13\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-13T11:10:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-13T11:10:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/06\/word-image-11142-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"David Koziel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Koziel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\"},\"author\":{\"name\":\"David Koziel\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\"},\"headline\":\"Dynamically Parse HTML with k6\",\"datePublished\":\"2024-06-13T11:10:58+00:00\",\"dateModified\":\"2024-06-13T11:10:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\"},\"wordCount\":180,\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"keywords\":[\"HTML\",\"k6\",\"Load Testing\",\"page content\",\"parse HTML\",\"RedLine13\",\"redline13 customer\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\",\"name\":\"Dynamically Parse HTML with k6 - RedLine13\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\"},\"datePublished\":\"2024-06-13T11:10:58+00:00\",\"dateModified\":\"2024-06-13T11:10:59+00:00\",\"description\":\"In this brief post, we will show you how to use k6 to dynamically parse HTML elements on a target test page.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.redline13.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dynamically Parse HTML with k6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"name\":\"RedLine13\",\"description\":\"(Almost) Free Load Testing in the Cloud\",\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.redline13.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\",\"name\":\"RedLine13\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"contentUrl\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"width\":300,\"height\":68,\"caption\":\"RedLine13\"},\"image\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\",\"name\":\"David Koziel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"caption\":\"David Koziel\"},\"url\":\"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dynamically Parse HTML with k6 - RedLine13","description":"In this brief post, we will show you how to use k6 to dynamically parse HTML elements on a target test page.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/","og_locale":"en_US","og_type":"article","og_title":"Dynamically Parse HTML with k6 - RedLine13","og_description":"In this brief post, we will show you how to use k6 to dynamically parse HTML elements on a target test page.","og_url":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/","og_site_name":"RedLine13","article_published_time":"2024-06-13T11:10:58+00:00","article_modified_time":"2024-06-13T11:10:59+00:00","og_image":[{"width":400,"height":300,"url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/06\/word-image-11142-1.png","type":"image\/png"}],"author":"David Koziel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"David Koziel","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/#article","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/"},"author":{"name":"David Koziel","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20"},"headline":"Dynamically Parse HTML with k6","datePublished":"2024-06-13T11:10:58+00:00","dateModified":"2024-06-13T11:10:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/"},"wordCount":180,"publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"keywords":["HTML","k6","Load Testing","page content","parse HTML","RedLine13","redline13 customer"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/","url":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/","name":"Dynamically Parse HTML with k6 - RedLine13","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/#website"},"datePublished":"2024-06-13T11:10:58+00:00","dateModified":"2024-06-13T11:10:59+00:00","description":"In this brief post, we will show you how to use k6 to dynamically parse HTML elements on a target test page.","breadcrumb":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.redline13.com\/blog\/2024\/06\/dynamically-parse-html-with-k6\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.redline13.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Dynamically Parse HTML with k6"}]},{"@type":"WebSite","@id":"https:\/\/www.redline13.com\/blog\/#website","url":"https:\/\/www.redline13.com\/blog\/","name":"RedLine13","description":"(Almost) Free Load Testing in the Cloud","publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.redline13.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.redline13.com\/blog\/#organization","name":"RedLine13","url":"https:\/\/www.redline13.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","contentUrl":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","width":300,"height":68,"caption":"RedLine13"},"image":{"@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20","name":"David Koziel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","caption":"David Koziel"},"url":"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/comments?post=11142"}],"version-history":[{"count":4,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11142\/revisions"}],"predecessor-version":[{"id":11148,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11142\/revisions\/11148"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media\/11143"}],"wp:attachment":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media?parent=11142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/categories?post=11142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/tags?post=11142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}