Forum Discussion

hao_bai's avatar
hao_bai
Occasional Contributor
15 years ago

Why testComplete can't replace string in htm file?



I want save or modify specified htm file after replace the string with "#FFFF99" to "#FFFFFF". Scripts as follows


===== VBScript  Begin=======

In VBScript:





sub Comp_Refile()


Dim file1,fileText1, newText1


' Reads the first text file


Set file1 = aqFile.OpenTextFile("C:\Temp\p2.htm",12,22)


fileText1 = file1.ReadAll


Log.Message(fileText1)






newText1 = Replace(fileText1, "#FFFF99", "#FFFFFF")


lala = aqFile.WriteToTextFile("C:\Temp\p2_2.htm", newText1, 22, Yes)


file1.Close


end sub






====== VBScript  end======






1. If the htm file is small size like a1.htm, script will running success,


2. But when the file P2.htm size is 163KB, the TC can't log.message of fileText1, See pic Log.1.JPG,


3. And TC can't save modified file p2_2.htm successfully, it only log "<"




TCRP.zip contained the a1.htm P2.htm and other files.




How can I deal with P2.htm in TC7.5?


2 Replies


  • Hi,





    The problem is not related to the P2.htm file's size.





    All three files use different character encoding systems: the a1.htm file has the ANSI encoding, the P2.htm file has the UTF-16 encoding, and the p2_2.htm file has the UTF-8 encoding. So, try using the modified routine:







    Sub Comp_Refile_modified()

      Dim file1, fileText1, newText1

        ' Reads the first text file

        Set file1 = aqFile.OpenTextFile("C:\Temp\p2.htm", aqFile.faReadWrite, aqFile.ctUnicode)

        fileText1 = file1.ReadAll

        Call Log.Message(fileText1)





        newText1 = Replace(fileText1, "#FFFF99", "#FFFFFF") 

        Call aqFile.WriteToTextFile("C:\Temp\p2_2.htm", newText1, aqFile.ctUTF8, Yes)

        Call file1.Close      

    End Sub