diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d9ceaea --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Appium/tests/helpers/mocks"] + path = Appium/tests/helpers/mocks + url = https://github.com/jasonkarns/bats-mock diff --git a/.travis.yml b/.travis.yml index 119d377..1358991 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ before_install: script: - docker build -t "appium/appium:latest" -f Appium/Dockerfile . - - docker run --rm "appium/appium:latest" bats tests/image.bats + - docker run --rm "appium/appium:latest" bats tests after_success: - if [[ $TRAVIS_PULL_REQUEST == "false" ]] && [[ "$TRAVIS_TAG" != "" ]]; then diff --git a/Appium/Dockerfile b/Appium/Dockerfile index 0ab672e..26a0727 100644 --- a/Appium/Dockerfile +++ b/Appium/Dockerfile @@ -104,7 +104,17 @@ EXPOSE 4723 #==================== COPY Appium/tests /root/tests -#=================== +#==================================================== +# Scripts to run appium and connect to Selenium Grid +#==================================================== +COPY \ + Appium/entry_point.sh \ + Appium/generate_config.sh \ + /root/ +RUN chmod +x /root/entry_point.sh +RUN chmod +x /root/generate_config.sh + +#======================================== # Run xvfb and appium server -#=================== -CMD xvfb-run appium +#======================================== +CMD ["/root/entry_point.sh"] diff --git a/Appium/entry_point.sh b/Appium/entry_point.sh new file mode 100755 index 0000000..e0107a6 --- /dev/null +++ b/Appium/entry_point.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +NODE_CONFIG_JSON="/root/nodeconfig.json" +CMD="xvfb-run appium" + +if [ ! -z "$CONNECT_TO_GRID" ]; then + /root/generate_config.sh $NODE_CONFIG_JSON + CMD+=" --nodeconfig $NODE_CONFIG_JSON" +fi + +$CMD diff --git a/Appium/generate_config.sh b/Appium/generate_config.sh new file mode 100755 index 0000000..9878886 --- /dev/null +++ b/Appium/generate_config.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +node_config_json=$1 + +if [ -z "$PLATFORM_NAME" ]; then + PLATFORM_NAME="Android" +fi + +if [ -z "$APPIUM_HOST" ]; then + APPIUM_HOST="127.0.0.1" +fi + +if [ -z "$APPIUM_PORT" ]; then + APPIUM_PORT=4723 +fi + +if [ -z "$SELENIUM_HOST" ]; then + SELENIUM_HOST="172.17.0.1" +fi + +if [ -z "$SELENIUM_PORT" ]; then + SELENIUM_PORT=4444 +fi + +if [ -z "$BROWSER_NAME" ]; then + BROWSER_NAME="android" +fi + +#Get device names +devices=($(adb devices | grep -oP "\K(\w+)(?=\sdevice(\W|$))")) + +#Create capabilities json configs +function create_capabilities() { + capabilities="" + for name in ${devices[@]}; do + os_version="$(adb -s $name shell getprop ro.build.version.release | tr -d '\r')" + capabilities+=$(cat <<_EOF +{ + "platform": "$PLATFORM_NAME", + "platformName": "$PLATFORM_NAME", + "version": "$os_version", + "browserName": "$BROWSER_NAME", + "deviceName": "$name", + "maxInstances": 1 + } +_EOF + ) + if [ ${devices[-1]} != $name ]; then + capabilities+=', ' + fi + done + echo "$capabilities" +} + +#Final node configuration json string +nodeconfig=$(cat <<_EOF +{ + "capabilities": [$(create_capabilities)], + "configuration": { + "cleanUpCycle": 2000, + "timeout": 30000, + "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy", + "url": "http://$APPIUM_HOST:$APPIUM_PORT/wd/hub", + "host": "$APPIUM_HOST", + "port": $APPIUM_PORT, + "maxSession": 6, + "register": true, + "registerCycle": 5000, + "hubHost": "$SELENIUM_HOST", + "hubPort": $SELENIUM_PORT + } +} +_EOF +) +echo "$nodeconfig" > $node_config_json diff --git a/Appium/tests/grid.bats b/Appium/tests/grid.bats new file mode 100644 index 0000000..c141308 --- /dev/null +++ b/Appium/tests/grid.bats @@ -0,0 +1,49 @@ +#!/usr/bin/env bats + +load helpers/mocks/stub + +default_node_config=\ +'{ + "capabilities": [{ + "platform": "Android", + "platformName": "Android", + "version": "7.1.1", + "browserName": "android", + "deviceName": "73QDU16916010699", + "maxInstances": 1 + }, { + "platform": "Android", + "platformName": "Android", + "version": "5.1.1", + "browserName": "android", + "deviceName": "4b13354b80b36200", + "maxInstances": 1 + }], + "configuration": { + "cleanUpCycle": 2000, + "timeout": 30000, + "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy", + "url": "http://127.0.0.1:4723/wd/hub", + "host": "127.0.0.1", + "port": 4723, + "maxSession": 6, + "register": true, + "registerCycle": 5000, + "hubHost": "172.17.0.1", + "hubPort": 4444 + } +}' +node_config_json="/root/nodeconfig.json" +adb_devices_output='73QDU16916010699 device 4b13354b80b36200 device' + +@test 'Verify selenium grid config is created' { + stub adb \ + "devices : echo $adb_devices_output" \ + "-s 73QDU16916010699 shell getprop ro.build.version.release : echo 7.1.1" \ + "-s 4b13354b80b36200 shell getprop ro.build.version.release : echo 5.1.1" + + run /root/generate_config.sh $node_config_json + [ "$(cat $node_config_json)" == "$default_node_config" ] + + unstub adb +} diff --git a/Appium/tests/helpers/mocks b/Appium/tests/helpers/mocks new file mode 160000 index 0000000..db2ac2b --- /dev/null +++ b/Appium/tests/helpers/mocks @@ -0,0 +1 @@ +Subproject commit db2ac2b3d2cb3ea80d19a44d5bf38aede07ba762