如何为所有Feature文件全局配置SSL证书
Hey there! Let's fix that tedious repeated SSL config in every Feature file—here are practical, stack-specific solutions to set this globally for your automation project:
If you're using Cucumber, a global @Before hook is the quickest way to apply SSL settings once for all tests. This hook runs before every Scenario, so your SSL config will be active across all Feature files.
Example (Java):
import io.cucumber.java.Before; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.RemoteWebDriver; public class GlobalTestHooks { @Before public void initializeGlobalSSL() { ChromeOptions chromeOpts = new ChromeOptions(); // Inject your SSL config into the driver capabilities chromeOpts.setCapability("ssl", "{ keyStore: 'wmcloudPreProd2_truststore.jks', keyStorePassword: 'manage', keyStoreType: 'jks' }"); // Store this configured driver in a global context (like a ThreadLocal) so all steps can access it DriverManager.setDriver(new RemoteWebDriver(chromeOpts)); } }
Example (JavaScript/TypeScript):
// hooks.js const { Before } = require('@cucumber/cucumber'); const { Builder } = require('selenium-webdriver'); const chrome = require('selenium-webdriver/chrome'); Before(function() { const options = new chrome.Options(); options.setCapability('ssl', { keyStore: 'wmcloudPreProd2_truststore.jks', keyStorePassword: 'manage', keyStoreType: 'jks' }); this.driver = new Builder().forBrowser('chrome').setChromeOptions(options).build(); });
For better maintainability, store your SSL config in a dedicated file (like config.properties or config.json) and load it once at test startup. This lets you update settings without touching test code.
Step 1: Create a config file (config.properties)
ssl.keyStore=wmcloudPreProd2_truststore.jks ssl.keyStorePassword=manage ssl.keyStoreType=jks
Step 2: Load the config globally (Java example)
import java.io.InputStream; import java.util.Properties; public class GlobalConfig { private static final Properties props = new Properties(); static { // Load the config file on class initialization try (InputStream is = GlobalConfig.class.getClassLoader().getResourceAsStream("config.properties")) { props.load(is); } catch (Exception e) { throw new RuntimeException("Failed to load global config", e); } } // Getters for SSL properties public static String getSSLKeyStore() { return props.getProperty("ssl.keyStore"); } public static String getSSLKeyStorePassword() { return props.getProperty("ssl.keyStorePassword"); } public static String getSSLKeyStoreType() { return props.getProperty("ssl.keyStoreType"); } }
Step 3: Apply the config in your hooks/driver setup
@Before public void setupSSL() { ChromeOptions options = new ChromeOptions(); options.setCapability("ssl", String.format( "{ keyStore: '%s', keyStorePassword: '%s', keyStoreType: '%s' }", GlobalConfig.getSSLKeyStore(), GlobalConfig.getSSLKeyStorePassword(), GlobalConfig.getSSLKeyStoreType() )); // Initialize driver with these options }
If you're using RestAssured for API automation, you can set SSL config globally once, and all requests will inherit it:
import io.restassured.RestAssured; import io.restassured.config.SSLConfig; import org.junit.BeforeClass; public class APIGlobalSetup { @BeforeClass public static void setGlobalSSL() { RestAssured.config = RestAssured.config() .sslConfig(SSLConfig.sslConfig() .keyStore(GlobalConfig.getSSLKeyStore(), GlobalConfig.getSSLKeyStorePassword(), GlobalConfig.getSSLKeyStoreType())); } }
Ensure this class runs before any tests (use @BeforeClass in JUnit, or add it to your test suite).
If you need to switch SSL config across environments (dev/prod), use environment variables. This keeps sensitive data (like passwords) out of code and makes CI/CD setup easier.
Set environment variables (bash example):
export SSL_KEYSTORE=wmcloudPreProd2_truststore.jks export SSL_KEYSTORE_PASSWORD=manage export SSL_KEYSTORE_TYPE=jks
Read variables in your code (Java example):
@Before public void setupSSLFromEnv() { String keyStore = System.getenv("SSL_KEYSTORE"); String keyStorePass = System.getenv("SSL_KEYSTORE_PASSWORD"); String keyStoreType = System.getenv("SSL_KEYSTORE_TYPE"); ChromeOptions options = new ChromeOptions(); options.setCapability("ssl", String.format( "{ keyStore: '%s', keyStorePassword: '%s', keyStoreType: '%s' }", keyStore, keyStorePass, keyStoreType )); // Initialize driver }
Whichever approach you choose, the key is to load the SSL config once, before any test scenarios run—this ensures every Feature file uses the same global settings without repetition.
内容的提问来源于stack exchange,提问作者Anupama

