################## selenium grid config start #######################
# UI自动化测试策略 Grid/Local
UIAutomation_TestStrategy = 'Grid'
selenium_grid_url = 'http://172.16.99.131:4444/wd/hub'
# Windows XP / linux
grid_platformName = 'linux'
# windows capabilities
windows_capabilities = {"platformName": "Windows XP", "browserName": "chrome"}
# linux capabilities
linux_capabilities = {"browserName": "chrome","browserVersion": "130.0","goog:chromeOptions": {"binary": "/usr/bin/google-chrome"},"platformName": "linux","se:containerName": "","se:noVncPort": 7900,"se:vncEnabled": True
}
proxy = "http://172.21.16.96:808"
################ selenium grid config end #########################
远程多节点运行:
from conf import config
import time
import concurrent.futures
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdef run_test_on_grid():hub_url = config.selenium_grid_urloptions = webdriver.ChromeOptions()options.add_argument('--ignore-certificate-errors')options.add_argument('--ignore-ssl-errors')options.add_argument('--ignore-urlfetcher-cert-requests')options.add_argument('--disable-dev-shm-usage')options.add_argument('--ignore-certificate-errors-spki-list')options.add_argument('--no-sandbox') # 在沙箱模式下运行浏览器,沙箱模式下的浏览器会忽略某些安全警告# # 95和96配置# if config.grid_platformName == "linux":# capabilities = config.linux_capabilities# # Configure proxy if needed# proxy = config.proxy# options.add_argument(f'--proxy-server={proxy}')# else:# capabilities = config.windows_capabilities## # ubuntu配置## # Merge capabilities for ubuntu## for key, value in capabilities.items():# options.set_capability(key, value)driver = webdriver.Remote(command_executor=hub_url, options=options)driver.get("www.baidu.com")print(driver.title)time.sleep(10)return driverif __name__ == "__main__":drivers = []try:with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:futures = [executor.submit(run_test_on_grid) for _ in range(2)]for future in concurrent.futures.as_completed(futures):drivers.append(future.result())except Exception as e:print(e)finally:for driver in drivers:driver.quit()