Skip to content

Commit 2c1c7ac

Browse files
Percy on Automate Setup on Robot
0 parents  commit 2c1c7ac

6 files changed

Lines changed: 207 additions & 0 deletions

File tree

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# BrowserStack Integration with Robot Framework Selenium (Python)
2+
3+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
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)

browserstack.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
userName:
2+
accessKey:
3+
projectName: BrowserStack Samples pytest robot
4+
buildName: browserstack build pytest robot
5+
buildIdentifier: '#${BUILD_NUMBER}'
6+
7+
platforms:
8+
- deviceName: Samsung Galaxy S22 Ultra
9+
browserName: chrome
10+
osVersion: 12.0
11+
- deviceName: iPhone 15
12+
osVersion: 26
13+
browserName: safari
14+
deviceOrientation: portrait
15+
16+
parallelsPerPlatform: 1
17+
browserstackLocal: false
18+
source: robot-browserstack:sample-sdk:v1.0
19+
debug: false
20+
networkLogs: false
21+
consoleLogs: errors
22+

resources/KeywordsFile.robot

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
*** Settings ***
2+
Library SeleniumLibrary
3+
Library PercyLibrary.py
4+
5+
*** Variables ***
6+
${remote_url}= http://localhost:4444/wd/hub
7+
8+
*** Keywords ***
9+
Open Session
10+
[Arguments] ${capabilities} ${test_url}
11+
open browser remote_url=${remote_url} browser=chrome desired_capabilities=${capabilities} url=${test_url}
12+
13+
Close Session
14+
close browser
15+
16+
Add Implicit Wait
17+
set selenium implicit wait 5
18+
19+
Get the page title
20+
get title
21+
22+
Verify Local Page
23+
Title Should be BrowserStack Local
24+
25+
Add first product to cart
26+
click element xpath=//*[@id="1"]/div[4]
27+
28+
Verify product is added to cart
29+
${product_name} get text xpath=//*[@id="1"]/p
30+
${product_incart} get text css=p.title
31+
element should contain css=p.title ${product_name}

resources/PercyLibrary.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Robot Framework keyword library for Percy snapshots.
2+
3+
This library provides a simple keyword to invoke Percy snapshots using the
4+
currently active SeleniumLibrary driver instance.
5+
6+
Usage in Robot Framework:
7+
Library PercyLibrary.py
8+
9+
Percy Snapshot My Snapshot Name
10+
11+
Make sure PERCY_TOKEN is set in your environment when running tests.
12+
"""
13+
14+
from robot.api.deco import keyword
15+
from robot.libraries.BuiltIn import BuiltIn
16+
from percy import percy_screenshot
17+
18+
19+
class PercyLibrary:
20+
@keyword("Percy Snapshot")
21+
def percy_snapshot(self, name: str):
22+
"""Take a Percy snapshot using the current SeleniumLibrary driver.
23+
24+
The SeleniumLibrary browser instance is retrieved via BuiltIn.get_library_instance.
25+
"""
26+
selib = BuiltIn().get_library_instance("SeleniumLibrary")
27+
driver = selib.driver
28+
percy_screenshot(driver, name)

resources/TestCases.robot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*** Settings ***
2+
Library SeleniumLibrary
3+
Resource ./KeywordsFile.robot
4+
5+
*** Keywords ***
6+
Add to Cart
7+
Add Implicit Wait
8+
Get the page title
9+
Add first product to cart
10+
Percy Snapshot After Add to Cart
11+
Verify product is added to cart

tests/test-1.robot

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
*** Settings ***
2+
Library SeleniumLibrary
3+
Resource ../resources/KeywordsFile.robot
4+
Resource ../resources/TestCases.robot
5+
Test Setup Execute test
6+
Test Teardown Close Session
7+
8+
9+
*** Variables ***
10+
${website_url}= https://bstackdemo.com
11+
&{test_caps} browser=chrome
12+
13+
14+
*** Keywords ***
15+
Execute test
16+
Open Session ${test_caps} ${website_url}
17+
18+
19+
*** Test Cases ***
20+
BStack Sample Test 1
21+
Add to Cart

0 commit comments

Comments
 (0)