Skip to content
This repository has been archived by the owner on Feb 7, 2022. It is now read-only.

HTMLExtractor converted code can't use array indexing on Selector object #8

Closed
robingustafsson opened this issue Feb 13, 2019 · 0 comments

Comments

@robingustafsson
Copy link
Member

The following JMeter JMX:

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.0 r1840935">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">1</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">1</stringProp>
        <stringProp name="ThreadGroup.ramp_time">1</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
      </ThreadGroup>
      <hashTree>
        <HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy" testname="HTTP Request" enabled="true">
          <elementProp name="HTTPsampler.Arguments" elementType="Arguments" guiclass="HTTPArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
            <collectionProp name="Arguments.arguments"/>
          </elementProp>
          <stringProp name="HTTPSampler.domain">httpbin.test.loadimpact.com</stringProp>
          <stringProp name="HTTPSampler.port"></stringProp>
          <stringProp name="HTTPSampler.protocol">https</stringProp>
          <stringProp name="HTTPSampler.contentEncoding"></stringProp>
          <stringProp name="HTTPSampler.path">/html</stringProp>
          <stringProp name="HTTPSampler.method">GET</stringProp>
          <boolProp name="HTTPSampler.follow_redirects">true</boolProp>
          <boolProp name="HTTPSampler.auto_redirects">false</boolProp>
          <boolProp name="HTTPSampler.use_keepalive">true</boolProp>
          <boolProp name="HTTPSampler.DO_MULTIPART_POST">false</boolProp>
          <stringProp name="HTTPSampler.embedded_url_re"></stringProp>
          <stringProp name="HTTPSampler.connect_timeout"></stringProp>
          <stringProp name="HTTPSampler.response_timeout"></stringProp>
        </HTTPSamplerProxy>
        <hashTree/>
        <HtmlExtractor guiclass="HtmlExtractorGui" testclass="HtmlExtractor" testname="CSS Selector Extractor" enabled="true">
          <stringProp name="HtmlExtractor.refname">pageTitle</stringProp>
          <stringProp name="HtmlExtractor.expr">body h1</stringProp>
          <stringProp name="HtmlExtractor.attribute"></stringProp>
          <stringProp name="HtmlExtractor.default">Default Title</stringProp>
          <boolProp name="HtmlExtractor.default_empty_value">false</boolProp>
          <stringProp name="HtmlExtractor.match_number">0</stringProp>
          <stringProp name="HtmlExtractor.extractor_impl"></stringProp>
        </HtmlExtractor>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

results in the following JS code:

import html from "k6/html";
import http from "k6/http";
import { check } from "k6";

const vars = {};

let output, matches, match, extract, url, opts, r;

export let options = {
  stages: [{ target: 1, duration: "1s" }]
};

export default function(data) {
  if (__VU >= 1 && __VU <= 1) {
    if (__ITER < 1) {
      url = "https://httpbin.test.loadimpact.com/html";
      opts = {
        redirects: 999
      };
      r = http.request("GET", url, "", opts);

      {
        output = "pageTitle";
        const doc = html.parseHTML(r.body);
        matches = doc.find("body h1");
        match =
          matches.size() === 0
            ? null
            : matches[Math.floor(Math.random() * matches.size())];
        extract = match ? match.text() : null;
        vars[output] = extract || "Default Title";
      }
    }
  }
}

The problem with this code is that the Selector object returned by doc.find() is not a JS Array but an Object. The correct code would instead use the Selector.eq() method:

match = matches.size() === 0 ? null : matches.eq(Math.floor(Math.random() * matches.size()));
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant