Ask a Question

Base64 encoded string

SOLVED
Philip_Baird
Community Expert

Base64 encoded string


Hi, as part of a XHR web service call I am testing, I am required to send basic Authorization in the header encoded as Base64.


 


I couldn't find a way to do this from a Test Complete object so came up with the following using the dotNET object:


 


function getEncodedCredentials( userName, password ) {


  var textBytes = dotNET.System_Text.Encoding.UTF8.GetBytes_2( userName + ":" + password );


  var encodedText = dotNET.System.Convert.ToBase64String( textBytes ); 


  


  return encodedText;


}


 


which is used such as:


 


Log.Message( getEncodedCredentials( "fred", "fl1ntst0ne" ) );


 


My question is this, is their another way to do this in Test Complete that does not require a call to the CLR Bridge?


 


Regards,


Phil Baird

1 ACCEPTED SOLUTION

Accepted Solutions
AlexKaras
Community Hero

RE: Base64 encoded string

Hi Phil,



I used .Net-based approach in several projects and it worked fine.

However, the following request http://www.google.com/search?q=base64+encode+string+jscript+vbscript returned quite a few results the first of which http://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript looks quite interesting (though I did not verified what people have talked there about).
Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

9 REPLIES 9
AlexKaras
Community Hero

RE: Base64 encoded string

Hi Phil,



I used .Net-based approach in several projects and it worked fine.

However, the following request http://www.google.com/search?q=base64+encode+string+jscript+vbscript returned quite a few results the first of which http://stackoverflow.com/questions/496751/base64-encode-string-in-vbscript looks quite interesting (though I did not verified what people have talked there about).
Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================

View solution in original post

Philip_Baird
Community Expert

RE: Base64 encoded string


Thanks Alexei, after a quick look I think I will stick with the .Net approach.


 


Phil

AlexKaras
Community Hero

RE: Base64 encoded string

The reason for my decision was exactly the same 😉
Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Afshal
New Contributor

Re: Base64 encoded string

Hi, Can i get the same equivalent script for decoding a string. 

AlexKaras
Community Hero

Re: Base64 encoded string

Hi,

 

If your test project is JScript/Java Script/C#Script-based, then the code from https://scotch.io/tutorials/how-to-encode-and-decode-strings-with-base64-in-javascript can be used as it is.

For the project that uses some other language you can put the referenced code into Script Extension and use it from there.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Afshal
New Contributor

Re: Base64 encoded string

Thanks, But i need the script in this format for decoding too


var string='test';
function main()
{
  getEncodedValue(string);
}
function getEncodedValue(string)
{
  var textBytes = dotNET.System_Text.Encoding.UTF8.GetBytes_2(string);
  var encodedText = dotNET.System.Convert.ToBase64String(textBytes);
//  return encodedText;
  Log.Message(encodedText);
}
 

 
AlexKaras
Community Hero

Re: Base64 encoded string

Hi,

 

The code I have referenced does not use .Net but is pure Java-based.

If you declare it as a public variable (as in the code sample), then use Base64.encode() to encode the string and Base64.decode() to decode encoded one.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
RohitSen
Senior Member

Re: Base64 encoded string

Hey Alex,

 

I need a Function containing only Decoding  part in java Script. I refered your Link Containing both encoding and decoding functions together and I'm not able to divide them seperately So can  you help me with Seperate functions for both encoding and decode functionalities.

 

Regards,

Rohit Sen

AlexKaras
Community Hero

Re: Base64 encoded string

Hi Rohit,

 

The referred link contains minified code (with extra spaces and indentations removed). You may expand it with the help of some Java Script formatter (or manually) and remove the part that you don't need.

 

P.S. Out of curiosity: is it really a problem if encoding function is present in the code?

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
cancel
Showing results for 
Search instead for 
Did you mean: