Robot Framework多测试用例共享单个浏览器会话可行性咨询
Absolutely feasible! The session loss issue you’re hitting boils down to how Robot Framework handles test isolation and library instances by default. Let’s unpack why your current approach isn’t working and walk through a clean, reliable solution.
What’s Causing the Session Crash?
Robot Framework creates a new instance of your Python test library for every test case by default. So when you store the driver in self.driver, that reference only exists for the single test case using that library instance. When the next test case runs, a fresh library instance spins up, and the old self.driver is gone—leaving you with no valid browser session.
The Proper Fix: Share the Driver via Suite-Level Variables
The core solution is to store your Selenium driver instance in a suite-level variable that persists across all test cases in your suite. Here’s how to implement it step by step:
1. Your Python Selenium Keyword Library
Create a Python file (e.g., CustomSelenium.py) to wrap your Selenium logic, using Robot’s BuiltIn library to manage the shared driver variable:
from robot.libraries.BuiltIn import BuiltIn from selenium import webdriver from selenium.webdriver.chrome.options import Options class CustomSelenium: def __init__(self): self.builtin = BuiltIn() def start_chrome_session(self): # Configure Chrome options (tweak as needed) chrome_options = Options() chrome_options.add_argument("--start-maximized") # Initialize the Chrome driver driver = webdriver.Chrome(options=chrome_options) # Save driver as a suite-wide variable self.builtin.set_suite_variable("${DRIVER}", driver) return driver def run_test_case_1(self): # Fetch the shared driver instance driver = self.builtin.get_variable_value("${DRIVER}") # Add your Test Case 1 logic here driver.get("https://example.com") assert driver.title == "Example Domain", "Test Case 1 Failed: Wrong page title" def run_test_case_2(self): # Reuse the same driver instance driver = self.builtin.get_variable_value("${DRIVER}") # Add your Test Case 2 logic here driver.find_element("link text", "More information").click() assert "iana.org" in driver.current_url, "Test Case 2 Failed: Did not navigate to correct page" def close_chrome_session(self): # Clean up the driver after the suite finishes driver = self.builtin.get_variable_value("${DRIVER}") if driver: driver.quit()
2. Your Robot Test Suite File
Set up your .robot file to use the suite-level setup/teardown and call your Python keywords:
*** Settings *** Library CustomSelenium.py Suite Setup Start Chrome Session Suite Teardown Close Chrome Session *** Variables *** # Optional: Define a default value for ${DRIVER} (not strictly required) ${DRIVER} None *** Test Cases *** Execute Test Case 1 Run Test Case 1 Execute Test Case 2 Run Test Case 2
Why This Works
Start Chrome Sessionruns once at the start of the entire suite, initializing the driver and storing it in${DRIVER}—a variable that’s accessible to every test case in the suite.- Each test case retrieves the same driver instance instead of relying on per-test library instance variables, so the browser session stays intact between tests.
Suite Teardownensures the browser is properly closed after all tests finish, preventing leftover processes.
Note on Your Temporary Solution
Your current approach of passing ${DRIVER} to each test case works, but using set_suite_variable is more elegant—it removes the need to explicitly pass the driver variable to every keyword or test case, as it’s automatically available throughout the suite.
内容的提问来源于stack exchange,提问作者Seth

