Skip to content

Commit

Permalink
add selenium doc and prepare script
Browse files Browse the repository at this point in the history
We have no documentation in Confluence or this repo for running
Selenium tests natively. This commit adds a doc for that.

We're also migrating the "prepare" script from the qa_tools repo
to this one.

Test Plan:
  - follow the instructions in doc/testing_with_selenium.md to
  verify it's accurate
  - run script/canvas_update to verify it still works
  - follow the setup instructions in script/prepare/README.md and
  verify it works

Change-Id: I11d63e60dc1faa8be1dfacfc9bebfcbea55c31f1
Reviewed-on: https://gerrit.instructure.com/167300
Tested-by: Jenkins
Reviewed-by: David Tan <[email protected]>
QA-Review: David Tan <[email protected]>
Product-Review: Michael Hargiss <[email protected]>
  • Loading branch information
mycargus committed Oct 10, 2018
1 parent f119cfd commit 3cfbdf3
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 103 deletions.
90 changes: 90 additions & 0 deletions doc/testing_with_selenium.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Testing with Selenium

You may run the Selenium tests either natively or in docker.

## Running Selenium Tests Natively (Mac)

We're making a few assumptions here:
- you're using an Apple computer
- you've already installed Homebrew
- you've already installed Postgres (postgresapp.com is an excellent option),
and Postgres is running on your computer
- you've already installed Node.js

0. Install `yarn` if you haven't already:

```sh
brew install yarn
```

If you find you need an older version of yarn, follow these instructions to
install the older version and switch to it:

