Skip to content

Instantly share code, notes, and snippets.

@brettscott
Created February 20, 2014 15:02
Show Gist options
  • Save brettscott/9115630 to your computer and use it in GitHub Desktop.
Save brettscott/9115630 to your computer and use it in GitHub Desktop.
Ghostdriver User-Agent Test
#!/bin/bash
# pkill -KILL phantomjs
# phantomjs --disk-cache=no --webdriver=4444 &
# ------------------------------------------------------------------
# Config
host="http://localhost"
port="4444"
testUri="http://localhost/index.php"
userAgents="iphone,firefox,safari,iphone"
# ------------------------------------------------------------------
clear
echo " "
echo " "
OIFS=$IFS;
IFS=",";
i=1
for userAgent in $userAgents; do
IFS=$OIFS;
# ------------------------------------------------------------------
# Each Test
echo -e "\e[96m=== TEST #$i ===\e[0m"
echo -e "\e[93mphantomjs.page.settings.userAgent: \e[5m $userAgent \e[0m"
echo " "
echo " "
# ------------------------------------------------------------------
# Start Session
echo "=== START SESSION ==="
echo "/session :"
request="curl --include --silent --data "{\"desiredCapabilities\":{\"phantomjs.page.settings.userAgent\":\"$userAgent\",\"deviceType\":\"mobile\",\"browserName\":\"phantomjs\"}}" $host:$port/wd/hub/session"
# Response example: {"sessionId":"5bad0930-8a8f-11e3-b573-37389a2d435c","status":0,"value":{"browserName":"phantomjs","version":"1.9.2","driverName":"ghostdriver","driverVersion":"1.0.4","platform":"linux-unknown-64bit","javascriptEnabled":true,"takesScreenshot":false,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnab
response=$($request)
echo "Response:"
echo "$response"
echo " "
echo " "
# Obtain Session Id
sessionString=$(egrep -io '"sessionId":"([^"]+)"' <<<"$response")
sessionId=$(echo "$sessionString" | cut -c14-49)
echo "SessionId: $sessionId"
echo " "
echo " "
# ------------------------------------------------------------------
# Request Page
echo "=== REQUEST PAGE ==="
echo "/url :"
request="curl --include --silent --data "{\"url\":\"$testUri\"}" $host:$port/wd/hub/session/$sessionId/url"
# Response example: {"sessionId":"5bad0930-8a8f-11e3-b573-37389a2d435c","status":0,"value":{}}
response=$($request)
echo "Response:"
echo "$response"
echo " "
echo " "
# ------------------------------------------------------------------
# Test Page Element (User-Agent within <div>)
echo "=== OBTAIN PAGE ELEMENT CONTENTS ==="
echo "/element :"
request="curl --include --silent --data "{\"using\":\"id\",\"value\":\"test_useragent\"}" $host:$port/wd/hub/session/$sessionId/element"
# Response example: {"sessionId":"5bad0930-8a8f-11e3-b573-37389a2d435c","status":0,"value":{"ELEMENT":":wdc:1391612863730"}}
response=$($request)
echo "Response:"
echo "$response"
echo " "
echo " "
# Get Element ID (webdriver element ID) eg :wdc:1391612863730
# eg {"sessionId":"5bad0930-8a8f-11e3-b573-37389a2d435c","status":0,"value":{"ELEMENT":":wdc:1391612863730"}}
elementString=$(egrep -io '"ELEMENT":"([^"]+)"' <<<"$response")
elementId=$(echo "$elementString" | cut -c12-29)
echo "/element/$elementId/text :"
request="curl --include --silent --get $host:$port/wd/hub/session/$sessionId/element/$elementId/text"
# Response example: {"sessionId":"5bad0930-8a8f-11e3-b573-37389a2d435c","status":0,"value":"iphone"}
response=$($request)
echo "Response:"
echo "$response"
echo " "
echo " "
# User-Agent as seen by server
partString=$(egrep -io '"value":"([^"]+)"' <<<"$response")
elementString=$(echo "$partString" | cut -c9-100)
if [ "$elementString" == "\"$userAgent\"" ]; then
echo -e "\e[93mActual User-Agent: $elementString \e[92m * Correct\e[0m"
else
echo -e "\e[93mActual User-Agent: $elementString \e[91m * Wrong (should be \"$userAgent\")\e[0m"
fi
echo " "
echo " "
# ------------------------------------------------------------------
echo " "
echo " "
echo " "
echo " "
echo " "
echo " "
((i++))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment