【问题标题】:Selenium: Run Java code with different URL for three environmentsSelenium:为三种环境运行具有不同 URL 的 Java 代码
【发布时间】:2019-11-21 18:21:41
【问题描述】:

我有一个 Java 自动化测试框架。我需要在 SIT、UAT 和 Prod 等多个环境中运行此代码,但所有这些环境都有不同的 URL。

sit-config.properties

主页 = 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>

问题(编辑):

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

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

请帮忙。谢谢。

【问题讨论】:

  • 您想要的是每个环境的一个属性文件(“dev.properties”、“sit.properties”等)。每个都有相同的属性集,例如 homepage.url=XXX 。不确定它是否重复,但请参阅stackoverflow.com/questions/22757318/…
  • 不要有单独的分支,而是您的测试代码应该与被测代码一起标记和发布(最好在同一个 SCM 系统中)。这是因为今天针对 Dev 运行的测试在针对 Production 运行时很可能会失败,因为 Production 处于 Dev 前一段时间的位置。因此在运行测试时,从 SCM 获取适合环境的测试套件版本,并在运行时指定环境(即要使用的属性文件)。
  • 如何指定在运行时使用哪个属性文件?
  • 由您决定 - 但通常在命令行上(如“-Denv=prod”)或通过在每台机器上设置的环境变量。然后使用它来构造要加载的属性文件的文件名,就像我链接到的问题一样。
  • 由于您使用的是 maven,另请参阅 stackoverflow.com/questions/1149352/…maven.apache.org/guides/mini/…

标签: java selenium environment


【解决方案1】:

首先,您必须使用不同的 URL 创建属性文件

然后添加配置文件阅读器

public String applicationUrl_QA() {
        String applicationUrl = properties.getProperty("applicationUrl_QA");
        if(applicationUrl != null) return applicationUrl;
        else throw new RuntimeException("url not specified in the Configuration.properties file.");
    }

然后你可以像下面这样配置所有环境

if(Env.equalsIgnoreCase("QA")){
            if(URL.equalsIgnoreCase("_QA")){
                driver.get(configFileReader.applicationUrl_QA());
}

那么你已经创建了单独的 XML,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" thread-count="5" verbose="1">

    <!--Values Chrome Firefox HLChrome HLFirefox HTMLUnit phantomJS-->
    <parameter name="browser" value="Firefox"/>

    <!--Values AdminURL StatementURL PatientURL-->
    <parameter name="URL" value="Value"/>

    <!--Values QA Dev Uat Prod -->
    <parameter name="Env" value="QA"/>
    <test name="AdminTests">
        <classes>
            <class name="tests.Test1"/>
        </classes>
         <classes>
            <class name="tests.test2"/>
        </classes>
    </test>
</suite>

最后你必须使用一个xml文件调用所有的xmls

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" thread-count="5" verbose="2">
<suite-files>
    <suite-file path="File1_QA.xml"/>
</suite-files>
    <suite-files>
        <suite-file path="File2_UAT.xml"/>
    </suite-files>
</suite>

【讨论】:

  • 正如我所说,我不想使用属性文件和 testng xml 文件。我想使用 Java 所有者库 + Maven 配置文件。
【解决方案2】:

我不得不解决一个类似的问题。这就是我的方法。

第 1 步:POM.xmlbuild &gt; plugin 部分下添加 surefire 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>

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

注意:如果您想运行特定的 testng xml,请取消注释 &lt;suiteXmlFiles&gt; 标记并提供您的 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)));

第 3 步:TestEnvironment 传递给mvn 命令

mvn clean test -DTestEnvironment=sit

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

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

【讨论】:

  • 我不想使用属性代码。我想使用 Java Owner 库 + Maven 配置文件。
  • @nicholas 您可以使用上述想法来决定在使用this approach 时从何处加载属性(甚至this approach,其中${myPath} 将来自Maven 配置文件)。您只需在每个 profile &gt; build &gt; plugins 部分下添加插件配置。您可以将基本配置保留在profiles 部分之外,只需将systemPropertyVariables 下推到profiles,Maven 应该能够合并它们
【解决方案3】:

根据您提供的example,下面有一条说明,从 Maven 执行测试需要做什么:

1) 在 src/test/resources 中为每个环境维护一个单独的属性文件

2) 在您的 Maven 项目中添加“所有者”依赖项。

3) 创建接口“环境”,如示例所示:

    @Sources({
    "classpath:${env}.properties"
    })

4) 在您的测试和页面对象中,您将使用 testEnvironment 对象:

public class EnvironmentTest {

    Environment testEnvironment;

    @Test
    public void functionalTest() {
        System.out.println(testEnvironment.url());
        System.out.println(testEnvironment.getDBHostname());
        System.out.println(testEnvironment.getDBPassword());
    }

    @BeforeTest
    public void beforeTest() {
        String environemnt = System.getProperty("environment"); //here you read your environment name
        ConfigFactory.setProperty("env", environemnt); //here you use your environment name for reading proper file
        testEnvironment = ConfigFactory.create(Environment.class); //here we create an instance of the Environment interface & access the property file
    }
}

5) 使用 Maven 运行您的程序,您可以在其中输入测试的环境名称:

clean test -Dtest=EnvironmentTest -Denvironment=qa

【讨论】:

    【解决方案4】:

    我建议您使用 Spring Boot 来使用不同的配置文件(配置文件)。但是,如果您只需要在执行过程中传递 URL 参数,请尝试:

    在您的代码中:

    public class Url {
    
        public static String host;
    
        static {
            Url.host = System.getProperty("mysystem.property.host");
        }
    }
    

    然后,当您执行测试时,按如下方式传递属性:

    mvn clean test -Dmysystem.property.host=yourUrl

    【讨论】:

    • 我为自动化测试项目而不是 Web 项目创建。
    • web项目和测试项目没有任何区别,我建议你多了解一下,它们都是基础。
    猜你喜欢
    • 2019-11-29
    • 2022-09-28
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 2021-06-01
    • 2014-07-14
    相关资源
    最近更新 更多