[https://stackoverflow.com/a/52525732/3038677](https://stackoverflow.com/a/52525732/3038677)

1. Follow the instructions in `script/prepare/README.md` to setup the `prepare`
script.

You'll use the `prepare` script later to automate installing and updating Canvas
on your computer.

Note: some features of `prepare` only work if you have access to Instructure's
Gerrit host. See the README for details.

2. Install a web browser driver on your computer for the browser you wish to run
the tests in. Homebrew is the easiest way to go:

```sh
brew install chromedriver # necessary for running tests in Chrome
brew install geckodriver # necessary for running tests in Firefox
```

Now let's get Canvas ready to run the tests.

3. Copy the Selenium and database configuration files:

```sh
cp config/selenium.yml.example config/selenium.yml
cp config/database.yml.example config/database.yml
```

4. Use `prepare` to install Canvas plugins and dependencies, create databases,
run database migrations, etc:

```sh
prepare
```

You might encounter problems with some Ruby dependencies. The ["Dependency
Installation" section](https://github.com/instructure/canvas-lms/wiki/Quick-Start#dependency-installation)
in the public Canvas LMS Github wiki has some useful tips.

4.a. Optional. Run delayed jobs in the foreground (not all Selenium tests need
this but some do):

```sh
script/delayed_job run
```

or run it in the background:

```sh
script/delayed_job run &
```

5. Run the Selenium tests:

```sh
bundle exec rspec spec/selenium
```

or run a specific Selenium test:

```sh
bundle exec rspec spec/selenium/accounts_spec.rb:36
```

## Running Selenium Tests in Docker

See the [Selenium section](https://github.com/instructure/canvas-lms/blob/master/doc/docker/developing_with_docker.md#selenium)
of the `doc/docker/developing_with_docker.md` instructions.
112 changes: 9 additions & 103 deletions script/canvas_update
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash

source script/common.sh

LOG="$(pwd)/log/canvas_update.log"

# TODO: improve usage / help
function usage {
echo "usage: canvas_update [-qhv] [-n phase]"
Expand All @@ -10,104 +14,6 @@ function bad_usage {
exit 1
}

function echo_console_and_log {
echo "$1"
echo "$1" >>"$LOG"
}

function is_git_dir {
git rev-parse --is-inside-git-dir >/dev/null 2>&1 && [ -d .git ]
return $?
}

function is_canvas_root {
CANVAS_IN_README=$(head -1 README.md 2>/dev/null | grep 'Canvas LMS')
[[ "$CANVAS_IN_README" != "" ]] && is_git_dir
return $?
}

function ensure_in_canvas_root_directory {
if ! is_canvas_root; then
echo "Please run from a Canvas root directory"
exit 0
fi
}

function intro_message {
echo "Bringing Canvas up to date ..."
echo " Log file is $LOG"

echo >>"$LOG"
echo "-----------------------------" >>"$LOG"
echo "Canvas Update ($(date)):" >>"$LOG"
echo "-----------------------------" >>"$LOG"
}

function update_plugin {
(
cd "$1"
if is_git_dir; then
echo_console_and_log " Updating plugin $1 ..."
( git checkout master >>"$LOG" 2>&1 && \
git pull --rebase >>"$LOG" 2>&1 ) || \
( echo " failed to pull plugin (see $LOG)"; kill -INT $$ )
fi
)
}

function update_plugins {
# Loop through each plugin dir, and if it's a git repo, update it
# This needs to be done first so that db:migrate can pull in any plugin-
# precipitated changes to the database.
for dir in {gems,vendor}; do
if [ -d "$dir/plugins" ]; then
for plugin in $dir/plugins/*; do update_plugin "$plugin"; done
fi
done
}

function rebase_canvas {
echo_console_and_log " Pulling Canvas code ..."
( git checkout master >>"$LOG" 2>&1 && git pull --rebase >>"$LOG" 2>&1 ) || \
( echo " failed to pull Canvas (see $LOG)"; kill -INT $$ )
}

function bundle_install_with_check {
echo_console_and_log " Checking your gems (bundle check) ..."
if bundle check >>"$LOG" 2>&1 ; then
echo_console_and_log " Gems are up to date, no need to bundle install ..."
else
bundle_install
fi
}

function bundle_install {
echo_console_and_log " Installing gems (bundle install) ..."
rm Gemfile.lock* >/dev/null 2>&1
bundle install >>"$LOG" 2>&1 || \
( echo " failed to bundle install (see $LOG)"; kill -INT $$ )
}

function rake_db_migrate_dev_and_test {
echo_console_and_log " Migrating DB ..."
RAILS_ENV=development bundle exec rake db:migrate >>"$LOG" 2>&1 || \
( echo " failed to migrate db (development) (see $LOG)"; kill -INT $$ )
RAILS_ENV=test bundle exec rake db:migrate >>"$LOG" 2>&1 || \
( echo " failed to migrate db (test) (see $LOG)"; kill -INT $$ )
}

function yarn_install {
echo_console_and_log " Installing node packages ..."
yarn install >>"$LOG" 2>&1 || \
( echo " failed to install node packages (see $LOG)"; kill -INT $$ )
}

function compile_assets {
echo_console_and_log " compiling assets ..."
bundle exec rake 'canvas:compile_assets_dev' >>"$LOG" 2>&1 || \
( echo " failed to generate JS (see $LOG)"; kill -INT $$ )
}

# TODO: moar tips plz
function tips {
echo "Tips:"
Expand All @@ -117,16 +23,17 @@ function tips {

function update_canvas {
ensure_in_canvas_root_directory
intro_message
intro_message "Canvas Update"

if [[ -z "$SKIP_CODE" ]] ; then
update_plugins
checkout_master_canvas
rebase_canvas
fi

if [[ -z "$SKIP_DEPS" ]] ; then
bundle_install_with_check
yarn_install
install_node_packages
fi

if [[ -z "$SKIP_DATA" ]] ; then
Expand All @@ -144,8 +51,6 @@ function update_canvas {
fi
}

LOG="$(pwd)/log/canvas_update.log"

# default options
PERFORM_UPDATE=true
QUICK_MODE=false
Expand Down Expand Up @@ -184,6 +89,7 @@ do
esac
done

if $PERFORM_UPDATE; then
if [ "$PERFORM_UPDATE" == "true" ]; then
trap print_results INT TERM EXIT
update_canvas
fi
121 changes: 121 additions & 0 deletions script/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/bin/bash

# This file contains commonly used BASH functions for scripting in canvas-lms,
# particularly script/canvas_update and script/prepare/prepare . As such,
# *be careful* when you modify these functions as doing so will impact multiple
# scripts that likely aren't used or tested in continuous integration builds.

function echo_console_and_log {
echo "$1"
echo "$1" >>"$LOG"
}

function print_results {
exit_code=$?
set +e

if [ "${exit_code}" == "0" ]; then
echo ""
echo_console_and_log " \o/ Success!"
else
echo ""
echo_console_and_log " /o\ Something went wrong. Check ${LOG} for details."
fi

exit ${exit_code}
}

function ensure_in_canvas_root_directory {
if ! is_canvas_root; then
echo "Please run from a Canvas root directory"
exit 0
fi
}

function is_canvas_root {
CANVAS_IN_README=$(head -1 README.md 2>/dev/null | grep 'Canvas LMS')
[[ "$CANVAS_IN_README" != "" ]] && is_git_dir
return $?
}

function is_git_dir {
git rev-parse --is-inside-git-dir >/dev/null 2>&1 && [ -d .git ]
return $?
}

# Parameter: the name of the script calling this function
function intro_message {
script_name="$1"
echo "Bringing Canvas up to date ..."
echo " Log file is $LOG"

echo >>"$LOG"
echo "-----------------------------" >>"$LOG"
echo "$1 ($(date)):" >>"$LOG"
echo "-----------------------------" >>"$LOG"
}

function update_plugin {
(
cd "$1"
if is_git_dir; then
echo_console_and_log " Updating plugin $1 ..."
git checkout master >>"$LOG" 2>&1
git pull --rebase >>"$LOG" 2>&1
fi
)
}

function update_plugins {
# Loop through each plugin dir, and if it's a git repo, update it
# This needs to be done first so that db:migrate can pull in any plugin-
# precipitated changes to the database.
for dir in {gems,vendor}; do
if [ -d "$dir/plugins" ]; then
for plugin in $dir/plugins/*; do update_plugin "$plugin"; done
fi
done
}

function checkout_master_canvas {
echo_console_and_log " Checking out canvas-lms master ..."
git checkout master >>"$LOG" 2>&1
}

function rebase_canvas {
echo_console_and_log " Rebasing canvas-lms on HEAD ..."
git pull --rebase >>"$LOG" 2>&1
}

function bundle_install {
echo_console_and_log " Installing gems (bundle install) ..."
rm Gemfile.lock* >/dev/null 2>&1
bundle install >>"$LOG" 2>&1
}

function bundle_install_with_check {
echo_console_and_log " Checking your gems (bundle check) ..."
if bundle check >>"$LOG" 2>&1 ; then
echo_console_and_log " Gems are up to date, no need to bundle install ..."
else
bundle_install
fi
}

function rake_db_migrate_dev_and_test {
echo_console_and_log " Migrating development DB ..."
RAILS_ENV=development bundle exec rake db:migrate >>"$LOG" 2>&1

echo_console_and_log " Migrating test DB ..."
RAILS_ENV=test bundle exec rake db:migrate >>"$LOG" 2>&1
}

function install_node_packages {
echo_console_and_log " Installing Node packages ..."
bundle exec rake js:yarn_install >>"$LOG" 2>&1
}

function compile_assets {
echo_console_and_log " Compiling assets (css and js only, no docs or styleguide) ..."
bundle exec rake canvas:compile_assets_dev >>"$LOG" 2>&1
}
Loading

0 comments on commit 3cfbdf3

Please sign in to comment.