2 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
使用屬性是個(gè)好主意。你走在正確的軌道上。
為了加載不同的屬性,可以根據(jù)環(huán)境,為屬性文件創(chuàng)建多個(gè)文件夾。
假設(shè)您將屬性文件存儲(chǔ)在src/main/java/dev/properties.properties
創(chuàng)建多個(gè)目錄,例如:
src/main/java/qa
src/main/java/dev
src/main/java/prod
在每個(gè)路徑中創(chuàng)建 1 個(gè)屬性文件,就像我在開始時(shí)所做的那樣。
現(xiàn)在,您想使用 Maven 加載正確的屬性。你可以用maven profiles它來做到這一點(diǎn)。
給你pom.xml添加:
</dependencies>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>dev</id>
<properties>
<configuration.path>src/main/dev</configuration.path>
</properties>
</profile>
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>prod</id>
<properties>
<configuration.path>src/main/prod</configuration.path>
</properties>
</profile>
</profiles>
</project>
就在下面</dependencies>
如您所見,我創(chuàng)建了 2 個(gè)配置文件。他們的 ID 是dev和prod。兩個(gè)配置文件都有一個(gè)configuration.path指向您的.properties文件的屬性。要使用 maven commend 運(yùn)行配置文件,您只需鍵入-PprofileId. -Pdev例如
我希望你使用,maven-surefire-plugin因?yàn)檫@就是我將在示例中展示的內(nèi)容。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<systemPropertyVariables>
<configuration.path>${configuration.path}</configuration.path>
</systemPropertyVariables>
</configuration>
</plugin>
以上只是部分配置surefire-plugin!讓我們關(guān)注systemPropertyVariables. 為了從 Maven 配置文件中獲取屬性,您可以通過將${configuration.paths}變量傳遞到surefire-plugin. 您可以使用相同的名稱。
現(xiàn)在,您需要將.properties文件中的屬性加載到系統(tǒng)中。我寫了一個(gè)類來做到這一點(diǎn),閱讀configuration.path。您可以使用其他解決方案。
public class Properties {
private static java.util.Properties props;
static {
props = new java.util.Properties();
String pathWithPropertiesFiles = System.getProperty("configuration.path");
String[] paths = pathWithPropertiesFiles.split("[;]");
Arrays.asList(paths).forEach(propertyPath -> Arrays.asList(Objects.requireNonNull(new File(propertyPath).listFiles())).forEach(propertyFile -> {
InputStream input;
try {
input = new FileInputStream(propertyFile);
props.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
}));
}
public static String getValue(String key) {
String envProperty = System.getenv(key);
if (envProperty != null && !envProperty.equals("null")) {
return envProperty;
}
String systemProperty = System.getProperty(key);
if (systemProperty != null && !systemProperty.equals("null")) {
return systemProperty;
}
return props.getProperty(key);
}
}
上述解決方案允許您將多個(gè)路徑傳遞到configuration.path屬性中,以 . 分隔;。
如果您想打開正確的 URL,您只需使用Properties.getValue("baseURL");它會(huì)根據(jù)您選擇的配置文件從正確的路徑獲取 URL。
現(xiàn)在,除了瀏覽器之外,您可以做類似的事情。我強(qiáng)烈建議閱讀BrowserFactory或Factory設(shè)計(jì)模式。我只能為您提供有關(guān)如何執(zhí)行此操作的提示,因?yàn)槲业慕鉀Q方案可能無法如您所愿。
創(chuàng)建其他配置文件(您可以將多個(gè)配置文件與 Maven 一起使用)但用于瀏覽器。
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>chrome</id>
<properties>
<browser.type>chrome</browser.type>
</properties>
</profile>
調(diào)用:-Pchrome
記得將它添加到surefire-plugin:
<configuration>
<systemPropertyVariables>
<configuration.path>${configuration.path}</configuration.path>
<browser.type>${browser.type}</browser.type>
</systemPropertyVariables>
</configuration>
您現(xiàn)在要做的就是考慮,您在哪里實(shí)例化您的瀏覽器。
簡(jiǎn)單的解決方案是(非線程安全!?。。?/p>
public class Browser {
public static WebDriver getDriver() {
String browserType = Properties.getValue("browser.type"); //it will get the `chrome` for profile `chrome`
switch(browserType) {
case "chrome": return new ChromeDriver();
}
}
}
您現(xiàn)在要做的就是在您的框架中實(shí)施解決方案。switch添加配置文件,使用所有使用的瀏覽器填寫聲明。
希望能幫助到你!

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊
對(duì)不起,如果代碼看起來很垃圾,我傾向于使用 Scala
這是一個(gè)使用我設(shè)法開始工作的屬性的簡(jiǎn)單示例:
省略了部分代碼
private String getBaseUrl() {
String base = "";
String foo = System.getProperty("browser", "chrome");
switch (foo) {
case "firefox":
base = "https://www.google.co.uk";
break;
case "chrome":
base = "https://www.bbc.co.uk";
break;
}
return base;
}
public void goTo() {
this.driver.get(getBaseUrl());
}
所以當(dāng)我使用命令時(shí):
mvn -Dbrowser=chrome test
司機(jī)將導(dǎo)航至https://www.bbc.co.uk
如果我使用:
mvn -Dbrowser=firefox test
然后驅(qū)動(dòng)程序?qū)?dǎo)航到https://www.google.co.uk
如果我只是輸入:
mvn test
然后它將導(dǎo)航到 bbc 因?yàn)槟J(rèn)設(shè)置為 Chrome
如果你有多個(gè)東西,比如不同的網(wǎng)絡(luò)驅(qū)動(dòng)程序等,那么它可以用同樣的方式完成。讀入一個(gè)屬性,然后根據(jù)該屬性的值,初始化您需要的任何驅(qū)動(dòng)程序。
添加回答
舉報(bào)