Skip to content

Commit 4dfa82d

Browse files
committed
Merge pull request #13 from codebendercc/disqus-support
Disqus support
2 parents 8b8ab6a + 1a476be commit 4dfa82d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1906
-273
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
__pycache__/
2+
.cache/
3+
bin/env_vars.sh
14
.tox
25
*.pyc
36
*.swp

Dockerfile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Dockerfile to build container with Codebender selenium tests.
2+
3+
FROM ubuntu:14.04
4+
# TODO: add MAINTAINER
5+
6+
# Install requirements and their dependencies
7+
RUN apt-get update -y
8+
RUN apt-get install -y \
9+
gcc \
10+
libffi-dev \
11+
python \
12+
python-dev \
13+
python-setuptools \
14+
openssl \
15+
libssl-dev \
16+
mailutils \
17+
ssmtp \
18+
sharutils
19+
20+
RUN easy_install pip
21+
RUN pip install --upgrade pip
22+
#RUN pip install -U setuptools
23+
24+
# Add source code and install dependencies
25+
RUN mkdir -p /opt/codebender
26+
ADD . /opt/codebender/seleniumTests
27+
28+
WORKDIR /opt/codebender/seleniumTests
29+
30+
RUN pip install -r requirements.txt
31+
32+
COPY ssmtp.conf /etc/ssmtp/
33+
34+
# Specify a default command for the container.
35+
# Right now we simply run bash. TODO: add ENTRYPOINT for running tests.
36+
37+
#CMD ["/bin/bash"]

README.md

Lines changed: 137 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,144 @@
11
# Codebender Selenium Tests
22

3-
This repo contains Selenium tests for the codebender website.
4-
The tests are written in Python 3.
3+
This repo contains Selenium tests for the codebender website. The tests are
4+
written in Python 2, and utilize pytest as a testing framework and Selenium
5+
for browser automation.
56

67
## Running Tests
78

