Using Variables as Arguments

Ask general questions here.
aunal
Posts: 17
Joined: Mon Jan 16, 2012 10:25 am

Using Variables as Arguments

Post by aunal » Wed Apr 17, 2013 8:54 am

Hello,

Now that we're using Ranorex, we are having some difficulties with values that are changing during run time. What i mean with this is;

We have a key field say X, that is unique accross the system. However, this X changes for each test case, since system generates another unique value for every new entity. We have no way to predict what this number will be until it is assigned by the process.

Is there any way to read a value from a field during execution then assign it to a global variable that is used by all the test cases under the same Test Suite. or do we need to do some extra implementation in order to do such a thing?

Thanks in advance,

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Using Variables as Arguments

Post by Support Team » Thu Apr 18, 2013 4:49 pm

Hello,

Please take a look at the code snippet below to store your value in a global parameter or a variable:
// store a value in a global parameter
TestSuite.Current.Parameters["varGlobal"] = repo.MicrosoftExcelBook1.CellA1.Element.GetAttributeValueText("Text");
Report.Info(TestSuite.Current.Parameters["varGlobal"]);

// store a value in a recording variable
varRec = repo.MicrosoftExcelBook1.CellA1.Element.GetAttributeValueText("Text");
Report.Info(varRec);
Please let me know if you need further advice.

Regards,
Markus (T)
.
Image

Shahnaz
Posts: 7
Joined: Thu Aug 27, 2015 2:25 pm

Re: Using Variables as Arguments

Post by Shahnaz » Thu Jan 14, 2016 9:08 am

Hi.. I am scripting in Vb and i tried using your code Testsuite.Current.Parameters["varGlobal"] . But i m getting sysntax error.
Instead i tried by using Testsuite.Current.Parameters("varGlobal"), i did not get any syntax error but during run time i m getting error "Object reference not set to an inatance of of an object"

Actually i m trying to replace some substring from a string. For example my main string is "Select* from table where first_name = <<<Firstname>>> And Last_name= <<<LastName>>>""

now i am extracting Substring Firstname and Lastname (enclosed in angle brackets) from the main string.
These Firstname and Lastname are my Varibales of my Recording1.Usercode whose values i m setting at runtime.

Now i want to replace these Firstname and Lastname (enclosed in angle brackets) with value of my varibales.

I thought Testsuite.Current.Parameters("Firstname") will work for me.. but it gives error


This is my code:

Public Sub main()
FirstName ="Shahnaz"
LastName="hussain"
Dim StrExpression As string= "Select* from table where firstname = <<<Firstname>>> And Lastname= <<<LastName>>>"
Dim StrLength,Iterator,SearchStartPos,StartPos,EndPos As Integer
Dim StrParamDesc,StrParamName,StrParamVal As String
Dim ArrParamVal,ArrParams As String()
ArrParamVal=split(StrExpression,"<<<")
'ArrParamVal=1
SearchStartPos=1
EndPos=1
For Iterator= 1 To ubound(ArrParamVal)
StartPos=Instr(SearchStartPos,StrExpression,"<<<")
StartPos=StartPos+3
EndPos=Instr(StartPos,StrExpression,">>>")
StrLength=EndPos-StartPos
StrParamDesc=Mid(StrExpression,StartPos,StrLength)

StrParamName=Testsuite.Current.Parameters("FirstName")

StrExpression=Replace(StrExpression,StrParamDesc,StrParamName)
SearchStartPos=StartPos+3
Next
StrExpression=Replace(StrExpression,"<<<","")
StrExpression=Replace(StrExpression,">>>","")
Report.Success(StrExpression)

End Sub

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Using Variables as Arguments

Post by Support Team » Fri Jan 15, 2016 9:10 am

Hi Shahnaz,

The code snippet above was written in C#. Since you are coding in VB.NET you have to replace the square brackets by curved brackets.

‘Testsuite.Current.Parameters("Firstname")’ just works if ‘Firstname’ is defined as a Ranorex global parameter. Since you are using Ranorex recording variables just replace ‘StrParamName = Testsuite.Current.Parameters("FirstName")’ by ‘StrParamName = FirstName’.

I hope this answers your question.

Regards
Manuel
.
Image