Skip to content

Commit

Permalink
test: fix ios build and ui test script
Browse files Browse the repository at this point in the history
  • Loading branch information
nattb8 committed Nov 21, 2024
1 parent 8e58151 commit 1a70728
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 71 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/ui-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ jobs:
- targetPlatform: StandaloneWindows64
runs-on: [self-hosted, windows]
test_script: pytest -xs test/test_windows.py::WindowsTest
# - targetPlatform: Android
# runs-on: [ self-hosted, macOS ]
# test_script: browserstack-sdk pytest -s ./test/test_android.py --browserstack.config "browserstack.android.yml"
- targetPlatform: Android
runs-on: [ self-hosted, macOS ]
test_script: browserstack-sdk pytest -s ./test/test_android.py --browserstack.config "browserstack.android.yml"
concurrency:
group: test-${{ matrix.targetPlatform }}
runs-on: ${{ matrix.runs-on }}
Expand Down Expand Up @@ -110,26 +110,26 @@ jobs:
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
working-directory: sample/Tests
run: ${{ matrix.test_script }}
# test-ios:
# name: Run iOS UI tests 🧪
# runs-on: [ self-hosted, macOS ]
# steps:
# - uses: actions/checkout@v3
# with:
# lfs: true
# - name: build iOS app
# working-directory: sample
# run: ./build_ios.sh
# - 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:
# MAILSLURP_API_KEY: ${{ secrets.MAILSLURP_API_KEY }}
# BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
# BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
# working-directory: sample/Tests
# run: browserstack-sdk pytest -s ./test/test_ios.py --browserstack.config "browserstack.ios.yml"
test-ios:
name: Run iOS UI tests 🧪
runs-on: [ self-hosted, macOS ]
steps:
- uses: actions/checkout@v3
with:
lfs: true
- name: Build iOS app
working-directory: sample
run: ./build_ios.sh
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: pip install -r "sample/Tests/requirements-mobile.txt"
- name: Run UI tests
env:
MAILSLURP_API_KEY: ${{ secrets.MAILSLURP_API_KEY }}
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}
working-directory: sample/Tests
run: browserstack-sdk pytest -s ./test/test_ios.py --browserstack.config "browserstack.ios.yml"

19 changes: 19 additions & 0 deletions sample/Assets/Editor/MobileBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ private static void BuildPlayer(string defaultBuildPath, BuildOptions buildOptio
{
string buildPath = GetBuildPathFromArgs(defaultBuildPath);

if (platform == BuildTarget.iOS)
{
string bundleIdentifier = GetBundleIdentifierFromArgs();
PlayerSettings.applicationIdentifier = bundleIdentifier;
}

BuildPlayerOptions buildPlayerOptions = CreateBuildPlayerOptions(buildPath, buildOptions, platform);

if (setupForAltTester)
Expand Down Expand Up @@ -79,6 +85,19 @@ private static BuildTarget GetPlatformFromArgs()
return BuildTarget.Android; // Default to Android if no platform is specified
}

private static string GetBundleIdentifierFromArgs()
{
string[] args = Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--bundleIdentifier" && i + 1 < args.Length)
{
return args[i + 1];
}
}
return "com.immutable.Immutable-Sample";
}

private static string GetAltTesterHostFromArgs()
{
string[] args = Environment.GetCommandLineArgs();
Expand Down
4 changes: 2 additions & 2 deletions sample/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ PlayerSettings:
applicationIdentifier:
Android: com.immutable.ImmutableSample
Standalone: com.immutable.unitysample
iPhone: com.immutable.Immutable-Sample
iPhone: com.immutable.Immutable-Sample-GameSDK
buildNumber:
Standalone: 0
VisionOS: 0
iPhone: 0
tvOS: 0
overrideDefaultApplicationIdentifier: 0
overrideDefaultApplicationIdentifier: 1
AndroidBundleVersionCode: 1
AndroidMinSdkVersion: 26
AndroidTargetSdkVersion: 33
Expand Down
2 changes: 1 addition & 1 deletion sample/Tests/browserstack.ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ source: pytest-browserstack:sample-sdk:v1.0
# Set `app` to define the app that is to be used for testing.
# It can either take the id of any uploaded app or the path of the app directly.
#app: ./WikipediaSample.apk
app: ../build/output/iOS/IPA/Payload.ipa #For running local tests
app: ./Payload.ipa #For running local tests

# =======================================
# Platforms (Browsers / Devices to test)
Expand Down
12 changes: 6 additions & 6 deletions sample/Tests/test/test_ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

from alttester import AltDriver, By

from test import TestConfig

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'src'))
from fetch_otp import EMAIL, fetch_code
from fetch_otp import fetch_code

