Skip to content

Commit d6a0b7c

Browse files
First Commit
1 parent c4fc85e commit d6a0b7c

File tree

6 files changed

+169
-1
lines changed

6 files changed

+169
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+
chromedriver

README.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
# python-csv-percy
1+
# Percy Batch URL Snapshotter
2+
3+
This project uses Selenium and Percy to capture visual snapshots of multiple URLs in batches using Python. By leveraging parallel threading, it efficiently processes large lists of URLs, uploading snapshots to Percy for visual testing.
4+
5+
## Table of Contents
6+
- [Prerequisites](#prerequisites)
7+
- [Setup](#setup)
8+
- [Usage](#usage)
9+
10+
## Prerequisites
11+
12+
- **Python**: Version 3.7 or higher
13+
- **Percy CLI**: Required for snapshot uploading (see installation steps below)
14+
- **ChromeDriver**: Ensure it matches your installed Chrome version
15+
- **Percy Account**: [Sign up for a Percy account](https://percy.io) to get your project token
16+
17+
## Setup
18+
19+
### Step 1: Install Percy CLI via npm
20+
21+
The Percy CLI is needed to capture and upload snapshots. Install it via npm:
22+
23+
```bash
24+
npm install
25+
```
26+
27+
### Step 2: Install Python dependencies
28+
29+
30+
```bash
31+
pip3 install -r requirements.txt
32+
```
33+
34+
35+
## Usage
36+
37+
### Step 1: Update the `urls.csv` file
38+
39+
This file contains all the URLs you need to capture using Percy.
40+
41+
### Step 2: Update the `CHROMEDRIVER_PATH` variable in `batchProcess.py` file
42+
43+
This will point the selenium test to your chromedrive to successfully launch the Chrome Browser. You can also update the variable `NUM_THREADS` if you want to increase the number of parallel threads.
44+
45+
### Step 3: Run the file to capture snapshots using Percy.
46+
47+
Export the Percy Token located in your project settings of Percy and then run the python command to initiate the execution.
48+
49+
```bash
50+
export PERCY_TOKEN=your-percy-token
51+
npx percy exec -- python3 batchProcess.py
52+
```
53+
54+
55+
56+
57+

batchProcess.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import csv
2+
import os
3+
from selenium import webdriver
4+
from selenium.webdriver.chrome.service import Service
5+
from percy import percy_snapshot
6+
from time import sleep
7+
from concurrent.futures import ThreadPoolExecutor
8+
9+
CSV_FILE = './urls.csv' # Path to your CSV file
10+
NUM_THREADS = 5 # Number of parallel threads
11+
CHROMEDRIVER_PATH = "./chromedriver"
12+
13+
# Load URLs from CSV
14+
def load_urls():
15+
with open(CSV_FILE, newline='') as file:
16+
reader = csv.reader(file)
17+
return [row[0].strip() for row in reader if row[0].strip().startswith(("http://", "https://"))]
18+
19+
# Function for each thread to process its batch of URLs
20+
def process_urls(urls):
21+
if not urls:
22+
print("No URLs provided to process.")
23+
return
24+
25+
service = Service(CHROMEDRIVER_PATH)
26+
driver = webdriver.Chrome(service=service)
27+
try:
28+
for url in urls:
29+
print(f"Loading URL: {url}")
30+
driver.get(url)
31+
sleep(2)
32+
33+
# Capture Percy snapshot
34+
snapshot_name = f"Snapshot for {url}"
35+
print(f"Capturing Percy snapshot: {snapshot_name}")
36+
percy_snapshot(driver, snapshot_name)
37+
finally:
38+
driver.quit() # Ensure the driver closes after the batch is done
39+
40+
def main():
41+
urls = load_urls()
42+
43+
# Split URLs into batches based on the number of threads
44+
batch_size = len(urls) // NUM_THREADS
45+
url_batches = [urls[i:i + batch_size] for i in range(0, len(urls), batch_size)]
46+
47+
# Process each batch in parallel
48+
with ThreadPoolExecutor(max_workers=NUM_THREADS) as executor:
49+
futures = [executor.submit(process_urls, batch) for batch in url_batches]
50+
51+
for future in futures:
52+
future.result()
53+
54+
if __name__ == "__main__":
55+
main()

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "python-csv-python",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"author": "",
9+
"license": "ISC",
10+
"description": "",
11+
"devDependencies": {
12+
"@percy/cli": "^1.30.1"
13+
}
14+
}
15+

requirements.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
attrs==24.2.0
2+
certifi==2024.8.30
3+
charset-normalizer==3.4.0
4+
h11==0.14.0
5+
idna==3.10
6+
outcome==1.3.0.post0
7+
percy-selenium==2.1.1
8+
PySocks==1.7.1
9+
requests==2.32.3
10+
selenium==4.25.0
11+
sniffio==1.3.1
12+
sortedcontainers==2.4.0
13+
trio==0.27.0
14+
trio-websocket==0.11.1
15+
typing_extensions==4.12.2
16+
urllib3==2.2.3
17+
websocket-client==1.8.0
18+
wsproto==1.2.0

urls.csv

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
URLs
2+
browserstack.com/docs
3+
browserstack.com/
4+
https://www.browserstack.com/docs/app-percy/integrate-bstack-sdk/webdriverio
5+
https://www.browserstack.com/docs/app-percy
6+
https://www.browserstack.com/docs/app-percy/overview/visual-testing-basics
7+
https://www.browserstack.com/docs/app-percy/overview/plans-and-billing
8+
https://www.browserstack.com/docs/app-percy/get-started/recommended-guidelines
9+
https://www.browserstack.com/docs/app-percy/source-code-integrations/overview
10+
https://www.browserstack.com/docs/app-percy/source-code-integrations/github
11+
https://www.browserstack.com/docs/app-automate/appium
12+
https://www.browserstack.com/docs/app-automate/appium/getting-started/java/integrate-your-tests
13+
https://www.browserstack.com/docs/app-automate/appium/getting-started/java/local-testing
14+
https://www.browserstack.com/docs/app-automate/appium/getting-started/java/parallelize-tests
15+
https://www.browserstack.com/docs/app-automate/appium/sdk-benefits
16+
https://www.browserstack.com/docs/app-automate/appium/how-sdk-works
17+
https://www.browserstack.com/docs/app-automate/appium/sdk-params
18+
https://www.browserstack.com/docs/app-automate/appium/sdk-faqs
19+
https://www.browserstack.com/docs/app-automate/appium/set-up-test-env
20+
https://www.browserstack.com/docs/app-automate/appium/set-up-test-env/upload-and-manage-apps
21+
https://www.browserstack.com/docs/app-automate/appium/upload-app-using-public-url

0 commit comments

Comments
 (0)