javascripthtmlfrisby.js

Accessing contents of HTML response with javascript


I need to automate some API tests using frisby.js and javascript, but I am having trouble accessing some data of a HTML response, most propabily because the response is not in JSON format.

Below is my code

var frisby = require('frisby');
frisby.create('Get some information')
    .get(someUrl)
    .after(function (err, res, body) {
        console.log(body);
    })
    .toss()

Below is the result of console.log(body);function

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <title>MDpay default response template for web</title>
</head>
<body OnLoad="OnLoadEvent();" >
<form name="downloadForm"
      action="https://someurl"
      method="POST">
  <input type="hidden"
         name="PaReq"
         value="eJxVUdtuwjAM/ZWKDyBJCWuLjCUuk8YDE2JMYk8opN6ogBTSdIK/X1LK2PISn5PYxz6G1c4STd9I15YQ5lRV6ouiIh92krgnZQdhMVrSGeGbbFWUBkWXd2Ngd+hTrN4p4xCUPo9nrygznkgOrIVwJDubIudc8HCA3Qgw6kg42m+V2W960828zOmwWVHlgDVPoMvaOHvFJPN6dwC1PeDOudOAMWABAHu0sKhDVPnkS5HjgtPc6Zyvx5PLm5YFlcXabd8/1TMfAgs/IFeOMOYi8d09RaI/4PFAJsAaHtQxqOJq+RH1eTf03jJwCkKjG+g3U/1lwLtpyegrZjLzvd8R0OVUGvI//ES/MeRUaYy8aLiBPYaYvARXtfN2xWkvTkUabG1wKFV4P0QqbrUCABYyWLsx1m7TR/+2/APkDqQg">
  <input type="hidden"
         name="TermUrl" value="https:someurl">
  <input type="hidden"
         name="MD"
         value="402277:56F8FBC5FC36742423302C8EFF56C796E7B123FC61132EA2AD89A389A02C4093:4243:##100100000">
  <!-- To support javascript unaware/disabled browsers -->
  <div style="text-align: center;">
    <img src="templates/preloader.gif"/><br/>
    <noscript>
      <center>Please click the submit button below.<br>
        <input type="submit" name="submit" value="Submit"></center>
    </noscript>
  </div>
</form>

<SCRIPT LANGUAGE="Javascript" >
  function OnLoadEvent() {
    document.downloadForm.submit();
  }
</SCRIPT>
</body>
</html>

My question is do you know a way to reach value's of PaReq, TermUrl and MD HTML tags.

Thanks


Solution

  • I had to do some string operations to reach the values, below is the code which worked for accessing these values

    var pareq = body.substr(body.search('name="PaReq"'),600);
    pareq = pareq.substring(pareq.indexOf('value="')+7,pareq.indexOf('">'));
    
    var termUrl = body.substr(body.search('name="TermUrl"'),200);
    termUrl = termUrl.substring(termUrl.indexOf('value="')+7,termUrl.indexOf('">'));
    
    var md = body.substr(body.search('name="MD"'),250);
    md = md.substring(md.indexOf('value="')+7,md.indexOf('">'));
    

    If you have better ways to solve this, please let me know, thanks!