Skip to content

Commit 2cf09ac

Browse files
Merge pull request #4 from BrowserStackCE/Santosh_update
updated the code with latest changes
2 parents 213a3a0 + 4f24d7f commit 2cf09ac

File tree

4 files changed

+65
-56
lines changed

4 files changed

+65
-56
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,49 @@ This project uses Selenium and Percy to capture visual snapshots of multiple URL
99

1010
## Prerequisites
1111

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
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
1616

1717
## Setup
1818

19-
### Step 1: Install Percy CLI via npm
19+
### Step 1: Create a Python virtual environment
20+
21+
It's recommended to create a virtual environment to manage dependencies isolated from your system Python.
22+
23+
```bash
24+
python3 -m venv venv
25+
source venv/bin/activate
26+
```
27+
28+
### Step 2: Install Percy CLI via npm
2029

2130
The Percy CLI is needed to capture and upload snapshots. Install it via npm:
2231

2332
```bash
2433
npm install
2534
```
2635

27-
### Step 2: Install Python dependencies
36+
### Step 3: Install Python dependencies
2837

38+
With the virtual environment activated, install the required Python packages:
2939

3040
```bash
3141
pip3 install -r requirements.txt
3242
```
3343

34-
3544
## Usage
3645

3746
### Step 1: Update the `urls.csv` file
3847

3948
This file contains all the URLs you need to capture using Percy.
4049

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.
50+
### Step 2: Run the file to capture snapshots using Percy
4651

4752
Export the Percy Token located in your project settings of Percy and then run the python command to initiate the execution.
4853

4954
```bash
5055
export PERCY_TOKEN=your-percy-token
5156
npx percy exec -- python3 batchProcess.py
52-
```
53-
54-
55-
56-
57-
57+
```

batchProcess.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,80 @@
11
import csv
2-
import os
32
from selenium import webdriver
43
from selenium.webdriver.chrome.service import Service
4+
from webdriver_manager.chrome import ChromeDriverManager
55
from percy import percy_snapshot
66
from time import sleep
77
from concurrent.futures import ThreadPoolExecutor
8+
from selenium.webdriver.chrome.options import Options
9+
from urllib.parse import urlparse
10+
import re
811

9-
CSV_FILE = './urls.csv' # Path to your CSV file
10-
NUM_THREADS = 5 # Number of parallel threads
11-
CHROMEDRIVER_PATH = "./chromedriver"
12+
CSV_FILE = './urls.csv' # Path to your CSV file
13+
NUM_THREADS = 2 # Number of parallel threads
1214

1315
# Load URLs from CSV
1416
def load_urls():
1517
with open(CSV_FILE, newline='') as file:
1618
reader = csv.reader(file)
17-
return [row[0].strip() for row in reader if row[0].strip().startswith(("http://", "https://"))]
19+
return [row[0].strip() for row in reader if row and row[0].strip().startswith(("http://", "https://"))]
20+
21+
# Helper to split list into n even chunks
22+
def split_list(lst, n):
23+
k, m = divmod(len(lst), n)
24+
return [lst[i*k + min(i, m):(i+1)*k + min(i+1, m)] for i in range(n)]
1825

1926
# Function for each thread to process its batch of URLs
2027
def process_urls(urls):
2128
if not urls:
2229
print("No URLs provided to process.")
2330
return
24-
25-
service = Service(CHROMEDRIVER_PATH)
26-
driver = webdriver.Chrome(service=service)
31+
# Use webdriver-manager to automatically install Chromedriver
32+
options = Options()
33+
options.add_argument("--headless=new") # optional but recommended for Percy
34+
options.add_argument("--no-sandbox")
35+
options.add_argument("--disable-dev-shm-usage")
36+
37+
service = Service(ChromeDriverManager(driver_version="139.0.7258.155").install())
38+
driver = webdriver.Chrome(service=service, options=options)
39+
driver.set_window_size(1200, 800)
2740
try:
2841
for url in urls:
2942
print(f"Loading URL: {url}")
3043
driver.get(url)
31-
sleep(2)
44+
sleep(2)
45+
46+
parsed_url = urlparse(url)
47+
hostname = parsed_url.netloc
48+
if hostname.startswith("www."):
49+
hostname = hostname[4:]
50+
51+
# Sanitize path: remove leading slash and replace other slashes with underscores
52+
path = parsed_url.path.lstrip('/')
53+
sanitized_path = re.sub(r'[^a-zA-Z0-9_-]', '_', path) # Replace non-alphanum/underscore/dash chars
54+
55+
# Construct snapshot name
56+
if sanitized_path:
57+
snapshot_name = f"Snapshot for {hostname}_{sanitized_path}"
58+
else:
59+
snapshot_name = f"Snapshot for {hostname}"
3260

33-
# Capture Percy snapshot
34-
snapshot_name = f"Snapshot for {url}"
3561
print(f"Capturing Percy snapshot: {snapshot_name}")
36-
percy_snapshot(driver, snapshot_name)
62+
percy_snapshot(driver, snapshot_name,widths=[768, 1200])
63+
3764
finally:
38-
driver.quit() # Ensure the driver closes after the batch is done
65+
driver.quit()
3966

4067
def main():
4168
urls = load_urls()
69+
if not urls:
70+
print("No URLs found in the CSV file.")
71+
return
4272

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)]
73+
url_batches = split_list(urls, NUM_THREADS)
4674

47-
# Process each batch in parallel
4875
with ThreadPoolExecutor(max_workers=NUM_THREADS) as executor:
49-
futures = [executor.submit(process_urls, batch) for batch in url_batches]
50-
76+
# Submit only non-empty batches
77+
futures = [executor.submit(process_urls, batch) for batch in url_batches if batch]
5178
for future in futures:
5279
future.result()
5380

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ typing_extensions==4.12.2
1616
urllib3==2.2.3
1717
websocket-client==1.8.0
1818
wsproto==1.2.0
19+
webdriver-manager==4.0.2

urls.csv

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
11
URLs
2-
browserstack.com/docs
3-
browserstack.com/
42
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)