Skip to content

Commit

Permalink
update readme, add new example
Browse files Browse the repository at this point in the history
  • Loading branch information
smithclay committed Jun 14, 2017
1 parent 99a1ebe commit 649b9bb
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 12 deletions.
16 changes: 16 additions & 0 deletions examples/cloudrace.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
6 changes: 4 additions & 2 deletions examples/visitgoogle.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
2 changes: 1 addition & 1 deletion lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 10 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand All @@ -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.

Expand Down
63 changes: 63 additions & 0 deletions scripts/upload-github-release-asset.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 649b9bb

Please sign in to comment.