Search code examples
pester

How to use global variables in Quamotion Pester tests


I'm using Quamotion and Pester to test my mobile app.

Right now, I find myself having to repeat a lot of parameters (such as usernames and passwords) which I use in my tests.

Is there any way to use global variables in Quamotion/Pester tests?


Solution

  • You define global variables in Pester by prefixing them with $script:. Global variables are normally defined at the top of your script.

    For example, here's a test which logs in to your app and stores the username as a variable:

    $script:username = "myuser"
    
    Describe "My App" {
        it "Login" {
            $usernameTextField = Find-Element -xpath "//XCUIElementTypeTextField[@name='username']"
            Set-Value -elementId $usernameTextField -value $script:username
    
            $loginButton = Find-Element -xpath "//XCUIElementTypeButton[@name='Login']"
            Click-Element -elementId $loginButton
        }
    }
    

    Hope it helps!