Skip to content

Commit

Permalink
Added functionality to connect to selenium grid (#43)
Browse files Browse the repository at this point in the history
* Added functionality to connect to selenium grid

* Dynamically get device properties, test for config creation

* Added bats-mock module for testing

* Selenium grid functionality tests with mocks
  • Loading branch information
andrcuns authored and SrinivasanTarget committed Jun 5, 2017
1 parent 004ae91 commit ea5cc15
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Appium/tests/helpers/mocks"]
path = Appium/tests/helpers/mocks
url = https://github.com/jasonkarns/bats-mock
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions Appium/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
11 changes: 11 additions & 0 deletions Appium/entry_point.sh
Original file line number Diff line number Diff line change
@@ -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
75 changes: 75 additions & 0 deletions Appium/generate_config.sh
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions Appium/tests/grid.bats
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions Appium/tests/helpers/mocks
Submodule mocks added at db2ac2

0 comments on commit ea5cc15

Please sign in to comment.