Skip to content

Instantly share code, notes, and snippets.

@aydinemre
Created February 27, 2021 21:39
Show Gist options
  • Save aydinemre/3a7cf3c75c7808ff80f6a514bde81820 to your computer and use it in GitHub Desktop.
Save aydinemre/3a7cf3c75c7808ff80f6a514bde81820 to your computer and use it in GitHub Desktop.
Docker Selenium Multi Client Test.
# docker run -d -p 4444:4444 selenium/standalone-chrome:4.0.0-beta-1-20210215
# python selenium_test.py
import uuid
from datetime import datetime
from multiprocessing import Pool
from random import randint
from time import sleep
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def get_remote_driver(max_try_count=10, sleep_time=10):
try_count = 0
driver_created = False
while try_count < max_try_count and not driver_created:
try:
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
driver_created = True
except Exception as e:
print(f"Exception={e}")
print("Sleeping 10 seconds...")
sleep(sleep_time)
if not driver_created:
raise RuntimeError("Can't connect to remote driver")
return driver
def throw_away_function(_):
return check_browser()
def check_browser():
sleep_time = randint(5, 10)
response = {'run_id': str(uuid.uuid4()), 'sleep_time': sleep_time, 'start': datetime.now()}
driver = get_remote_driver()
driver.get("http://google.com")
response['get_driver'] = datetime.now()
sleep(sleep_time)
assert "google" in driver.page_source
driver.quit()
response['end'] = datetime.now()
return response
if __name__ == '__main__':
results = Pool(2).map(throw_away_function, range(5)) # 2 process at same time.
result_df = pd.DataFrame(results)
result_df.to_csv('docker_selenium_test_results.csv', index=False)
print(result_df)
"""
run_id,sleep_time,start,get_driver,end
d3ac00a8-4746-4bf9-8a2c-73c8d767b246,10,2021-02-28 00:33:48.554172,2021-02-28 00:33:51.642149,2021-02-28 00:34:01.767759
3c9e73eb-8570-4347-aae3-40ccb517880d,7,2021-02-28 00:33:48.557115,2021-02-28 00:33:52.128206,2021-02-28 00:33:59.263834
7bec7587-8901-47e6-a288-4da4ceaff6a5,9,2021-02-28 00:33:59.264057,2021-02-28 00:34:01.581005,2021-02-28 00:34:10.714194
7c510c5c-f8cf-435b-ba3f-5f0d26661a51,7,2021-02-28 00:34:01.767972,2021-02-28 00:34:03.597832,2021-02-28 00:34:10.759991
262d160e-b4ac-4a04-b29a-e9fe09c477e1,7,2021-02-28 00:34:10.714399,2021-02-28 00:34:12.669645,2021-02-28 00:34:19.792564
"""
@aydinemre
Copy link
Author

aydinemre commented Feb 27, 2021

Start standalone chrome command: docker run -d -p 4444:4444 selenium/standalone-chrome:4.0.0-beta-1-20210215

@UmutAlihan
Copy link

very nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment