|
| 1 | +# BrowserStack Integration with Robot Framework Selenium (Python) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +A minimal example repo showing how to run Robot Framework Selenium tests on **BrowserStack Automate**, with optional visual testing using **Percy (Percy on Automate)**. |
| 6 | + |
| 7 | +It includes sample Robot tests, a BrowserStack config file (`browserstack.yml`), and the commands needed to run tests via `browserstack-sdk` and `percy exec`. |
| 8 | + |
| 9 | +## Prerequisite |
| 10 | +``` |
| 11 | +python3 should be installed |
| 12 | +``` |
| 13 | + |
| 14 | +## Setup |
| 15 | +* Clone the repo |
| 16 | + ``` |
| 17 | + git clone -b sdk https://github.com/browserstack/robot-browserstack.git |
| 18 | + ``` |
| 19 | +* It is recommended to use a virtual environment to install dependencies. To create a virtual environment: |
| 20 | + ``` |
| 21 | + python3 -m venv env |
| 22 | + source env/bin/activate # on Mac |
| 23 | + env\Scripts\activate # on Windows |
| 24 | + ``` |
| 25 | +* Install dependencies |
| 26 | + ``` |
| 27 | + pip install -r requirements.txt |
| 28 | + ``` |
| 29 | + |
| 30 | +## Set BrowserStack Credentials |
| 31 | +* Add your BrowserStack username and access key in the `browserstack.yml` config fle. |
| 32 | +* You can also export them as environment variables, `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY`: |
| 33 | + |
| 34 | + #### For Linux/MacOS |
| 35 | + ``` |
| 36 | + export BROWSERSTACK_USERNAME=<browserstack-username> |
| 37 | + export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key> |
| 38 | + ``` |
| 39 | + #### For Windows |
| 40 | + ``` |
| 41 | + set BROWSERSTACK_USERNAME=<browserstack-username> |
| 42 | + set BROWSERSTACK_ACCESS_KEY=<browserstack-access-key> |
| 43 | + |
| 44 | + setx BROWSERSTACK_USERNAME=<browserstack-username> |
| 45 | + setx BROWSERSTACK_ACCESS_KEY=<browserstack-access-key> |
| 46 | + ``` |
| 47 | +
|
| 48 | +## Running tests |
| 49 | +
|
| 50 | +* To run sample tests: |
| 51 | + - To run the sample tests in parallel across the platforms defined in the `browserstack.yml` file, run: |
| 52 | + ``` |
| 53 | + browserstack-sdk robot ./tests/test-*.robot |
| 54 | + ``` |
| 55 | +* To run tests on locally hosted websites: |
| 56 | + - To run the local test in parallel across the platforms defined in the `browserstack.yml` file, run: |
| 57 | + ``` |
| 58 | + browserstack-sdk robot ./tests/local-test.robot |
| 59 | + ``` |
| 60 | +
|
| 61 | +## Percy visual testing (BrowserStack + Percy on Automate) |
| 62 | +
|
| 63 | +The repository can be run through Percy to capture visual snapshots in your BrowserStack Automate runs. |
| 64 | +
|
| 65 | +1. Install Percy CLI (recommended global install): |
| 66 | + ``` |
| 67 | + npm install -g @percy/cli |
| 68 | + ``` |
| 69 | +
|
| 70 | +2. Install Percy SDK for Selenium (required for Percy snapshots): |
| 71 | + ``` |
| 72 | + pip install percy-selenium |
| 73 | + ``` |
| 74 | +
|
| 75 | +3. Export your Percy token (from your Percy project settings): |
| 76 | + ``` |
| 77 | + export PERCY_TOKEN="<your token here>" |
| 78 | + ``` |
| 79 | +
|
| 80 | +4. Run your Robot Framework tests through Percy: |
| 81 | + ``` |
| 82 | + npx percy exec --verbose -- browserstack-sdk robot ./tests/test-1.robot |
| 83 | + ``` |
| 84 | +
|
| 85 | +> **Note:** Percy snapshots are captured using `percy_screenshot` from `percy_selenium`. For Python-based Selenium tests, you can add `from percy import percy_screenshot` and call `percy_screenshot(driver, name="...")` in your test script. For Robot Framework tests, add a corresponding keyword in your test library that calls into the Percy SDK. |
| 86 | +
|
| 87 | +Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) |
| 88 | +
|
| 89 | +## Additional Resources |
| 90 | +* [Documentation for writing Automate test scripts in Python](https://www.browserstack.com/automate/python) |
| 91 | +* [Customizing your tests on BrowserStack](https://www.browserstack.com/automate/capabilities) |
| 92 | +* [Browsers & mobile devices for selenium testing on BrowserStack](https://www.browserstack.com/list-of-browsers-and-platforms?product=automate) |
| 93 | +* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api) |
| 94 | +* [Percy + BrowserStack Automate guide](https://www.browserstack.com/docs/percy/selenium/getting-started/python/integrate-your-tests?fw-lang=python#Percy_with_Automate) |
0 commit comments