8-
To run tests locally, you'll need to be running a selenium server. See
9-
[here](https://selenium-python.readthedocs.org/installation.html#downloading-selenium-server)
10-
for instructions.
9+
Tests are run by invoking `tox`. Run `tox --help` to see all the
10+
Codebender-specific arguments that can be passed to `py.test`.
1111

12-
Once you've got a Selenium server running, simply run `$ tox` from within the
13-
repo. If you don't have tox, run `$ sudo pip3 install -r requirements-dev.txt`
14-
from within the repo to install it.
12+
In addition to these arguments, there are certain environment variables that
13+
should be set when running tests:
14+
15+
- `CODEBENDER_SELENIUM_HUB_URL`: the URL of the Selenium Hub. If you are using
16+
SauceLabs, the URL has the following format:
17+
`http://{USERNAME}:{ACCESS_KEY}@ondemand.saucelabs.com:80/wd/hub`. You can
18+
also use a [docker-selenium](https://github.com/SeleniumHQ/docker-selenium)
19+
hub. In that case, it is necessary to link the docker-selenium instance to the
20+
Docker instance from which tests are running.
21+
- `CODEBENDER_TEST_USER`: username that the webdriver will use to log into the
22+
site in order to perform tests.
23+
- `CODEBENDER_TEST_PASS`: password for `CODEBENDER_TEST_USER`.
24+
25+
Rather than invoking `tox` directly, the easiest way to run tests is with
26+
Docker. If you are not familiar with Docker, please consult the
27+
[documentation](http://docs.docker.com/) for an introduction.
28+
29+
First, build the image with `$ docker build . -t codebender/selenium`.
30+
31+
Then invoke `tox` via `docker run`. Here is a sample command to run all tests,
32+
where the Codebender server is running at `http://192.168.1.2:8080`:
33+
34+
```
35+
$ docker run -e CODEBENDER_SELENIUM_HUB_URL=http://johndoe:[email protected]:80/wd/hub \
36+
-e CODEBENDER_TEST_USER=tester \
37+
-e CODEBENDER_TEST_PASS=1234 \
38+
-it codebender/selenium \
39+
tox -- --url http://192.168.1.2:8080 --source bachelor
40+
```
41+
42+
### Running Tests Manually
43+
44+
The recommended way of running tests is with Docker. If you would like to
45+
manually provision your machine to be able to run tests, you can use the
46+
Dockerfile as a step-by-step guide for provisioning. Then invoke `tox` to run
47+
tests.
48+
49+
#### Specifying a URL for Tests
50+
51+
Tests can either be run for the
52+
[bachelor](https://github.com/codebendercc/bachelor) version of the site,
53+
running locally, or for the live site. The version of the site that is running
54+
is inferred from the `--url` parameter. You can run `$ tox --url
55+
http://localhost` to run the tests for a locally running bachelor site (this is
56+
the default url), or `$ tox --url http://codebender.cc` to run the tests for the
57+
live site.
58+
59+
Certain tests are specially written for one site or the other. This is
60+
implemented with a custom `pytest` marker. Tests that require a certain `--url`
61+
are decorated with `@pytest.mark.requires_url(<url>)`.
62+
63+
### Changing Test Configuration
64+
65+
Various global configuration parameters are specified in
66+
`codebender_testing/config.py`. Such parameters include URLs and site endpoints
67+
which are subject to change. This is also where the webdrivers (Firefox and
68+
Chrome) are specified.
69+
70+
## Compilation Logs
71+
72+
Certain tests exist to iterate through groups of sketches and compile them
73+
one-by-one. Since these tests take a long time, they are not run in full by
74+
default. You can run them by specifying the `--full` option; for example: `$ tox
75+
tests/cb_compile_tester --full`.
76+
77+
The following test cases are compile tests that generate such logs:
78+
- `tests/libraries/test_libraries.py::TestLibraryExamples`
79+
- `tests/compile_tester/test_compile_tester_projects.py::TestCompileTester`
80+
81+
The generated logs are placed in the `logs` directory. They give detailed output
82+
in JSON format containing the codebender site URL that was used to run the
83+
tests, along with the URLs of the individual sketches that were compiled, and
84+
whether they succeeded or failed to compile.
85+
86+
## Framework Overview
87+
88+
The following outlines the structure of the repository as well as important
89+
framework components.
90+
91+
### Directory Structure
92+
93+
#### `tests/`
94+
95+
The `tests/` directory contains all of the actual unit tests for the codebender
96+
site. That is, all of the tests discovered by `py.test` should come from this
97+
directory.
98+
99+
**`tests/conftest.py`** contains the global configuration for pytest,
100+
including specifying the webdriver fixtures as well as the available command
101+
line arguments.
102+
103+
#### `codebender_testing/`
104+
105+
This is where all major components of the testing framework live. All of the
106+
unit tests rely on the files in this directory.
107+
108+
**`codebender_testing/config.py`** specifies global configuration parameters for
109+
testing (see "Changing Test Configuration" above).
110+
111+
**`codebender_testing/utils.py`** defines codebender-specific utilities used to
112+
test the site. These mostly consist of abstractions to the Selenium framework.
113+
The most important class is `SeleniumTestCase`, which all of the unit test cases
114+
inherit from. This grants them access (via `self`) to a number of methods and
115+
attributes that are useful for performing codebender-specific actions.
116+
117+
**`codebender_testing/capabilities.yaml`** defines a list of `capabilities` to
118+
be passed as arguments when instantiating remote webdrivers. In particular, it
119+
specifies the web browsers that we would like to use. Consult this file for more
120+
information.
121+
122+
#### `batch/`
123+
124+
The `batch/` directory contains any executable scripts not directly used to
125+
perform tests. For example, it contains a script `fetch_projects.py` which can
126+
be used to download all of the public projects of a particular codebender user.
127+
128+
#### `extensions/`
129+
130+
The `extensions/` directory contains the codebender browser extensions to be
131+
used by the Selenium webdrivers.
132+
133+
#### `test_data/`
134+
135+
The `test_data/` directory contains any data used for testing. For example, it
136+
contains example projects that we should successfully be able to upload and
137+
compile.
138+
139+
#### `logs/`
140+
141+
The `logs/` directory contains the results of running certain tests, e.g.
142+
whether certain sets of sketches have compiled successfully (see "Compilation
143+
Logs").
15144

16-
When running tox, you might get a `pkg_resources.DistributionNotFound` error
17-
with reference to `virtualenv`. This is likely due to an out of date setuptools.
18-
To fix this issue, run `$ sudo pip3 install -U setuptools`.
File renamed without changes.

batch/fetch_projects.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
"""A script to downloads all projects for a particular user."""
3+
4+
# This is necessary in order to run the script; otherwise the script needs to
5+
# be run as a python module (which is inconvenient).
6+
if __name__ == '__main__' and __package__ is None:
7+
from os import sys, path
8+
sys.path.append(path.join(path.dirname(path.abspath(__file__)), '..'))
9+
10+
11+
from urllib.request import urlopen
12+
from urllib.request import urlretrieve
13+
import argparse
14+
import os
15+
16+
from codebender_testing.config import LIVE_SITE_URL
17+
18+
from lxml import html
19+
20+
21+
def download_projects(url, user, path):
22+
connection = urlopen('/'.join([url, 'user', user]))
23+
dom = html.fromstring(connection.read().decode('utf8'))
24+
os.chdir(path)
25+
for link in dom.xpath('//table[@id="user_projects"]//a'):
26+
project_name = link.xpath('text()')[0]
27+
sketch_num = link.xpath('@href')[0].split(':')[-1]
28+
print("Downloading %s (sketch %s)" % (project_name, sketch_num))
29+
urlretrieve('%s/utilities/download/%s' % (url, sketch_num),
30+
os.path.join(path, '%s.zip' % project_name))
31+
32+
33+
if __name__ == "__main__":
34+
parser = argparse.ArgumentParser()
35+
parser.add_argument("user", help="the user whose projects we want to download")
36+
parser.add_argument("-u", "--url", help="url of the codebender site to use",
37+
default=LIVE_SITE_URL)
38+
parser.add_argument("-d", "--directory", help="output directory of the downloaded projects",
39+
default=".")
40+
args = parser.parse_args()
41+
download_projects(args.url, args.user, args.directory)

batch/requirements-batch.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Requirements for running batch scripts.
2+
3+
# If lxml fails to install in Ubuntu, install the following dependencies with
4+
# apt-get: libxml2-dev libxslt1-dev python3-dev
5+
6+
lxml

bin/env_vars.sh.template

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
SAUCELABS_USER=""
4+
SAUCELABS_KEY=""
5+
export CODEBENDER_SELENIUM_HUB_URL=http://${SAUCELABS_USER}:${SAUCELABS_KEY}@ondemand.saucelabs.com:80/wd/hub
6+
export CODEBENDER_TEST_USER=""
7+
export CODEBENDER_TEST_PASS=""
8+
export DISQUS_ACCESS_TOKEN=""
9+
export DISQUS_API_SECRET=""
10+
export DISQUS_API_PUBLIC=""
11+
export DISQUS_SSO_ID=""
12+
export DISQUS_SSO_USERNAME=""
13+
export DISQUS_SSO_EMAIL=""
14+
export EMAIL=""

bin/test_common.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
source ./env_vars.sh
4+
cd ..
5+
time tox tests/common -- --url=https://codebender.cc --source=codebender_cc
6+
RETVAL=$?
7+
cd -
8+
echo "tests return value: ${RETVAL}"
9+
exit ${RETVAL}

bin/test_examples.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
source ./env_vars.sh
4+
export CAPABILITIES='capabilities_firefox.yaml'
5+
export CODEBENDER_SELENIUM_HUB_URL="http://127.0.0.1:4444/wd/hub"
6+
cd ..
7+
time tox tests/libraries -- --url=https://codebender.cc --source=codebender_cc -F
8+
RETVAL=$?
9+
cd -
10+
echo "tests return value: ${RETVAL}"
11+
exit ${RETVAL}

bin/test_sketches.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
4+
source ./env_vars.sh
5+
export CAPABILITIES='capabilities_firefox.yaml'
6+
cd ..
7+
time tox tests/compile_tester -- --url=https://codebender.cc --source=codebender_cc -F
8+
RETVAL=$?
9+
cd -
10+
echo "tests return value: ${RETVAL}"
11+
exit ${RETVAL}

0 commit comments

Comments
 (0)