我是自动化UI测试的新手,我正在使用Cucumber和Selenium进行UI自动化。
因此,在我的项目中,我创建了一个挂钩类来设置用于测试的Web驱动程序。这样的东西:
System.setProperty("webdriver.chrome.driver", "driver path");
driver = new chrome driver;
但是,如果我想在不同的浏览器和不同的环境中运行相同的测试。例如,我想使用Chrome和Environment A进行测试,并使用Firefox和Environment 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
从正确的文件中选择属性,并根据浏览器参数设置Web驱动程序。
有可能这样做吗?这种情况有什么例子吗?
使用属性是一个好主意。您在正确的轨道上。
为了基于环境加载不同的属性,您可以为属性文件创建多个文件夹。
让我们假设您将属性文件存储在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
。这两个配置文件均具有指向您.properties
文件的configuration.path
属性。要使用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
文件中的属性加载到系统中。我写了一堂课来阅读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
驱动程序将导航到https://www.bbc.co.uk
,如果我使用:
mvn -Dbrowser=firefox test
然后驱动程序将导航到https://www.google.co.uk
如果我只输入:
mvn test
然后,它将导航到BBC,因为默认设置为Chrome
如果您有多个诸如不同的Web驱动程序等之类的东西,则可以以相同的方式完成。在属性中阅读然后根据属性的价值,初始化所需的哪个驱动程序。
希望有帮助