Ask a Question

How to convert a string to a Object

mazhar555
Contributor

How to convert a string to a Object

Hi, i am trying to get an object from excel sheet, obvoiusly it will return a string value, now i want to convert it into Object so that i can use it in my script. is there any way to do so?
-
Muhammad Mazhar Mahmood
Certified Software Tester (CSTE)
8 REPLIES 8
AlexKaras
Community Hero

RE: How to convert a string to a Object

Hi Muhammad,



You should use either Eval() (for VBScript and, possibly, for JScript/C#Script/C++Script) or evaluate() (for DelphiScript) function provided by the corresponding scripting language that you are using for the tests. See the documentation for the scripting language for more details. (Note: C#Script and C++Script are actually JScript with modified syntax, so you should read JScript documentation is you use any of these languages.)

For the VBScript the code will be like this:

strObject = "Sys.Process(""Notepad"")"

Set obj = Eval(strObject)



For DelphiScript the code will be like this:

strObject := 'Sys.Process(''Notepad'')'

obj := evaluate(strObject)
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
================================
mazhar555
Contributor

RE: How to convert a string to a Object

Thanks, i am using VB Script and new to automation. What if i want to do the same in KeywordTest?
-
Muhammad Mazhar Mahmood
Certified Software Tester (CSTE)
shashidharsls
New Contributor

RE: How to convert a string to a Object

Hi Team,



 I want to set_color of a object. I have to convert the hex color pattern (Ex: "000080") to object to pass it as a parammeter to PathOfObject.set_SelectedColor(). Please help me to do this conversion in Vb Scripting.



Note:  PathOfObject.set_SelectedColor "#000080" 

It shows the following error if I run the above line of code.

Object of type "System.String" cannot be converted to type of "System.Windows.media.Color type"
adithyasekar
Occasional Contributor

Re: RE: How to convert a string to a Object

How should we achieve the same using python in Testcomplete. I understand evaluate() and Eval() can be used for jscript and delphy.

Can somebody please help on this. its an urgent requirement. 

tristaanogre
Community Hero

Re: RE: How to convert a string to a Object

Quick google search found that Python supports an "eval()" method as well.  have you tried that?


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
adithyasekar
Occasional Contributor

Re: RE: How to convert a string to a Object

yes . but it didnt work. 

Query in detail:  I trying to automate notepad. 

enter some value in notepad.

the workspace path is = Aliases.notepad2.wndNotepad 

if am trying to enter the value as = Aliases.notepad2.wndNotepad.Keys("Hello world"), then it is entering the word . but when i put the path in excel and trying get the path from excel and trying to enter value like = DDT.CurrentDriver.Value["columnName"].Keys("Hello world") >> then its throwing error as string doesnt have keys attribute.

 

please help in resolving this.

tristaanogre
Community Hero

Re: RE: How to convert a string to a Object

As mentioned, you need to use eval.  I don't know Python so the syntax may be off.  But, this is what I would do

 

var myObject;

myObject = eval(DDT.CurrentDriver.Value["columnName"]);
myObject.Keys('Hello World');

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
adithyasekar
Occasional Contributor

Re: RE: How to convert a string to a Object

Thank you Sir. Appreciate  your help, That worked. 

cancel
Showing results for 
Search instead for 
Did you mean: