Skip to content

Instantly share code, notes, and snippets.

@krishtoautomate
Created January 3, 2024 16:04
Show Gist options
  • Save krishtoautomate/be947f1cd35f6988dd9c1d20c91a0674 to your computer and use it in GitHub Desktop.
Save krishtoautomate/be947f1cd35f6988dd9c1d20c91a0674 to your computer and use it in GitHub Desktop.
up appium nodes for Grid
#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/share:/opt/homebrew/share/android-sdk/tools:/opt/homebrew/share/android-sdk/platform-tools
pkill -f "java -jar selenium-server-"
pkill -f "appium"
# Function to create a .toml file for a given device
create_toml_file() {
local device_type="$1"
local device_name="$2"
local udid="$3"
local automation_name="$4"
local device_version="$5"
local port="$6"
local serverPort="$7"
local uniquePort="$8"
local devicePort="$9"
local browserName="${10}"
local deviceClass="${11}"
#local-ip-address
wlan_ip=$(ifconfig en8 | grep "inet " | awk '{print $2}')
if [ $device_type == 'ANDROID' ]
then
cat > "config/$device_type-$udid.toml" << EOF
[server]
host = "$wlan_ip"
url = "http://$wlan_ip:$port"
port = $port
# session-request-timeout = 1800
[events]
publish = "tcp://<Grid ip-address>:4442"
subscribe = "tcp://<Grid ip-address>:4443"
[node]
detect-drivers = false
max-sessions = 1
session-timeout = 180
[relay]
url = "http://$wlan_ip:$serverPort"
display-name = "$device_type-$udid"
service-host = "$wlan_ip"
service-port = $serverPort
status-endpoint = "/status"
configs = [
"1", "{\"appium:dashboard\": \"http://$wlan_ip:$serverPort/dashboard\", \"platformName\": \"$device_type\", \"appium:udid\": \"$udid\", \"appium:deviceName\": \"$device_name\",\"appium:automationName\": \"$automation_name\", \"appium:systemPort\":$devicePort, \"appium:platformVersion\": \"$device_version\", \"appium:deviceClass\":\"$deviceClass\"}"
]
EOF
else
rounded=$(echo "$device_version" | awk -F'.' '{print $1"."$2}')
versionCap="14.9"
if (( $(awk -v rounded="$rounded" 'BEGIN {print (rounded > 14.9) ? 1 : 0}') )); then
echo "$udid - $device_version"
# continue;
versionCap="15.0"
fi
cat > "config/$device_type-$udid.toml" << EOF
[server]
host = "$wlan_ip"
url = "http://$wlan_ip:$port"
port = $port
# session-request-timeout = 1800
[events]
publish = "tcp://<Grid ip-address>:4442"
subscribe = "tcp://<Grid ip-address>:4443"
[node]
display-name = "$device_type-$udid"
detect-drivers = false
max-sessions = 1
session-timeout = 180
[relay]
url = "http://$wlan_ip:$serverPort"
service-host = "$wlan_ip"
service-port = $serverPort
status-endpoint = "/status"
configs = [
"1", "{\"appium:dashboard\": \"http://$wlan_ip:$serverPort/dashboard\", \"platformName\": \"$device_type\", \"appium:udid\": \"$udid\", \"appium:deviceName\": \"$device_name\",\"appium:automationName\": \"$automation_name\", \"appium:wdaLocalPort\":$devicePort, \"appium:platformVersion\": \"$device_version\", \"appium:minVersion\":\"$versionCap\", \"appium:deviceClass\":\"$deviceClass\"}"
]
EOF
fi
cat > "config/$device_type-$udid.yml" << EOF
server:
port: $serverPort
keep-alive-timeout: 80
session-override: true
# allow-insecure: true
# allow-cors: true
log-level: error
# use-plugin: appium-dashboard
use-drivers:
- $automation_name #uiautomator2 #XCUITest
default-capabilities:
$uniquePort: $devicePort
appium:udid: "$udid"
appium:deviceName: "$device_name"
appium:platformName: "$device_type"
appium:platformVersion: "$device_version"
# browserName: "$browserName"
EOF
}
# Get connected Android devices' information
serverPort=4781
port=5781
devicePort=8300
android_devices=($(adb devices | grep -v "List of devices" | grep "device$" | awk '{print $1}'))
# Create .toml files for Android devices
for udid in "${android_devices[@]}"; do
# if [[ $udid == *"9RJ0220516003054"* ]]; then
# continue;
# fi
device_name="$udid"
android_version=$(adb -s $udid shell getprop ro.build.version.release)
android_name=$(adb -s $udid shell getprop ro.product.model)
if [ "$android_name" = "" ]; then
continue
fi
port=$((port+1))
serverPort=$((serverPort+1))
devicePort=$((devicePort+1))
create_toml_file "ANDROID" "$android_name" "$udid" "UIAutomator2" $android_version "$port" "$serverPort" "appium:systemPort" "$devicePort" "Chrome" "NA"
nohup appium --config ./config/ANDROID-"$udid".yml </dev/null &>/tmp/"$serverPort".log & sleep 5
nohup java -jar selenium-server-4.12.1.jar node --config ./config/ANDROID-"$udid".toml </dev/null &>/tmp/"$serverPort".log & sleep 5
echo "appium --config ./config/ANDROID-"$udid".yml"
echo "java -jar selenium-server-4.12.1.jar node --config ./config/ANDROID-"$udid".toml"
echo "started node : $udid-$serverPort"
# break;
done
# Get connected iOS devices' information
ios_devices=($(idevice_id -l))
# Create .toml files for iOS devices
for udid in "${ios_devices[@]}"; do
# if [[ $udid == *"c5aa6d6e334d2840e1302fbbca97bff7b87e11d8"* ]]; then
# continue;
# fi
# if [[ $udid == *"00008120-000C59A93C72201E"* ]]; then
# continue;
# fi
device_name="$udid"
ios_version=$(ideviceinfo -u $udid -k ProductVersion)
ios_deviceName=$(ideviceinfo -u $udid -k DeviceName)
ios_deviceClass=$(ideviceinfo -u $udid -k DeviceClass)
port=$((port+1))
serverPort=$((serverPort+1))
devicePort=$((devicePort+1))
create_toml_file "IOS" "$ios_deviceName" "$udid" "XCUITest" "$ios_version" "$port" "$serverPort" "appium:wdaLocalPort" "$devicePort" "Safari" "$ios_deviceClass"
nohup appium --config ./config/IOS-"$udid".yml </dev/null &>/tmp/"$serverPort".log & sleep 5
nohup java -jar selenium-server-4.12.1.jar node --config ./config/IOS-"$udid".toml </dev/null &>/tmp/"$serverPort".log & sleep 5
echo "started node : $udid-$serverPort"
# break;
done
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment