Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Maven中使用不同的配置运行UI测试_Java_Selenium_Cucumber_Ui Automation - Fatal编程技术网

Java 如何在Maven中使用不同的配置运行UI测试

Java 如何在Maven中使用不同的配置运行UI测试,java,selenium,cucumber,ui-automation,Java,Selenium,Cucumber,Ui Automation,我是自动化UI测试的新手,我正在使用Cucumber和Selenium开发UI自动化 所以在我的项目中,我创建了一个钩子类来设置web驱动程序进行测试。大概是这样的: System.setProperty("webdriver.chrome.driver", "driver path"); driver = new chrome driver; 但是如果我想在不同的浏览器和环境下运行相同的测试。例如,我想用chrome和environment A运行测试,用firefox和environmen

我是自动化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

我想放一个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配置文件来实现这一点

在pom.xml中添加:

以上只是surefire插件的部分配置!让我们关注systemPropertyVariables。为了从maven概要文件中获取属性,您可以通过将${configuration.paths}变量传递到surefire插件中,将它们加载到系统中。您可以使用相同的名称

现在,您需要将.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.getValuebaseURL;它将根据您选择的配置文件从正确的路径获取URL

现在,除了浏览器,你可以做类似的事情。我强烈建议阅读BrowserFactory或Factory设计模式。我只能给你一些提示,告诉你怎么做,因为我的解决方案可能不会像你想的那样有效

创建其他配置文件您可以将多个配置文件与maven一起使用,但用于浏览器

<profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <id>chrome</id>
            <properties>
                <browser.type>chrome</browser.type>
            </properties>
        </profile>
你现在要做的就是想一想,在哪里实例化你的浏览器

简单的解决方案是非线程安全的!!!:

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

这是一个使用属性的简单示例,我已设法使其正常工作:

省略了一些代码

私有字符串getBaseUrl{ 字符串基=; 字符串foo=System.getPropertybrowser,chrome; 开关foo{ 案例firefox: 基数=https://www.google.co.uk; 打破 外壳铬: 基数=https://www.bbc.co.uk; 打破 } 返回基地; } 公开无效后藤{ this.driver.getBaseURL; } 因此,当我使用命令时:

mvn-Dbrowser=铬测试

驾驶员将导航到

如果我使用:

mvn-Dbrowser=firefox测试

然后,驾驶员将导航到

如果我刚刚进入:

mvn测试

然后它将导航到bbc,因为默认设置为Chrome

如果你有很多东西,比如不同的网络驱动程序等等,那么你可以用同样的方法来完成。读入一个属性,然后根据该属性的值初始化所需的驱动程序

希望有帮助

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);
    }
}
<profile>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <id>chrome</id>
            <properties>
                <browser.type>chrome</browser.type>
            </properties>
        </profile>
<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();
        }
    }
}