如何 运行 UI 在 Maven 中使用不同的配置进行测试
How to run UI tests with different configuration in Maven
我是自动化 UI 测试的新手,我正在使用 Cucumber 和 Selenium 进行 UI 自动化测试。
所以在我的项目中,我创建了一个 Hook class 来设置用于测试的网络驱动程序。像这样:
System.setProperty("webdriver.chrome.driver", "driver path");
driver = new chrome driver;
但是如果我想 运行 使用不同的浏览器和不同的环境进行相同的测试。例如,我想运行测试chrome和环境A,运行同样测试firefox和环境B。我计划为不同的环境创建两个属性文件
env=qa
baseURl = http://qa......
username = test
password = test
和
env=dev
baseURl = http://dev......
username = test1
password = test1
我只想输入一个像
这样的 maven 命令
mvn clean test broswer=chrome env=qa
从正确的文件中选择属性并根据浏览器参数设置网络驱动程序。
可以吗?这种情况的任何例子?
使用属性是个好主意。你走在正确的轨道上。
为了加载不同的属性,根据环境,可以为属性文件创建多个文件夹。
假设您将属性文件存储在 src/main/java/dev/properties.properties
下
创建多个目录,如:
src/main/java/qa
src/main/java/dev
src/main/java/prod
在每个路径中创建 1 个属性文件,就像我一开始所做的那样。
现在,您想使用 Maven 加载正确的属性。您可以使用 maven profiles
来做到这一点。
在您的 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>
下面
如您所见,我创建了 2 个配置文件。他们的 ID 是 dev
和 prod
。两个配置文件都有一个 configuration.path
属性 指向您的 .properties
文件。
要 运行 带有 maven 推荐的配置文件,您只需键入 -PprofileId
。 -Pdev
例如
我希望你使用 maven-surefire-plugin
因为这就是我将在示例中显示的内容。
<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
的部分配置!让我们关注 systemPropertyVariables
。为了从 Maven 配置文件中获取属性,您可以通过将 ${configuration.paths}
变量传递到 surefire-plugin
来将它们加载到系统中。您可以使用相同的名称。
现在,您需要将 .properties
文件中的属性加载到系统中。我写了一个 class 来做到这一点,阅读 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);
}
}
上述解决方案允许您将多个路径传递到 configuration.path
和 属性,并用 ;
.
分隔
如果您想打开正确的 URL,您只需使用 Properties.getValue("baseURL");
它会根据您选择的配置文件从正确的路径获取 URL。
现在,除了浏览器之外,您可以做类似的事情。我强烈建议阅读 BrowserFactory
或 Factory
设计模式。我只能为您提供有关如何执行此操作的提示,因为我的解决方案可能不会如您所愿。
创建额外的配置文件(您可以使用多个配置文件与 maven),但用于浏览器。
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>chrome</id>
<properties>
<browser.type>chrome</browser.type>
</properties>
</profile>
调用:-Pchrome
记得加到surefire-plugin
:
<configuration>
<systemPropertyVariables>
<configuration.path>${configuration.path}</configuration.path>
<browser.type>${browser.type}</browser.type>
</systemPropertyVariables>
</configuration>
你现在要做的就是想想,你在哪里实例化你的浏览器。
简单的解决方案是(非线程安全!!!):
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();
}
}
}
您现在要做的就是在您的框架中实施解决方案。添加配置文件,使用所有使用的浏览器填写 switch
语句。
希望对您有所帮助!
对不起,如果代码看起来很垃圾,我倾向于使用 Scala
这是一个使用属性的简单示例,我已经设法开始工作:
省略部分代码
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());
}
所以当我使用命令时:
mvn -Dbrowser=chrome test
driver 将导航到 https://www.bbc.co.uk
如果我使用:
mvn -Dbrowser=firefox test
然后 driver 将导航到 https://www.google.co.uk
如果我直接输入:
mvn test
然后它将导航到 bbc 因为默认设置为 Chrome
如果你有多个东西,比如不同的 webdrivers 等等,那么它可以用同样的方式完成。读入属性,然后根据属性的值,初始化你需要的driver。
希望对您有所帮助
我是自动化 UI 测试的新手,我正在使用 Cucumber 和 Selenium 进行 UI 自动化测试。
所以在我的项目中,我创建了一个 Hook class 来设置用于测试的网络驱动程序。像这样:
System.setProperty("webdriver.chrome.driver", "driver path");
driver = new chrome driver;
但是如果我想 运行 使用不同的浏览器和不同的环境进行相同的测试。例如,我想运行测试chrome和环境A,运行同样测试firefox和环境B。我计划为不同的环境创建两个属性文件
env=qa
baseURl = http://qa......
username = test
password = test
和
env=dev
baseURl = http://dev......
username = test1
password = test1
我只想输入一个像
这样的 maven 命令mvn clean test broswer=chrome env=qa
从正确的文件中选择属性并根据浏览器参数设置网络驱动程序。
可以吗?这种情况的任何例子?
使用属性是个好主意。你走在正确的轨道上。
为了加载不同的属性,根据环境,可以为属性文件创建多个文件夹。
假设您将属性文件存储在 src/main/java/dev/properties.properties
创建多个目录,如:
src/main/java/qa
src/main/java/dev
src/main/java/prod
在每个路径中创建 1 个属性文件,就像我一开始所做的那样。
现在,您想使用 Maven 加载正确的属性。您可以使用 maven profiles
来做到这一点。
在您的 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>
如您所见,我创建了 2 个配置文件。他们的 ID 是 dev
和 prod
。两个配置文件都有一个 configuration.path
属性 指向您的 .properties
文件。
要 运行 带有 maven 推荐的配置文件,您只需键入 -PprofileId
。 -Pdev
例如
我希望你使用 maven-surefire-plugin
因为这就是我将在示例中显示的内容。
<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
的部分配置!让我们关注 systemPropertyVariables
。为了从 Maven 配置文件中获取属性,您可以通过将 ${configuration.paths}
变量传递到 surefire-plugin
来将它们加载到系统中。您可以使用相同的名称。
现在,您需要将 .properties
文件中的属性加载到系统中。我写了一个 class 来做到这一点,阅读 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);
}
}
上述解决方案允许您将多个路径传递到 configuration.path
和 属性,并用 ;
.
如果您想打开正确的 URL,您只需使用 Properties.getValue("baseURL");
它会根据您选择的配置文件从正确的路径获取 URL。
现在,除了浏览器之外,您可以做类似的事情。我强烈建议阅读 BrowserFactory
或 Factory
设计模式。我只能为您提供有关如何执行此操作的提示,因为我的解决方案可能不会如您所愿。
创建额外的配置文件(您可以使用多个配置文件与 maven),但用于浏览器。
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<id>chrome</id>
<properties>
<browser.type>chrome</browser.type>
</properties>
</profile>
调用:-Pchrome
记得加到surefire-plugin
:
<configuration>
<systemPropertyVariables>
<configuration.path>${configuration.path}</configuration.path>
<browser.type>${browser.type}</browser.type>
</systemPropertyVariables>
</configuration>
你现在要做的就是想想,你在哪里实例化你的浏览器。
简单的解决方案是(非线程安全!!!):
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();
}
}
}
您现在要做的就是在您的框架中实施解决方案。添加配置文件,使用所有使用的浏览器填写 switch
语句。
希望对您有所帮助!
对不起,如果代码看起来很垃圾,我倾向于使用 Scala
这是一个使用属性的简单示例,我已经设法开始工作:
省略部分代码
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());
}
所以当我使用命令时:
mvn -Dbrowser=chrome test
driver 将导航到 https://www.bbc.co.uk
如果我使用:
mvn -Dbrowser=firefox test
然后 driver 将导航到 https://www.google.co.uk
如果我直接输入:
mvn test
然后它将导航到 bbc 因为默认设置为 Chrome
如果你有多个东西,比如不同的 webdrivers 等等,那么它可以用同样的方式完成。读入属性,然后根据属性的值,初始化你需要的driver。
希望对您有所帮助