Selenium: Run Java code with different URL for three environments Selenium: Run Java code with different URL for three environments selenium selenium

Selenium: Run Java code with different URL for three environments


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>


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.


According to the example that you gave, there is an instruction below, what you have to do for executing the test from Maven:

1) Maintain a separate property file for each environment in src/test/resourcces

2) Add 'owner' dependency in your Maven project.

3) Create Interface 'Environment' as shown in the example with:

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

4) Throughout your test and page objects, you would be using the testEnvironment object:

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) Run your program using Maven where you enter environment name for your test:

clean test -Dtest=EnvironmentTest -Denvironment=qa