diff --git a/examples/cloudrace.js b/examples/cloudrace.js new file mode 100644 index 0000000..928c948 --- /dev/null +++ b/examples/cloudrace.js @@ -0,0 +1,16 @@ +// Interact with a React SPA app. +// This uses the selenium-webdriver 3.4 package. +// Docs: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index.html + +$browser.get('https://d3q2ykjqmzt1zl.cloudfront.net/').then(function(){ + return $browser.findElement($driver.By.id('play-race')).then(function(element){ + return element.click().then(function(){ + $browser.wait(function() { + return $driver.until.elementLocated($driver.By.xpath("//*[contains(text(),'#1')]")); + }, 10000).then(function() { + console.log('Race finished!'); + $browser.close(); + }); + }); + }); +}); \ No newline at end of file diff --git a/examples/visitgoogle.js b/examples/visitgoogle.js index 33c0918..aa3e9f5 100644 --- a/examples/visitgoogle.js +++ b/examples/visitgoogle.js @@ -1,7 +1,9 @@ -// Sample selenimum-webdriver script +// Sample selenimum-webdriver script that visits google.com +// This uses the selenium-webdriver 3.4 package. +// Docs: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index.html // $browser = webdriver session // $driver = driver libraries -// console.log will output to AWS Cloudwatch logs +// console.log will output to AWS Lambda logs (via Cloudwatch) console.log('About to visit google.com...'); $browser.get('http://www.google.com/ncr'); diff --git a/lib/sandbox.js b/lib/sandbox.js index f01ba56..dfc6c26 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -14,7 +14,7 @@ exports.executeScript = function(scriptText, browser, driver, cb) { try { script.runInContext(scriptContext); } catch (e) { - console.log('[script-error]', e); + console.log('[script error]', e); return cb(e, null); } diff --git a/readme.md b/readme.md index 395a998..292c461 100644 --- a/readme.md +++ b/readme.md @@ -3,13 +3,6 @@ This uses the binaries from the [serverless-chrome](https://github.com/adieuadieu/serverless-chrome) project to prototype running headless chromium with `selenium-webdriver` in AWS Lambda. I've also bundled the chromedriver binary so the browser can be interacted with using the [Webdriver Protocol](https://www.w3.org/TR/webdriver/). -#### Requirements - -* An AWS Account -* [Terraform](https://terraform.io) (optional but highly recommended for function creation and deploy) -* node.js + npm -* `make` - #### Background The function interacts with [headless Chromium](https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md) process using [chromedriver](https://sites.google.com/a/chromium.org/chromedriver/) and a popular webdriver node.js client library. @@ -18,7 +11,15 @@ The function interacts with [headless Chromium](https://chromium.googlesource.co Since this Lambda function is written using node.js, you can run almost any script written for [selenium-webdriver](https://www.npmjs.com/package/selenium-webdriver). Example scripts can be found in the `examples` directory. -### Installation +## Installation + +#### Requirements + +* An AWS Account +* [Terraform](https://terraform.io) (optional but highly recommended for function creation and deploy) +* node.js + npm +* `make` + #### Fetching dependencies The headless chromium binary is too large for Github, you need to fetch it using a script bundled in this repository. [Marco Lüthy](https://github.com/adieuadieu) has an excellent post on Medium about how he built chromium for for AWS Lambda [here](https://medium.com/@marco.luethy/running-headless-chrome-on-aws-lambda-fa82ad33a9eb). @@ -43,7 +44,7 @@ This will create the function using Terraform with all required permissions. The optional `DEBUG_ENV` environment variable will log additional information to Cloudwatch. The `PATH` environment variable points to the `bin` directory in this project—this is required in order to launch the `chromedriver` process. -### Running the function +## Usage If dependencies are installed and `make deploy` succeeds you can have Lambda run a script. There's an example of a selenium-webdriver simple script in the `examples/` directory that the Lambda function can now run. diff --git a/scripts/upload-github-release-asset.sh b/scripts/upload-github-release-asset.sh new file mode 100644 index 0000000..8a95b51 --- /dev/null +++ b/scripts/upload-github-release-asset.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# +# Author: Stefan Buck +# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 +# +# +# This script accepts the following parameters: +# +# * owner +# * repo +# * tag +# * filename +# * github_api_token +# +# Script to upload a release asset using the GitHub API v3. +# +# Example: +# +# upload-github-release-asset.sh github_api_token=TOKEN tag=v0.1.0 filename=./build.zip +# + +# Check dependencies. +set -e +xargs=$(which gxargs || which xargs) + +# Validate settings. +[ "$TRACE" ] && set -x + +CONFIG=$@ + +for line in $CONFIG; do + eval "$line" +done + +# Define variables. +GH_API="https://api.github.com" +GH_REPO="$GH_API/repos/smithclay/lambdium" +GH_TAGS="$GH_REPO/releases/tags/$tag" +AUTH="Authorization: token $github_api_token" +WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie" +CURL_ARGS="-LJO#" + +if [[ "$tag" == 'LATEST' ]]; then + GH_TAGS="$GH_REPO/releases/latest" +fi + +# Validate token. +curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } + +# Read asset tags. +response=$(curl -sH "$AUTH" $GH_TAGS) + +# Get ID of the asset based on given filename. +eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=') +[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } + +# Upload asset +echo "Uploading asset... $localAssetPath" >&2 + +# Construct url +GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)" + +curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET