我已经使用Maven,Java和TestNG框架在本地计算机上为我的项目创建了WebApp测试自动化框架。我们的CI / CD中有TeamCity,而我的团队希望该框架使用TeamCity的环境变量(包括用户名并传递到测试中的WebApp),而不是在我的项目“ configuration.properties”文件中硬编码的属性。
在我的项目中用于属性配置的机制如下:
-包含“ configuration.properties”文件的资源包。该文件包含变量,例如app.url,浏览器,chrome.driver.location。
-然后由配置包中的类读取“ configuration.properties”文件。类如下:connection.connect(function(err) {
if (err) throw err;
});
,AppProperties.java
,ConfigurationProperties.java
,PropertiesLoader.java
和LocalWebDriverPorperties.java
类
Configuration.properties
TestRunProperties .java
AppProperties.java
app.url=url for app under test
is.remote.run=false
grid.url="";
browser=CHROME
chrome.driver.location=C:/Selenium/chromedriver.exe
firefox.driver.location=C:/Selenium/geckodriver.exe
ConfigurationProperties.java
package configuration;
public class AppProperties {
public static String getAllUrl() {
return ConfigurationProperties.getProperties().getProperty("app.url");
}
}
PropertiesLoader.java
package configuration;
import java.util.Properties;
public class ConfigurationProperties {
private static Properties properties;
private ConfigurationProperties() {
}
public static void setProperties(Properties properties) {
ConfigurationProperties.properties = properties;
}
public static Properties getProperties() {
if (properties == null) {
throw new IllegalStateException("Please set properties using setProperties() before calling getProperties()");
}
return properties;
}
}
LocalWebDriverProperties.java
package configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLoader {
private Logger logger = LogManager.getLogger(PropertiesLoader.class);
public Properties getPropertiesFromFile(String propertiesFileName) {
InputStream inputStream = null;
Properties properties = new Properties();
try {
logger.info("Trying to load properties with file name: " + propertiesFileName);
inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFileName);
if (inputStream != null) {
properties.load(inputStream);
logger.info("Successfully loaded properties for file: " + propertiesFileName);
} else {
throw new FileNotFoundException("Property file '" + propertiesFileName + "' not found in the classpath");
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Cannot load properties due to IOException!");
} finally {
closeResource(inputStream);
}
return properties;
}
private void closeResource(InputStream inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
TestRunProperties.java
package configuration;
public class LocalWebDriverProperties {
return BrowserType.valueOf(ConfigurationProperties.getProperties().getProperty("browser"));
// }
public static String getChromeWebDriverLocation() {
return ConfigurationProperties.getProperties().getProperty("chrome.driver.location");
}
public static String getFirefoxWebDriverLocation() {
return ConfigurationProperties.getProperties().getProperty("firefox.driver.location");
}
public static String getInternetExplorerWebDriverLocation() {
return ConfigurationProperties.getProperties().getProperty("ie.driver.location");
}
}
如果我在框架的configuration.properties文件中指定Team City服务器使用的服务器属性(例如,app.url,浏览器),则上述配置可以正常工作。 但是我正在寻找的是使用另一种方法,而不是在configuration.properties文件中设置属性-使用TeamCity的环境变量。
可能在类似package configuration;
import driver.manager.BrowserType;
public class TestRunProperties {
public static String getGridUrl() {
return ConfigurationProperties.getProperties().getProperty("grid.url");
}
public static BrowserType getBrowserToRun() {
return BrowserType.valueOf(ConfigurationProperties.getProperties().getProperty("browser"));
}
public static boolean getIsRemoteRun(){
return Boolean.parseBoolean(ConfigurationProperties.getProperties().getProperty("is.remote.run"));
}
}
的一个类中具有此功能,而不是像我目前正在使用的5个类。
有什么办法吗?