Comparing Two Variables in Script

5 posts, 1 answers
  1. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 13 Feb 2012 Link to this post

    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.
  2. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 13 Feb 2012 Link to this post

    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
  3. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 13 Feb 2012 Link to this post

    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.
  4. Answer
    Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 13 Feb 2012 Link to this post

    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
  5. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 13 Feb 2012 Link to this post

    Many thanks, your solution is spot on!
Back to Top