Skip to content

Instantly share code, notes, and snippets.

@sidwood
Created September 1, 2014 10:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidwood/fcee60dc7b7e704d818d to your computer and use it in GitHub Desktop.
Save sidwood/fcee60dc7b7e704d818d to your computer and use it in GitHub Desktop.
A selenium server management script for node.js projects. Dug this relic up and updated line #7 so that it can act as a replacement for protractor's currently broken `webdriver-manager update`. Just pop this in your ./bin folder, make it executable, and run `./bin/selenium.sh install`. Jobs a good’un.
#!/usr/bin/env bash
#
# Constants
#
# DOWNLOAD_PATH=vendor/selenium
DOWNLOAD_PATH=node_modules/protractor/selenium
CD_VERSION=2.10
CD_URL=http://chromedriver.storage.googleapis.com/${CD_VERSION}
CD_FILE=chromedriver_mac32.zip
SWD_VERSION=2.42
SWD_PATCH=2
SWD_URL=http://selenium-release.storage.googleapis.com/${SWD_VERSION}
SWD_FILE=selenium-server-standalone-${SWD_VERSION}.${SWD_PATCH}.jar
#
# Exit with given message
#
abort() {
printf "\n \033[31mError: $@\033[0m\n\n" && exit 1
}
#
# Invalid argument message
#
invalid_argument() {
printf "\n \033[31mError: $@ is not a valid argument\033[0m\n"
}
#
# Ensure working directory is node.js project
#
if [ ! -d "$(pwd)/node_modules" ]; then abort "./node_modules not found"; fi
if [ ! -f "$(pwd)/package.json" ]; then abort "./package.json not found"; fi
#
# Ensure dependencies are installed
#
command -v curl > /dev/null 2>&1 || abort "curl is required"
command -v unzip > /dev/null 2>&1 || abort "unzip is required"
command -v java > /dev/null 2>&1 || abort "java is required"
#
# Output usage information
#
display_help() {
cat <<-EOF
Usage: selenium [options]
Install, uninstall and start selenium server with chromedriver. Running
without any options starts selenium server.
Options:
-h, --help, help output usage information
-i, --install, install install selenium server and chromedriver
-u, --uninstall, uninstall remove selenium server and chromedriver
-c, --clean, clean same as uninstall just easier to type
-s, --serve, serve start selenium server with chromedriver
EOF
}
#
# Install selenium and chromedriver
#
install() {
mkdir -p ${DOWNLOAD_PATH}
if [ ! -f "${DOWNLOAD_PATH}/chromedriver" ]; then
cd ${DOWNLOAD_PATH} \
&& echo " Downloading chromedriver archive..." \
&& curl -O --progress-bar ${CD_URL}/${CD_FILE} \
&& unzip ${CD_FILE} \
&& rm ${CD_FILE} \
&& cd - >/dev/null
else
echo " Chromedriver found"
fi
if [ ! -f "${DOWNLOAD_PATH}/${SWD_FILE}" ]; then
cd ${DOWNLOAD_PATH} \
&& echo " Downloading selenium server..." \
&& curl -O --progress-bar ${SWD_URL}/${SWD_FILE} \
&& cd - >/dev/null
else
echo " Selenium server found"
fi
echo "done!"
}
#
# Uninstall selenium and chromedriver
#
uninstall() {
rm -rf ${DOWNLOAD_PATH} 2>/dev/null && mkdir -p ${DOWNLOAD_PATH} 2>/dev/null
}
#
# Start selenium server with chromedriver
#
serve() {
java \
-jar ${DOWNLOAD_PATH}/selenium-server-standalone-*.jar \
-Dwebdriver.chrome.driver=${DOWNLOAD_PATH}/chromedriver
}
#
# Handle arguments
#
if [ $# -eq 0 ]; then
serve; exit 0
else
for arg in $*; do
case $arg in
-h|--help|help) display_help; exit 0 ;;
-i|--install|install) install; exit 0 ;;
-u|--uninstall|uninstall) uninstall; exit 0 ;;
-c|--clean|clean) uninstall; exit 0 ;;
-s|--serve|serve) serve; exit 0 ;;
*) invalid_argument $arg; display_help; exit 1 ;;
esac
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment