Skip to content

[SDK-3403] add Windows UI tests #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
with:
unityVersion: 2021.3.26f1
targetPlatform: ${{ matrix.targetPlatform }}
projectPath: './sample'
projectPath: sample
- name: Deploy to GitHub Pages
uses: JamesIves/[email protected]
if: matrix.targetPlatform == 'WebGL'
Expand Down
48 changes: 34 additions & 14 deletions .github/workflows/ui-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ concurrency:

jobs:
build:
name: Build sample game for AltTester 🛠️
name: Build ${{ matrix.targetPlatform }} for AltTester 🛠️
runs-on: ubuntu-latest-8-cores
strategy:
fail-fast: false
matrix:
include:
- targetPlatform: StandaloneOSX
buildMethod: MacBuilder.BuildForAltTester
buildPath: MacOS
- targetPlatform: StandaloneWindows64
buildMethod: WindowsBuilder.BuildForAltTester
buildPath: Windows64
steps:
- uses: actions/checkout@v3
with:
lfs: true
- uses: actions/cache@v3
with:
path: Library
key: Library-${{ hashFiles('sample/Assets/**', 'sample/Packages/**', 'sample/ProjectSettings/**') }}
key: Library-${{ matrix.targetPlatform }}-${{ hashFiles('sample/Assets/**', 'sample/Packages/**', 'sample/ProjectSettings/**') }}
restore-keys: |
Library-${{ matrix.targetPlatform }}
Library-
- name: Build project
uses: game-ci/unity-builder@v4
Expand All @@ -31,39 +42,48 @@ jobs:
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_SERIAL: ${{ secrets.UNITY_SERIAL }}
with:
targetPlatform: StandaloneOSX
targetPlatform: ${{ matrix.targetPlatform }}
projectPath: sample
buildMethod: MacBuilder.BuildForAltTester
buildMethod: ${{ matrix.buildMethod }}
customParameters: -logFile logFile.log -quit -batchmode
- name: List build directory
run: ls -R sample/Builds/
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: Build-StandaloneOSX
path: sample/Builds/MacOS
name: Build-${{ matrix.targetPlatform }}
path: sample/Builds/${{ matrix.buildPath }}
test:
name: Run UI tests on AltTester 🧪
runs-on: self-hosted
name: Run ${{ matrix.targetPlatform }} UI tests 🧪
needs: build
strategy:
matrix:
include:
- targetPlatform: StandaloneOSX
runs-on: [self-hosted, macOS]
test_script: test_mac.sh
- targetPlatform: StandaloneWindows64
runs-on: [self-hosted, windows]
test_script: test_windows.ps1
runs-on: ${{ matrix.runs-on }}
steps:
- uses: actions/checkout@v3
with:
lfs: true
- uses: actions/download-artifact@v4
with:
name: Build-StandaloneOSX
name: Build-${{ matrix.targetPlatform }}
path: sample/Tests
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: pip install -r "sample/Tests/requirements.txt"
- name: Run UI tests
env:
UNITY_APP_PATH: ${{ github.workspace }}/SampleApp.app
UNITY_APP_PATH: SampleApp.app
UNITY_APP_NAME: SampleApp
MAILSLURP_API_KEY: ${{ secrets.MAILSLURP_API_KEY }}
working-directory: sample/Tests
run: |
chmod -R 755 ${{ github.workspace }}/SampleApp.app
chmod +x test_mac.sh
./test_mac.sh
run: ./${{ matrix.test_script }}

4 changes: 0 additions & 4 deletions sample/Assets/Editor/AndroidBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
Expand Down Expand Up @@ -136,5 +134,3 @@ public static void RemoveAltFromScene(string scene)
}

}

#endif
6 changes: 1 addition & 5 deletions sample/Assets/Editor/WindowsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#if UNITY_EDITOR_WIN

using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
Expand Down Expand Up @@ -132,6 +130,4 @@ public static void RemoveAltFromScene(string scene)
}
}

}

#endif
}
6 changes: 2 additions & 4 deletions sample/Tests/src/device_code_login_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
from gmail_fetch_otp import fetch_gmail_code

EMAIL = '[email protected]'
from fetch_otp import EMAIL, fetch_code

# Add chrome.exe to environment variable
# Download chrome driver and add to environment variable
Expand Down Expand Up @@ -46,7 +44,7 @@ def main():
time.sleep(10)

print("Get OTP from Gmail...")
code = fetch_gmail_code()
code = fetch_code()
if code:
print(f"Successfully fetched OTP: {code}")
else:
Expand Down
22 changes: 22 additions & 0 deletions sample/Tests/switch-app.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
param (
[string]$appName
)

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@

$hWnd = [User32]::FindWindow([NullString]::Value, $appName)
if ($hWnd -ne [IntPtr]::Zero) {
[User32]::SetForegroundWindow($hWnd) | Out-Null
} else {
Write-Output "Window not found"
}
3 changes: 3 additions & 0 deletions sample/Tests/test_mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ app_name="${UNITY_APP_NAME:-SampleApp}"
# Capture the start time
start_time=$(date +%s)

