首页 > 解决方案 > 如何在 Maven 中运行具有不同配置的 UI 测试

问题描述

我是自动化 UI 测试的新手,我正在使用 Cucumber 和 Selenium 进行 UI 自动化。

所以在我的项目中,我创建了一个 Hook 类来设置用于测试的 Web 驱动程序。像这样的东西:

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

我只想放一个像

mvn clean test broswer=chrome env=qa

从正确的文件中选择属性并根据浏览器参数设置 Web 驱动程序。

有可能这样做吗?这种场景有什么例子吗?

标签: javaseleniumcucumberui-automation

解决方案


使用属性是个好主意。你在正确的轨道上。

为了加载不同的属性,根据环境,您可以为属性文件创建多个文件夹。

假设您将属性文件存储在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 是devprod。两个配置文件都有一个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到系统中。我写了一个类来做到这一点,阅读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。

现在,除了浏览器之外,您可以做类似的事情。我强烈建议阅读BrowserFactoryFactory设计模式。我只能为您提供有关如何执行此操作的提示,因为我的解决方案可能无法按您的意愿工作。

创建其他配置文件(您可以使用 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声明。

希望能帮助到你!


推荐阅读