猿问

如何在 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

我只想放一个像这样的maven命令


mvn clean test broswer=chrome env=qa

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


有可能这样做吗?这种情况的任何例子?


凤凰求蛊
浏览 116回答 2
2回答

有只小跳蛙

使用属性是个好主意。你走在正确的轨道上。为了加载不同的属性,可以根据环境,为属性文件创建多个文件夹。假设您将属性文件存储在src/main/java/dev/properties.properties创建多个目录,例如:src/main/java/qasrc/main/java/devsrc/main/java/prod在每个路径中创建 1 个属性文件,就像我在开始时所做的那样。现在,您想使用 Maven 加载正确的属性。你可以用maven profiles它来做到这一点。给你pom.xml添加:</dependencies>&nbsp; &nbsp; <profiles>&nbsp; &nbsp; &nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activeByDefault>true</activeByDefault>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>dev</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration.path>src/main/dev</configuration.path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; &nbsp; &nbsp; </profile>&nbsp; &nbsp; &nbsp; &nbsp; <profile>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activeByDefault>false</activeByDefault>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>prod</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration.path>src/main/prod</configuration.path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; &nbsp; &nbsp; </profile>&nbsp; &nbsp; </profiles></project>就在下面</dependencies>如您所见,我创建了 2 个配置文件。他们的 ID 是dev和prod。两个配置文件都有一个configuration.path指向您的.properties文件的属性。要使用 maven commend 运行配置文件,您只需键入-PprofileId. -Pdev例如我希望你使用,maven-surefire-plugin因为这就是我将在示例中展示的内容。<plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-surefire-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.22.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <systemPropertyVariables>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration.path>${configuration.path}</configuration.path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </systemPropertyVariables>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>以上只是部分配置surefire-plugin!让我们关注systemPropertyVariables. 为了从 Maven 配置文件中获取属性,您可以通过将${configuration.paths}变量传递到surefire-plugin. 您可以使用相同的名称。现在,您需要将.properties文件中的属性加载到系统中。我写了一个类来做到这一点,阅读configuration.path。您可以使用其他解决方案。public class Properties {&nbsp; &nbsp; private static java.util.Properties props;&nbsp; &nbsp; static {&nbsp; &nbsp; &nbsp; &nbsp; props = new java.util.Properties();&nbsp; &nbsp; &nbsp; &nbsp; String pathWithPropertiesFiles = System.getProperty("configuration.path");&nbsp; &nbsp; &nbsp; &nbsp; String[] paths = pathWithPropertiesFiles.split("[;]");&nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(paths).forEach(propertyPath -> Arrays.asList(Objects.requireNonNull(new File(propertyPath).listFiles())).forEach(propertyFile -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input = new FileInputStream(propertyFile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; props.load(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; }&nbsp; &nbsp; public static String getValue(String key) {&nbsp; &nbsp; &nbsp; &nbsp; String envProperty = System.getenv(key);&nbsp; &nbsp; &nbsp; &nbsp; if (envProperty != null && !envProperty.equals("null")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return envProperty;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String systemProperty = System.getProperty(key);&nbsp; &nbsp; &nbsp; &nbsp; if (systemProperty != null && !systemProperty.equals("null")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return systemProperty;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return props.getProperty(key);&nbsp; &nbsp; }}上述解决方案允许您将多个路径传递到configuration.path属性中,以 . 分隔;。如果您想打开正确的 URL,您只需使用Properties.getValue("baseURL");它会根据您选择的配置文件从正确的路径获取 URL。现在,除了浏览器之外,您可以做类似的事情。我强烈建议阅读BrowserFactory或Factory设计模式。我只能为您提供有关如何执行此操作的提示,因为我的解决方案可能无法如您所愿。创建其他配置文件(您可以将多个配置文件与 Maven 一起使用)但用于浏览器。<profile>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <activeByDefault>true</activeByDefault>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </activation>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>chrome</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <browser.type>chrome</browser.type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </properties>&nbsp; &nbsp; &nbsp; &nbsp; </profile>调用:-Pchrome记得将它添加到surefire-plugin:<configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <systemPropertyVariables>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration.path>${configuration.path}</configuration.path>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <browser.type>${browser.type}</browser.type>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </systemPropertyVariables>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>您现在要做的就是考虑,您在哪里实例化您的浏览器。简单的解决方案是(非线程安全!!!):public class Browser {&nbsp; &nbsp; public static WebDriver getDriver() {&nbsp; &nbsp; &nbsp; &nbsp; String browserType = Properties.getValue("browser.type"); //it will get the `chrome` for profile `chrome`&nbsp; &nbsp; &nbsp; &nbsp; switch(browserType) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "chrome": return new ChromeDriver();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}您现在要做的就是在您的框架中实施解决方案。switch添加配置文件,使用所有使用的浏览器填写声明。希望能帮助到你!

慕容3067478

对不起,如果代码看起来很垃圾,我倾向于使用 Scala这是一个使用我设法开始工作的属性的简单示例:省略了部分代码private String getBaseUrl() {&nbsp; &nbsp; &nbsp; &nbsp; String base = "";&nbsp; &nbsp; &nbsp; &nbsp; String foo = System.getProperty("browser", "chrome");&nbsp; &nbsp; &nbsp; &nbsp; switch (foo) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "firefox":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base = "https://www.google.co.uk";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "chrome":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base = "https://www.bbc.co.uk";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return base;&nbsp; &nbsp; }public void goTo() {&nbsp; &nbsp; &nbsp; &nbsp; this.driver.get(getBaseUrl());&nbsp; &nbsp; }所以当我使用命令时:mvn -Dbrowser=chrome test司机将导航至https://www.bbc.co.uk如果我使用:mvn -Dbrowser=firefox test然后驱动程序将导航到https://www.google.co.uk如果我只是输入:mvn test然后它将导航到 bbc 因为默认设置为 Chrome如果你有多个东西,比如不同的网络驱动程序等,那么它可以用同样的方式完成。读入一个属性,然后根据该属性的值,初始化您需要的任何驱动程序。
随时随地看视频慕课网APP

相关分类

Java
我要回答