# Set permissions for the app bundle
chmod -R 755 "$app_path"

echo "Starting Unity sample app..."
open_sample_app "$app_path"

Expand Down
154 changes: 154 additions & 0 deletions sample/Tests/test_windows.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Function to stop the Unity sample app if it's running
function Stop-SampleApp {
$process = Get-Process -Name "SampleApp" -ErrorAction SilentlyContinue
if ($process) {
Stop-Process -Id $process.Id
Write-Output "SampleApp.exe has been closed."
} else {
Write-Output "SampleApp.exe is not running."
}
Start-Sleep -Seconds 5
}

# Function to start the Unity sample app
function Start-SampleApp {
Write-Output "Starting Unity sample app..."
Start-Process -FilePath "SampleApp.exe"
Start-Sleep -Seconds 10
}

# Function to bring the Unity sample app to the foreground
function Bring-SampleAppToForeground {
$POWERSHELL_SCRIPT_PATH = "./switch-app.ps1"
Write-Output "Bringing Unity sample app to the foreground..."
powershell.exe -Command "Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process; & '$POWERSHELL_SCRIPT_PATH' -appName 'Immutable Sample'"
}

# Function to run pytest tests
function Run-Pytest {
param (
[string]$testFile
)
Write-Output "Running pytest for $testFile..."
Start-Process -FilePath "pytest" -ArgumentList $testFile -NoNewWindow -PassThru | Wait-Process
}

# Function to stop Chrome if it's running
function Stop-Chrome {
Write-Output "Stopping Chrome.."
$process = Get-Process -Name "chrome" -ErrorAction SilentlyContinue
if ($process) {
$process | ForEach-Object {
Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
}
Write-Output "All Chrome processes have been closed."
} else {
Write-Output "Chrome is not running."
}

Start-Sleep -Seconds 10
}

# Login
function Login {
param (
[string]$testFile
)
# Start Chrome for remote debugging
Write-Output "Starting Chrome..."
$chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
Start-Process -FilePath $chromePath -ArgumentList "--remote-debugging-port=9222"

# Run Python script for login
Write-Output "Running python script to login..."
$pythonProcess = Start-Process -FilePath "python" -ArgumentList "src/device_code_login_windows.py" -NoNewWindow -PassThru
Write-Output "Python script running in the background..."

Start-Sleep -Seconds 5

Bring-SampleAppToForeground

Write-Output "Running login test..."
$pytestProcess = Start-Process -FilePath "pytest" -ArgumentList $testFile -NoNewWindow -PassThru

$pythonProcess | Wait-Process

Bring-SampleAppToForeground

$pytestProcess | Wait-Process

Stop-Chrome
}

# Logout
function Logout {
# Start Chrome for remote debugging
Write-Output "Starting Chrome..."
$chromePath = (Get-Command chrome.exe).Source
Start-Process -FilePath $chromePath -ArgumentList "--remote-debugging-port=9222"

Write-Output "Running python script to logout..."
$pythonProcess = Start-Process -FilePath "python" -ArgumentList "src/device_code_logout_windows.py" -NoNewWindow -PassThru
Start-Sleep -Seconds 5

Bring-SampleAppToForeground

Write-Output "Running logout test..."
$pytestProcess = Start-Process -FilePath "pytest" -ArgumentList "test/test_mac_device_code_logout.py" -NoNewWindow -PassThru

$pythonProcess | Wait-Process

Bring-SampleAppToForeground

$pytestProcess | Wait-Process

Stop-Chrome
}

# Capture the start time
$startTime = Get-Date

# Start Unity sample app
Start-SampleApp

# Login
Login "test/test_windows.py::WindowsTest::test_1_device_code_login"

# Run IMX and zkEVM tests
Run-Pytest "test/test.py"

# Relogin
Stop-SampleApp
Start-SampleApp
Run-Pytest "test/test_windows.py::WindowsTest::test_3_device_code_relogin"

# Reconnect
Stop-SampleApp
Start-SampleApp
Run-Pytest "test/test_windows.py::WindowsTest::test_4_device_code_reconnect"

# Logout
Logout

# Connect IMX
Stop-SampleApp
Start-SampleApp
Write-Output "Connect to IMX..."
Login "test/test_windows.py::WindowsTest::test_2_device_code_connect_imx"

# Bring the Unity sample app to the foreground
Bring-SampleAppToForeground

# Logout
Logout

# Final stop of Unity sample app
Stop-SampleApp

# Capture the end time
$endTime = Get-Date

# Calculate and display the elapsed time
$elapsedTime = $endTime - $startTime
Write-Output "All tests completed."
Write-Output "Elapsed time: $($elapsedTime.TotalMinutes) minutes"
Loading