Forum Discussion

maximojo's avatar
maximojo
Frequent Contributor
12 years ago

Integrate HTML tidy or other malformed HTML fixer upper

Hi all,



Does anyone know how to integrate HTML tidy (http://tidy.sourceforge.net/) into TestComplete for use in cleaning up HTML through jscript (long story on why I have to do this)?



The simplest (maybe?) answer would be if there was a javascript file version i could modify and add to my project but I can't find one.



Any ideas on the best way? Or perhaps another HTML cleaner upper? :)



Cheers

Mark

2 Replies

  • maximojo's avatar
    maximojo
    Frequent Contributor
    I worked out something where I pass in a file to the tidy.exe and run it from the commandline in script. Kinda ugly but it works ok.
  • maximojo's avatar
    maximojo
    Frequent Contributor
    Here's the function if anyone is keen. Not super advanced. Also there are a TON of parameters to tidy.exe which you should look at and customize for your use.



    The TimeHelper object is a custom thing I wrote to help wait for X amount of time. I can post the faux jscript class if anyone wants it.



    There are probably more errors I'm not handling but... well there you are :)






    function CleanHTML_HTMLTidy(html)

    {

      var WshShell = new ActiveXObject("WScript.Shell");

       

      if(WshShell == null)

      {

        Log.Error("CleanHTML - error creating WshShell object! Aborting!");

        return false;

      }

      var filePath = Project.Path + "tempHTML.html";

      if(!aqFile.WriteToTextFile(filePath, html, aqFile.ctUTF8, true))

      {

        Log.Error("CleanHTML - error writing temp file! Cleaning failed!")

        return false;

      }

       

      var runObj = Runner.CallObjectMethodAsync(WshShell, "Run",

                            "U:\\Resources\\TestComplete\\SharedFiles\\html_tidy\\tidy.exe -m --tidy-mark no " + filePath, 1, false);

     

        // wait for 10 seconds max

      TimeHelper.Init(10000);

     

      while(!runObj.Completed && !TimeHelper.TimeElapsed())

        Delay(500);

       

      if(TimeHelper.TimeElapsed())

      {

        Log.Error("CleanHTML - Timeout waiting for file to be processed! Aborting!");

        return false;

      }

     

      var cleanHTML = aqFile.ReadWholeTextFile(filePath, aqFile.ctUTF8);

     

      if(aqString.GetLength(cleanHTML) < 1)

      {

        Log.Error("CleanHTML - Cleaned file has 0 size! Aborting!");

        return false; 

      }

     

      return cleanHTML; 

    }