|
1 | 1 | # Codebender Selenium Tests
|
2 | 2 |
|
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. |
5 | 6 |
|
6 | 7 | ## Running Tests
|
7 | 8 |
|
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`. |
11 | 11 |
|
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"). |
15 | 144 |
|
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`. |
|
0 commit comments