Skip to content

Sending Capybara Screenshots to S3

fonglh edited this page Jan 10, 2019 · 3 revisions

Motivation

To help with debugging when feature specs fail on Travis but not locally, it can be useful to upload screenshots taken by Capybara Screenshot to AWS S3. This is documented in the gem's README and implemented in #3169.

AWS S3 Bucket

Create a separate S3 bucket to hold the screenshots. For convenience, this was setup as a public bucket. This allows a listing of the screenshots to be accessed at the example URL https://s3.<region>.amazonaws.com/<bucket-name>/

The use of a public bucket was deemed acceptable as this code repository is fully open source anyway and the test data from which the screenshots are generated is publicly available in the code.

The screenshots can then be accessed by appending the <Key> variable to the bucket URL.

AWS IAM User for Bucket Access

Create a separate AWS IAM user just for this bucket. Create an access key for this user, noting down the Access Key ID and the secret.

To give the user access to the bucket, the following policy can be attached directly to the IAM user. Modify the bucket name accordingly.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::<bucket-name>"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:PutObjectACL"
            ],
            "Resource": [
                "arn:aws:s3:::<bucket-name>/*"
            ]
        }
    ]
}

Environment Variables

The following environment variables must be set in Travis for this to work:

  • CAPYBARA_SCREENSHOT_ACCESS_KEY_ID: AWS S3 access key ID allowed to access the S3 bucket where the screenshots will be stored.
  • CAPYBARA_SCREENSHOT_SECRET_ACCESS_KEY: AWS S3 secret access key, used in conjunction with the access key ID to authenticate with S3.
  • CAPYBARA_SCREENSHOT_REGION: AWS S3 region where the bucket is located.
  • CAPYBARA_SCREENSHOT_BUCKET_NAME: Name of the AWS S3 bucket.

To preserve the secrecy of these variables, especially the secret access key, define them in Travis' repository settings.