Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Run Java code with different URL for three environments

I have an automation testing framework in Java. I need this code run on multiple environment such as SIT, UAT and Prod but all of these environment have different URL.

sit-config.properties

hompepage = XXX

uat-config.properties

homepage = YYY

Maven Profile

<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>

Questions (EDIT):

  1. How to load specific properties file based on environment test?
  2. I got an example for Java Owner library but for testng not Maven.

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

Please help. Thanks.

like image 393
nicholas Avatar asked Jul 12 '19 01:07

nicholas


2 Answers

First you have to create properties file with your different URLs

Then Add config File reader

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.");
    }

Then you can config all the environments like below

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

then you have create seperate XMLs like below

<?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>

Finally you have to call all the xmls using one xml file

<?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>
like image 102
Devdun Avatar answered Oct 03 '22 15:10

Devdun


I had to solve a similar problem. And here is how I approached.

Step-1: Add surefire plugin in your POM.xml under build > plugin section.

        <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>

Here, TestEnvironment is the custom system property you are setting up, this can be retrieved later in your code.

Note: If you want to run a specific testng xml, un-comment <suiteXmlFiles> tag and provide the path of your testng xml file.

Step-2: Add code to get the system property and read from the respective properties file.

        // 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)));

Step-3: Pass TestEnvironment to mvn command

mvn clean test -DTestEnvironment=sit

This command will read from your sit-config.properties file and execute the tests. To read from different properties files pass a different value in the command line.

Please let me know if this answered your questions.

like image 44
Appu Mistri Avatar answered Oct 03 '22 17:10

Appu Mistri