Skip to content

Instantly share code, notes, and snippets.

@cosic
Created August 9, 2021 08:44
Show Gist options
  • Save cosic/58d03847d536128a1a1bc4265a597cef to your computer and use it in GitHub Desktop.
Save cosic/58d03847d536128a1a1bc4265a597cef to your computer and use it in GitHub Desktop.
Marathon
#!/bin/bash
# Script adb+
# Usage
# You can run any command adb provides on all your currently connected devices
# ./adb+ <command> is the equivalent of ./adb -s <serial number> <command>
#
# Examples
# ./adb+ version
# ./adb+ install -r ./apidemo.apk
# ./adb+ uninstall com.example.android.apis
if [[ -z "${ANDROID_HOME}" ]]; then
echo
echo "Error: ANDROID_HOME is empty. Please set ANDROID_HOME at your environment."
echo "For more info visit https://kobkrit.com/setting-android-home-enviromental-variable-on-mac-os-x-after-install-android-studio-79f08a9d7ec"
exit 1
fi
adb="${ANDROID_HOME}/platform-tools/adb"
device_list=$(${adb} devices)
parallel_commands=""
while IFS= read -r line;
do
if [[ "${line}" != "List of devices attached" ]]; then
if [[ ! "${line}" = "" ]] && [[ `echo ${line} | awk '{print $2}'` = "device" ]]; then
device_id=`echo ${line} | awk '{print $1}'`
if [[ "$@" == *"shell"* ]]; then
parallel_commands+="${adb} -s ${device_id} $@ & "
else
echo "${adb} -s ${device_id} $@ ..."
${adb} -s ${device_id} $@
fi
else
echo "Can't parse plugged in device from: '${line}'"
fi
fi
done <<< "${device_list}"
if [[ ! -z "${parallel_commands}" ]]; then
echo "Execute parallel commands: ${parallel_commands}"
eval "${parallel_commands}"
fi
#!/bin/bash
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
MARATHON_VERSION="0.6.0"
cd ${SCRIPTPATH}
function remove_directory {
local directory=$1
if [[ -d ${directory} ]]; then
echo "Remove '${directory}' directory..."
rm -rv ${directory}
echo
fi
}
if [[ -f ./bin/version && ! -z $(grep "${MARATHON_VERSION}" ./bin/version) ]]; then
echo "Marathon has already been installed!"
else
remove_directory "./bin"
remove_directory "./lib"
echo "Marathon installation..."
echo
curl -LJO "https://github.com/Malinskiy/marathon/releases/download/${MARATHON_VERSION}/marathon-${MARATHON_VERSION}.zip"
unzip ./marathon-${MARATHON_VERSION}.zip
mv ./marathon-${MARATHON_VERSION} ./tmp
cp -rp ./tmp/bin/ ./bin
cp -rp ./tmp/lib/ ./lib
rm -rv ./tmp
rm ./marathon-${MARATHON_VERSION}.zip
echo ${MARATHON_VERSION} > ./bin/version
echo
echo "Marathon successfully installed!"
fi
cd -
#!/bin/bash
echo "::Start Marathon script..."
echo
UITEST_MOCK="mock"
UITEST_E2E="e2e"
UITEST_TYPES=(${UITEST_MOCK} ${UITEST_E2E})
TYPE_CUSTOM="custom"
SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
HTML_REPORT_PATH="${SCRIPTPATH}/app/build/reports/marathon/html/index.html"
MARATHON_DIR="${SCRIPTPATH}/.marathon"
MARATHON_SCRIPT="${MARATHON_DIR}/bin/marathon"
MARATHON_INSTALL_SCRIPT="${MARATHON_DIR}/install-marathon.sh"
TOOLS_DIR="${SCRIPTPATH}/.tools"
YAML_SPACE=' '
TEST_METHOD_NAME=""
TEST_CLASS_NAME=""
REPEAT_COUNT=1
MARATHON_CONFIG_ALL_MOCK_FILE="${MARATHON_DIR}/config/marathon_mock_config.yaml"
MARATHON_CONFIG_ALL_E2E_FILE="${MARATHON_DIR}/config/marathon_e2e_config.yaml"
MARATHON_CONFIG_CUSTOM_DIRECTORY="${SCRIPTPATH}/app/build/reports/marathon"
MARATHON_CONFIG_CUSTOM_FILE="${MARATHON_CONFIG_CUSTOM_DIRECTORY}/custom_config.yaml"
export PATH=${TOOLS_DIR}:$PATH
export PATH=${ANDROID_HOME}/platform-tools/:$PATH
function howTo {
echo
echo "Usage:"
echo "Run ./$(basename $BASH_SOURCE) [options]"
echo
echo "Supported options:"
echo -e "[ ${UITEST_MOCK} | ${UITEST_E2E} ] (required)' - test target as first argument"
echo -e "\t'${UITEST_MOCK}' - run mocked UI tests"
echo -e "\t'${UITEST_E2E}' - run e2e UI tests"
echo -e "'-m, --method (optional)' - method name to execute a single test"
echo -e "'-c, --class (optional)' - class name to execute all test at class"
echo -e "'-r, --repeat (optional)' - repeat count for every test"
echo
echo -e "For example:"
echo -e "./$(basename $BASH_SOURCE) mock"
echo -e "./$(basename $BASH_SOURCE) mock -c FlatValuationUiTest"
echo -e "./$(basename $BASH_SOURCE) mock -m shouldShowChatsOnEnabledFeatureToggle"
echo -e "./$(basename $BASH_SOURCE) mock --method shouldShowChatsOnEnabledFeatureToggle --class FlatValuationUiTest"
echo
}
function disableDeviceAnimations {
set -e
echo
echo "Disable device animations..."
adb devices
adb+ shell settings put global window_animation_scale 0
adb+ shell settings put global transition_animation_scale 0
adb+ shell settings put global animator_duration_scale 0
set +e
echo
}
function enableDeviceAnimations {
set -e
echo
echo "Enable device animations..."
adb devices
adb+ shell settings put global window_animation_scale 1
adb+ shell settings put global transition_animation_scale 1
adb+ shell settings put global animator_duration_scale 1
set +e
echo
}
function isDeviceAvailable() {
adb devices | while read line;
do
if [[ ! "${line}" = "" ]] && [[ `echo ${line} | awk '{print $2}'` = "device" ]]; then
device=`echo ${line} | awk '{print $1}'`
echo "true"
fi
done
echo "false"
}
function addRow() {
echo ${1} >> ${MARATHON_CONFIG_CUSTOM_FILE}
}
function createCustomMarathonConfigFile() {
local type=${1}
local class=${2}
local method=${3}
local repeats=${4}
if [[ -f ${MARATHON_CONFIG_CUSTOM_FILE} ]]; then
rm -rv ${MARATHON_CONFIG_CUSTOM_FILE}
fi
if [[ ! -d ${MARATHON_CONFIG_CUSTOM_DIRECTORY} ]]; then
mkdir -p ${MARATHON_CONFIG_CUSTOM_DIRECTORY}
fi
addRow "name: \"All ${type} tests\""
addRow "outputDir: \"app/build/reports/marathon\""
addRow "retryStrategy:"
addRow "\ttype: \"fixed-quota\""
addRow "\tretryPerTestQuota: 0"
addRow "shardingStrategy:"
addRow "\ttype: \"count\""
addRow "\tcount: ${REPEAT_COUNT}"
addRow "filteringConfiguration:"
addRow "\tallowlist:"
addRow "\t- type: \"annotation\""
if [[ "${type}" == "${UITEST_E2E}" ]]; then
addRow "\t\tregex: \"ru.cian.uitest.annotation.E2ETest\""
fi
if [[ "${type}" == "${UITEST_MOCK}" ]]; then
addRow "\t\tregex: \"ru.cian.uitest.annotation.MockTest\""
fi
if [[ ! -z ${method} ]]; then
addRow "\t- type: \"method\""
addRow "\t\tregex: \"${method}\""
fi
if [[ ! -z ${class} ]]; then
addRow "\t- type: \"simple-class-name\""
addRow "\t\tregex: \"${class}\""
fi
addRow "testOutputTimeoutMillis: 120000"
addRow "vendorConfiguration:"
addRow "\ttype: \"Android\""
addRow "\tapplicationApk: \"app/build/outputs/apk/debug/app-debug.apk\""
addRow "\ttestApplicationApk: \"app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk\""
addRow "\tallureConfiguration:"
addRow "\t\tenabled: true"
addRow "\t\tresultsDirectory: \"/sdcard/allure-results\""
addRow "\tautoGrantPermission: true"
addRow "\tapplicationPmClear: true"
addRow "\ttestApplicationPmClear: true"
addRow "\tserialStrategy: \"ddms\""
addRow "\tinstrumentationArgs:"
addRow "\t\tlistener: ru.cian.uitest.leak.CustomFailTestOnLeakRunListener"
sed -i -e "s/\\\t/${YAML_SPACE}/g" ${MARATHON_CONFIG_CUSTOM_FILE}
}
function installMarathon() {
${MARATHON_INSTALL_SCRIPT}
}
function execMarathon() {
local configFile=${1}
${MARATHON_SCRIPT} -m ${configFile}
}
########################################################################################################################
# Main code block;
########################################################################################################################
TYPE=${1}
TARGET=${1}
if [[ ! " ${UITEST_TYPES[@]} " =~ " ${TYPE} " ]]; then
echo "Error: the first argumen is not one of [${UITEST_TYPES[@]}]"
howTo
exit 1
fi
shift
# Read input params
while :; do
case ${1} in
-h|-\?|--help)
howTo
exit 0
;;
-c|--class)
TEST_CLASS_NAME=${2}
shift
;;
-r|--repeat)
REPEAT_COUNT=${2}
shift
;;
-m|--method)
TEST_METHOD_NAME=${2}
shift
;;
-:)
printf "missing argument for ${1}\n" "${2}" >&2
howTo
exit 1
;;
--)
shift
break
;;
-?*)
printf "illegal option: ${1}\n" "${2}" >&2
howTo
exit 1
;;
*)
break
esac
shift
done
if [[ -z "${ANDROID_HOME}" ]]; then
echo
echo "Error: ANDROID_HOME doesn't exist or is empty. Please set ANDROID_HOME at your environment."
echo "For more info visit https://kobkrit.com/setting-android-home-enviromental-variable-on-mac-os-x-after-install-android-studio-79f08a9d7ec"
exit 1
fi
installMarathon
if [[ "$(isDeviceAvailable)" == "false" ]]; then
echo
echo "Error: There aren't any connected devices!"
exit 1
fi
disableDeviceAnimations
if [[ ! -z ${TEST_METHOD_NAME} || ! -z ${TEST_CLASS_NAME} ]]; then
TARGET=${TYPE_CUSTOM}
fi
case ${TARGET} in
${UITEST_MOCK})
execMarathon ${MARATHON_CONFIG_ALL_MOCK_FILE}
;;
${UITEST_E2E})
execMarathon ${MARATHON_CONFIG_ALL_E2E_FILE}
;;
${TYPE_CUSTOM})
createCustomMarathonConfigFile "${TYPE}" "${TEST_CLASS_NAME}" "${TEST_METHOD_NAME}"
execMarathon ${MARATHON_CONFIG_CUSTOM_FILE}
;;
*)
howTo
enableDeviceAnimations
exit 1
esac
exitcode=$?
echo
if [[ ${exitcode} != 0 ]]; then
echo "UI tests failed!"
else
echo "UI tests successfully finished!"
fi
enableDeviceAnimations
echo
echo "Marathon Report: ${HTML_REPORT_PATH}"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment