如何使用Selenium测试两个独立URL的集成及前后端文档流转验证
Got it, let's walk through a practical, reliable Selenium test plan for this cross-app integration scenario—where you need to upload and approve a document in the admin portal, then verify it shows up correctly in the frontend, all within the same session. Here's how to structure it:
Test Plan for Admin-to-Frontend Document Sync Verification
1. Pre-Test Setup
First, get your environment and assets ready to avoid roadblocks:
- Install Selenium WebDriver matching your target browser (e.g., ChromeDriver for Chrome) and a test framework (pytest for Python, TestNG/JUnit for Java)
- Prepare a test document (PDF, Word, etc.) with known attributes: fixed filename, clear content, and any required metadata (like category or description)
- Confirm your admin test account has full permissions to upload and approve documents
- Ensure the admin portal (
www.admin.com) and frontend app are connected in your test environment (data syncs in real-time or with minimal delay)
2. Core Test Steps (Same Session Throughout)
Never spin up a new WebDriver instance mid-test—keep the same session to maintain shared cookies/authentication between admin and frontend.
Step 2.1: Admin Portal Operations
- Initialize your WebDriver instance (this will be used for the entire test flow)
- Navigate to
www.admin.comand log in with your admin credentials - Locate the document upload interface:
- Click the upload button, use the file input element to select your prepped test document
- Fill in required metadata (title, category, etc.) and submit the upload
- Wait for a success confirmation (use explicit waits, not hard sleeps!) before moving on
- Navigate to the pending documents queue, find your test document, and complete the approval (e.g., click "Approve" button)
- Verify the approval success message is displayed
Step 2.2: Frontend App Verification
- In the same WebDriver session, either open a new tab or directly navigate to the frontend app URL
- Go to the document listing page (or the specific section where approved docs are displayed)
- Validate these critical details:
- The test document’s filename matches exactly what you uploaded
- The document status shows as "Approved" (or the frontend’s equivalent text)
- Optional: If preview is available, check that the preview content matches your test document
- Optional: Click into the document’s detail page to confirm metadata (title, category) matches what you entered in the admin portal
3. Key Best Practices to Avoid Failures
- Session Persistence: Always use the same WebDriver object—new instances will break shared authentication, and the frontend won’t see the freshly approved document
- Explicit Waits: Replace
time.sleep()withWebDriverWait(or framework-specific waits) to wait for elements to load, uploads to complete, and statuses to update. This prevents flaky tests - Data Cleanup: Add a teardown step to delete the test document after the test finishes (e.g., navigate back to admin, find the doc, and delete it) to keep your test environment clean
- Stable Locators: Use IDs, names, or stable CSS selectors instead of fragile absolute XPaths. If you must use XPath, use relative paths with unique text/attributes
- Headless Mode: For CI/CD pipelines, run tests in headless browser mode to save resources and run without a GUI
Example Code Snippet (Python + pytest)
Here’s a simplified code example to illustrate the flow:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def test_admin_upload_frontend_verify(): # Initialize driver (single instance for entire test) driver = webdriver.Chrome() wait = WebDriverWait(driver, 15) test_doc_path = "/path/to/your/test-document.pdf" test_doc_name = "Sample-Test-Doc.pdf" frontend_url = "www.frontend.com/documents" try: # Admin Login & Upload driver.get("www.admin.com") wait.until(EC.presence_of_element_located((By.ID, "username"))).send_keys("admin-test") wait.until(EC.presence_of_element_located((By.ID, "password"))).send_keys("test-pass") wait.until(EC.element_to_be_clickable((By.ID, "login-btn"))).click() # Upload document wait.until(EC.element_to_be_clickable((By.ID, "upload-trigger"))).click() driver.find_element(By.ID, "file-input").send_keys(test_doc_path) wait.until(EC.presence_of_element_located((By.ID, "doc-title"))).send_keys("Test Document Title") wait.until(EC.element_to_be_clickable((By.ID, "submit-upload"))).click() wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "upload-success"))) # Approve document wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Pending Approvals"))).click() wait.until(EC.element_to_be_clickable((By.XPATH, f"//tr[contains(text(), '{test_doc_name}')]//button[text()='Approve']"))).click() wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "approval-success"))) # Frontend Verification (same session) driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get(frontend_url) # Validate document presence and status doc_element = wait.until(EC.presence_of_element_located((By.XPATH, f"//div[contains(@class, 'doc-item') and contains(text(), '{test_doc_name}')]"))) assert doc_element.is_displayed(), "Test document not found on frontend" doc_status = doc_element.find_element(By.CLASS_NAME, "doc-status").text assert doc_status == "Approved", f"Expected 'Approved' status, got '{doc_status}'" finally: # Cleanup: Delete test document and close driver # Add your cleanup logic here (navigate back to admin, delete the doc) driver.quit()
内容的提问来源于stack exchange,提问作者Deepak Chand Rai
相关产品推荐
相关产品推荐

