Selenium:针对三种环境使用不同的URL运行Java代码

nic*_*las 5 java environment selenium

我有一个Java自动测试框架。我需要在多个环境(例如SIT,UAT和Prod)上运行此代码,但是所有这些环境都具有不同的URL。

sit-config.properties

hompepage = XXX

uat-config.properties

主页= YYY

Maven个人资料

<profiles>
    <profile>
        <id>sit</id>
        <activation>
            <property>
                <name>environment</name>
                <value>sit</value>
            </property>
        </activation>
    </profile>
    <!-- mvn -Denvironment=sit clean test -->

    <profile>
        <id>uat</id>
        <activation>
            <property>
                <name>environment</name>
                <value>uat</value>
            </property>
        </activation>
    </profile>

  </profiles>
Run Code Online (Sandbox Code Playgroud)

问题(编辑):

  1. 如何根据环境测试加载特定的属性文件?
  2. 我有一个Java所有者库的示例,但有一个testng而不是Maven。

http://www.testautomationguru.com/selenium-webdriver-how-to-execute-tests-in-multiple-environments/

请帮忙。谢谢。

App*_*tri 3

我必须解决类似的问题。这就是我的方法。

第 1 步:添加surefire plugin到您的POM.xml下一节build > plugin

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <systemPropertyVariables>
                    <TestEnvironment>local</TestEnvironment>
                </systemPropertyVariables>
                <!-- <suiteXmlFiles>
                    <suiteXmlFile>here goes your testng xml</suiteXmlFile>
                </suiteXmlFiles> -->
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

这里TestEnvironment是您正在设置的自定义系统属性,稍后可以在代码中检索它。

注意:如果您想运行特定的 testng xml,请取消注释<suiteXmlFiles>标记并提供 testng xml 文件的路径。

步骤 2:添加代码以获取系统属性并从相应的属性文件中读取。

        // Assuming your properties files are in the root directory of your project.
        String configFileName = "./%s-config.properties";
        String EnvironmentName = System.getProperty("TestEnvironment");
        System.out.println("TestEnvironment: " + EnvironmentName);

        configFileName = String.format(configFileName, EnvironmentName);
        properties = new Properties();
        properties.load(new FileInputStream(new File(configFileName)));
Run Code Online (Sandbox Code Playgroud)

步骤 3:传递TestEnvironmentmvn命令

mvn clean test -DTestEnvironment=sit
Run Code Online (Sandbox Code Playgroud)

该命令将从您的文件中读取sit-config.properties并执行测试。要从不同的属性文件中读取,请在命令行中传递不同的值。

如果这回答了您的问题,请告诉我。