Site icon Vinsguru

Selenium WebDriver – How To Execute Tests In Multiple Environments

Overview:

I have been running thousands of automated regression test cases in multiple test environments for years. As part of CI / CD pipeline, I run the tests in QA, UAT, Staging and PROD. In this article, I would like to show you the approach I follow to make the tests run on any given test environment.

Many of us tend to write the scripts as shown below with a lot of variables and if-else blocks which is very difficult to maintain.

    if(environment.equals("prod")){       
        url="http://testautomationguru.com";
        username="tag";
        password="password";   
    }else if(environment.equals("qa")){
        url="http://qa.testautomationguru.com";
        username="qatag";
        password="qapassword";        
    }else if(environment.equals("dev")){
        url="http://dev.testautomationguru.com";
        username="devtag";
        password="devpassword";        
    }

    driver.get(url);
    driver.findElement(By.id("username")).sendKeys(username);
    driver.findElement(By.id("password")).sendKeys(password);

Now adding few environment details, say DB connections details to this script is not an easy task! It requires a lot of code change and testing to ensure that the script is not broken.

Keeping Environment Specific Data:

In order to make the script work for any given environment, We should avoid using any hard coded environment specific details in the script. As part of this article, Lets assume, We would need below details to run the script.

I would suggest you to keep the environment specific details completely away from the test data as It is not going to change for each test. So, Lets create a property file as shown here.

# application properties
url=http://testautomationguru.com
username=tag
password=password123

# databsse properties
db.hostname=db.testautomationguru.com
db.port=3306
db.username=dba_admin
db.password=secured!

Lets maintain a separate property file for each environment as shown here.

Accessing Environment Specific Data:

To access environment specific data, I will be using Java – Owner library.

<dependency>
    <groupId>org.aeonbits.owner</groupId>
    <artifactId>owner</artifactId>
    <version>1.0.8</version>
</dependency>
@Sources({
    "classpath:qa.properties" // mention the property file name
})
public interface Environment extends Config {

    String url();

    String username();

    String password();

    @Key("db.hostname")
    String getDBHostname();

    @Key("db.port")
    int getDBPort();

    @Key("db.username")
    String getDBUsername();

    @Key("db.password")
    String getDBPassword();

}
Environment testEnvironment = ConfigFactory.create(Environment.class);

 // prints http://qa.testautomationguru.com
 System.out.println(testEnvironment.url());
 
 // prints qa.db.testautomationguru.com
 System.out.println(testEnvironment.getDBHostname());
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() {
        testEnvironment = ConfigFactory.create(Environment.class);
    }

}
@Sources({
    "classpath:${env}.properties"
})
public interface Environment extends Config {

    String url();

    String username();

    String password();

    @Key("db.hostname")
    String getDBHostname();

    @Key("db.port")
    int getDBPort();

    @Key("db.username")
    String getDBUsername();

    @Key("db.password")
    String getDBPassword();

}
<suite name="TAG Suite">
    <parameter name="environment"  value="prod"/>
    <test name="Simple example">
    <-- ... -->
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
    @Parameters({"environment"})
    public void beforeTest(String environemnt) {
        ConfigFactory.setProperty("env", environemnt);
        testEnvironment = ConfigFactory.create(Environment.class);
    }

}

Summary:

Adding any new environment specific data is very easy with this approach. Your tests and page objects remain unchanged. We need to update only Environment interface.

You can run the test against any given environment Dev, QA, UAT, Pre-Production, PROD without any code change. Name of the environment can be passed to the test as a parameter as shown above.

 

Happy Testing & Subscribe 🙂

 

 

Share This:

Exit mobile version