Forum Discussion

Dekks's avatar
Dekks
New Contributor
7 years ago

Using Global variables in Unit Script

Trying to learn scripting, and have a simple script that opens a webpage and logs in. I have successfully gotten this to work using local variables, however I want to use persistent variables that can be reused in other scripts. So I created a new variable with a type of 'String' as such:

Name: PageLink

Type: String

Default Value: http://www.mypage.com/login/

Local Value: http://www.mypage.com/login/

 

In my script, I can open the browser to the page if I set the variable locally, i.e:

Sub Login
Dim Env
Env = "http://www.mypage.com/login/"
Browsers.Item(btIExplorer).Run Env
End Sub

That will open IE to the page set in 'Env'.

 

If I try using my global variable:

 

Sub Login
Dim Env
Dim Variables
Set Variables = ProjectSuite.Variables
Env = Variables.VariableByName("PageLink")
Browsers.Item(btIExplorer).Run Env
End Sub

Then I get the error message: "The Variables object does not contain a variable with the "PageLink" name".

 

I'm not quite sure what I'm doing wrong, or how I can tell my local variable to get its value from my global variable?

 

  • When you create a variable in the Project/ProjectSuite, It autocompletes after typing Project.Variables.

     


    Dekks wrote:

     

     

    Sub Login
    Dim Env
    Dim Variables
    Set Variables = ProjectSuite.Variables
    Env = Variables.VariableByName("PageLink")
    Browsers.Item(btIExplorer).Run Env
    End Sub 

    You do not need to go like this much lines code to get your variable. Just do like below after creating a variable.

    Browsers.Item(btIExplorer).Run ProjectSuite.Variables.PageLink

     

  • shankar_r's avatar
    shankar_r
    Community Hero

    When you create a variable in the Project/ProjectSuite, It autocompletes after typing Project.Variables.

     


    Dekks wrote:

     

     

    Sub Login
    Dim Env
    Dim Variables
    Set Variables = ProjectSuite.Variables
    Env = Variables.VariableByName("PageLink")
    Browsers.Item(btIExplorer).Run Env
    End Sub 

    You do not need to go like this much lines code to get your variable. Just do like below after creating a variable.

    Browsers.Item(btIExplorer).Run ProjectSuite.Variables.PageLink

     

    • Dekks's avatar
      Dekks
      New Contributor

      Apologies for the late response, that worked perfectly thank you. I am able to call my variable directly by saying [action] Project.Variables.PageLink.

      One thing I'm not entirely clear on is whether it fetches Default Value or Local value? (I have them both set to the same value).

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    > Set Variables = ProjectSuite.Variables

    Check if you created ProjectSuite variable or Project variable.

    In the latter case you must use

    Set Variables = Project.Variables
    • Marsha_R's avatar
      Marsha_R
      Moderator

      You can also skip the step of assigning the local variable.  Just use the global variable directly.