This is a migrated thread and some comments may be shown as answers.

Comparing Two Variables in Script

4 Answers 298 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nigel
Top achievements
Rank 2
Nigel asked on 13 Feb 2012, 11:19 AM
Hi Folks,

I want to compare two variables in my script.  The scenario is that I:
   1) extract the current value from a dropdown and bind to a variable
   2) do something (to make the value change)
   3) extract the new value from the dropdownb and bind to a different variable
   4) compare the two values.  If the values match, the test passes.

These are my two extract lines:
Extract selection 'ByText' on 'UserInterfaceThemeNameSelect' into DataBindVariable $(SchemeChosen1)
Extract selection 'ByText' on 'UserInterfaceThemeNameSelect' into DataBindVariable $(SchemeChosen2)

In code I would use an IF...ELSE statement, but I really want to avoide using code.  Is there a native solution?  (I've briefly searched the forum and didn't find anything that answered my specific query, but if you know of a thread - or a video - please tell me).

If there is no native solution, would someone help with the coding (VB)?

Thanks,
Ngiel Edwards, Transition COmputing.

4 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 13 Feb 2012, 12:14 PM
Hi Nigel,

Currently you can only compare the extracted values using a coded step.
Here's the VB code: 
Dim firstValue As String = GetExtractedValue("SchemeChosen1").ToString()
Dim secondValue As String = GetExtractedValue("SchemeChosen2").ToString()
         
If firstValue = secondValue Then
    'Do something
Else
    'Do something else
End If


Also, instead of using "If...Else" statement, you can simply use the Assert method like this:
Dim firstValue As String = GetExtractedValue("SchemeChosen1").ToString()
Dim secondValue As String = GetExtractedValue("SchemeChosen2").ToString()
         
Assert.IsTrue(firstValue = secondValue)
This will check whether the firstValue is equal to the secondValue. The test will pass and continue to the next step if they are equals and fail if not.

Hope this helps!

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nigel
Top achievements
Rank 2
answered on 13 Feb 2012, 01:16 PM
Thanks for the amazingly quick response!  Your first code solution works perfectly.  I have one additional follow-up question.

To record the result of the test I write to the log file, which is not a problem.  However, is there some way to force the Steps screens back in the Test Studio interface to reflect the result?  At the moment, my compare line ([CompareCurrentSchemeWithPrevious] : New Coded Step) always reports success because the coded step behind it functioned correctly.  Is there a way to force the little green circle with the white tick to change to a little red circle with a white 'X' if my compare?  Thanks in advance.
0
Accepted
Plamen
Telerik team
answered on 13 Feb 2012, 01:53 PM
Hello Nigel,

Here's the line of code you are looking for:
Throw New Exception("Test failed!")


You can throw a new exception whenever you want. For example in your case you can use it like this:
If firstValue = secondValue Then
    'Do something
Else
    Throw New Exception("The two values are not equal!")
End If


Kind regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nigel
Top achievements
Rank 2
answered on 13 Feb 2012, 02:01 PM
Many thanks, your solution is spot on!
Tags
General Discussions
Asked by
Nigel
Top achievements
Rank 2
Answers by
Plamen
Telerik team
Nigel
Top achievements
Rank 2
Share this question
or