# To run this test on an actual Android device: appium --base-path /wd/hub --allow-insecure chromedriver_autodownload
class TestBase(unittest.TestCase):
Expand All @@ -24,9 +26,7 @@ class TestBase(unittest.TestCase):
def setUpClass(cls):
# https://appium.github.io/appium-xcuitest-driver/latest/preparation/real-device-config/
options = XCUITestOptions()
options.app = "./Payload.ipa"
options.show_xcode_log = True
options.xcode_org_id = "APPLE_TEAM_ID" # Replace with Apple Team ID
options.auto_accept_alerts = True

cls.appium_driver = webdriver.Remote('https://hub-cloud.browserstack.com/wd/hub/', options=options)
Expand Down Expand Up @@ -75,14 +75,14 @@ def test_1_pkce_login(self):
# Found email
target_context = context

email_field.send_keys("[email protected]")
email_field.send_keys(TestConfig.EMAIL)
submit_button = driver.find_element(by=AppiumBy.XPATH, value="//form/div/div/div[2]/button")
submit_button.click()

time.sleep(10) # Wait for OTP

code = fetch_gmail_code()
assert code, "Failed to fetch OTP from Gmail"
code = fetch_code()
assert code, "Failed to fetch OTP from MailSlurp"
print(f"Successfully fetched OTP: {code}")

# Unlike on Android, each digit must be entered into a separate input field on iOS.
Expand Down
98 changes: 61 additions & 37 deletions sample/build_ios.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/bin/bash

PATH_UNITY="/Applications/Unity/Unity.app/Contents/MacOS/Unity"
PATH_UNITY="/Applications/Unity/Hub/Editor/2021.3.26f1/Unity.app/Contents/MacOS/Unity"
PATH_TO_UNITY_SDK_SAMPLE_APP="./"
BUILD_METHOD="MobileBuilder.BuildForAltTester"
APPLE_TEAM_ID=""
APPLE_TEAM_ID="54XMLXPF98"

# Define the build paths
BUILD_XCODE_PATH="$(pwd)/build/output/iOS/Xcode"
BUILD_ARCHIVE_PATH="$(pwd)/build/output/iOS/Archive"
BUILD_IPA_PATH="$(pwd)/build/output/iOS/IPA"
DERIVED_DATA_PATH="$(pwd)/build/output/iOS/DerivedData"

