You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Selenium测试的Python文件中自动递增更新URL?

Automatically Increment Selenium Test URLs for New Builds

Absolutely! You’ve got several practical ways to automate this URL update—no manual editing of your Python files needed. Here are the most common approaches tailored to different workflow setups:

This is the cleanest method if you’re using a CI/CD tool like Jenkins, GitHub Actions, or GitLab CI. Instead of hardcoding the build number in your test file, pull it from an environment variable that your pipeline sets automatically.

Step 1: Update your Selenium test code

Modify your Python file to generate the URL dynamically:

import os
from selenium import webdriver

# Get build number from environment variable, default to 1 if not set
build_number = os.getenv("BUILD_NUMBER", "1")
base_url = f"https://build{build_number}.test.com"

driver = webdriver.Chrome()
driver.get(base_url)
# Rest of your test logic...

Step 2: Configure your CI/CD pipeline

In your pipeline config (e.g., github-actions.yml, Jenkinsfile), set the BUILD_NUMBER variable to your current build iteration. Most CI tools have a built-in variable for this—for example:

  • GitHub Actions uses ${{ github.run_number }}
  • Jenkins uses BUILD_NUMBER (already available by default)
  • GitLab CI uses ${CI_PIPELINE_IID}

Just pass this variable to your test execution step, and your URL will auto-increment with every pipeline run.

2. Use a Config File with an Increment Script

If you don’t use a CI/CD pipeline, or prefer a file-based approach, store the current build number in a config file (JSON, YAML, or even a plain text file) and write a small script to increment it before running tests.

Step 1: Create a config file

Make a config.json file in your project:

{
  "current_build": 1
}

Step 2: Update test code to read the config

import json
from selenium import webdriver

# Load build number from config file
with open("config.json", "r") as f:
    config = json.load(f)
build_number = config["current_build"]
base_url = f"https://build{build_number}.test.com"

driver = webdriver.Chrome()
driver.get(base_url)
# Test logic...

Step 3: Write an increment script

Create a small Python script (increment_build.py) to bump the build number:

import json

with open("config.json", "r") as f:
    config = json.load(f)

config["current_build"] += 1

with open("config.json", "w") as f:
    json.dump(config, f, indent=2)

Run this script right before your tests when a new build is released:

python increment_build.py && python your_selenium_test.py

3. Pull Build Number from Git Tags (If Using Versioned Releases)

If you tag each new build with a version (e.g., v1, v2, v3), you can extract the number directly from Git tags to generate your URL.

Example code to get Git tag number:

import subprocess
from selenium import webdriver

# Get the latest Git tag (e.g., "v3")
result = subprocess.run(
    ["git", "describe", "--abbrev=0", "--tags"],
    capture_output=True,
    text=True
)
latest_tag = result.stdout.strip()
# Extract the numeric part from the tag
build_number = latest_tag.lstrip("v")

base_url = f"https://build{build_number}.test.com"
driver = webdriver.Chrome()
driver.get(base_url)

Key Notes:

  • Always include a default value (like "1") in case the build number isn’t available (e.g., local testing).
  • For CI/CD pipelines, ensure your build number increments sequentially—most tools handle this automatically, but double-check your pipeline settings.
  • If you need to roll back to an older build, just override the environment variable or config file value temporarily.

内容的提问来源于stack exchange,提问作者Cal Corbin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:31:34