cancel
Showing results for 
Search instead for 
Did you mean: 

Base64 encoded string

SOLVED
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
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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
9 REPLIES 9
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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Community Expert

RE: Base64 encoded string


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


 


Phil

Highlighted
Community Hero

RE: Base64 encoded string

The reason for my decision was exactly the same Smiley Wink
Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Contributor

Re: Base64 encoded string

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

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
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);
}
 

 
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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
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

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Here?
Join us and watch the welcome video:
Join the September Hub-bub to show off, learn and win