# Function to clear a directory
clear_directory() {
Expand All @@ -25,9 +26,15 @@ clear_directory() {
clear_directory "$BUILD_XCODE_PATH"
clear_directory "$BUILD_ARCHIVE_PATH"
clear_directory "$BUILD_IPA_PATH"
clear_directory "$DERIVED_DATA_PATH"

mkdir -p "$BUILD_XCODE_PATH"
mkdir -p "$BUILD_ARCHIVE_PATH"
mkdir -p "$BUILD_IPA_PATH"
mkdir -p "$DERIVED_DATA_PATH"

# Unity build command
UNITY_COMMAND="$PATH_UNITY -projectPath \"$PATH_TO_UNITY_SDK_SAMPLE_APP\" -executeMethod $BUILD_METHOD -logFile logFile.log -quit -batchmode --buildPath \"$BUILD_XCODE_PATH\" --platform iOS"
UNITY_COMMAND="$PATH_UNITY -projectPath \"$PATH_TO_UNITY_SDK_SAMPLE_APP\" -executeMethod $BUILD_METHOD -logFile logFile.log -quit -batchmode --buildPath \"$BUILD_XCODE_PATH\" --platform iOS --bundleIdentifier com.immutable.Immutable-Sample-GameSDK"
echo "Running command: $UNITY_COMMAND"

# Execute the Unity build command
Expand All @@ -39,39 +46,56 @@ if [ $? -ne 0 ]; then
exit 1
fi

# Build and archive project
xcodebuild -project "$(pwd)/build/output/iOS/Xcode/Unity-iPhone.xcodeproj" \
echo "Building app..."
xcodebuild clean build \
-project "$(pwd)/build/output/iOS/Xcode/Unity-iPhone.xcodeproj" \
-scheme Unity-iPhone \
-archivePath "$(pwd)/build/output/iOS/Archive/Unity-iPhone.xcarchive" \
-configuration Release \
-destination "generic/platform=iOS" \
DEVELOPMENT_TEAM="$APPLE_TEAM_ID" \
CODE_SIGN_STYLE=Automatic \
archive

# Create ExportOptions.plist with the correct APPLE_TEAM_ID
EXPORT_OPTIONS_PATH="$(pwd)/build/output/iOS/Archive/ExportOptions.plist"

cat <<EOF > "$EXPORT_OPTIONS_PATH"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>development</string> <!-- Use 'ad-hoc' or 'app-store' as needed -->
<key>teamID</key>
<string>$APPLE_TEAM_ID</string>
<key>signingStyle</key>
<string>automatic</string> <!-- Use automatic signing -->
<key>compileBitcode</key>
<false/>
<key>thinning</key>
<string>&lt;none&gt;</string>
</dict>
</plist>
EOF

# Generate .ipa file
xcodebuild -exportArchive \
-archivePath "$(pwd)/build/output/iOS/Archive/Unity-iPhone.xcarchive" \
-exportPath "$(pwd)/build/output/iOS/IPA" \
-exportOptionsPlist "$EXPORT_OPTIONS_PATH"
-allowProvisioningUpdates \
-derivedDataPath "$(pwd)/build/output/iOS/DerivedData"

mkdir -p "$(pwd)/build/output/iOS/IPA/Payload"

mv "$(pwd)/build/output/iOS/DerivedData/Build/Products/ReleaseForRunning-iphoneos/ImmutableSample.app" "$(pwd)/build/output/iOS/IPA/Payload"

pushd "$(pwd)/build/output/iOS/IPA" && zip -r Payload.zip Payload && popd

mv "$(pwd)/build/output/iOS/IPA/Payload.zip" "$(pwd)/Tests/Payload.ipa"

# # Build and archive project
# xcodebuild -project "$(pwd)/build/output/iOS/Xcode/Unity-iPhone.xcodeproj" \
# -scheme Unity-iPhone \
# -archivePath "$(pwd)/build/output/iOS/Archive/Unity-iPhone.xcarchive" \
# -configuration Release \
# DEVELOPMENT_TEAM="$APPLE_TEAM_ID" \
# CODE_SIGN_STYLE=Automatic \
# archive

# # Create ExportOptions.plist with the correct APPLE_TEAM_ID
# EXPORT_OPTIONS_PATH="$(pwd)/build/output/iOS/Archive/ExportOptions.plist"

# cat <<EOF > "$EXPORT_OPTIONS_PATH"
# <?xml version="1.0" encoding="UTF-8"?>
# <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
# <plist version="1.0">
# <dict>
# <key>method</key>
# <string>development</string> <!-- Use 'ad-hoc' or 'app-store' as needed -->
# <key>teamID</key>
# <string>$APPLE_TEAM_ID</string>
# <key>signingStyle</key>
# <string>automatic</string> <!-- Use automatic signing -->
# <key>compileBitcode</key>
# <false/>
# <key>thinning</key>
# <string>&lt;none&gt;</string>
# </dict>
# </plist>
# EOF

# # Generate .ipa file
# xcodebuild -exportArchive \
# -archivePath "$(pwd)/build/output/iOS/Archive/Unity-iPhone.xcarchive" \
# -exportPath "$(pwd)/build/output/iOS/IPA" \
# -exportOptionsPlist "$EXPORT_OPTIONS_PATH"

0 comments on commit 1a70728

Please sign in to comment.