QA Automation: QTP tutorial – using the same script for multiple environments

Hi. Today, I will show you an awesome tutorial for using the same script to test multiple environments. Let's say you're working on an application that requires automation tests to run on two environments. The first environment is the test environment before deployment to production. And the second environment is the actual production environment after deployment. Let's also say that the functionality for both environments is exactly the same, but the login username and password differs based on the environment. In this scenario, you don't need to create two separate scripts to accommodate the differences in login credentials. You can create one script and just change the login credentials based on the environment.

I will be using our premade framework to write the main script. This is a framework I haven't shared on our website so you will have to write your own code from scratch. I just want you to use this tutorial as an example. Let's get started!

1. Open UFT and create a new test. We will create the login functionality for our qtptutorial.net website. Some automation testers may create two separate scripts for these two environments which may look something like this:

'Test Environment

Open_URL "https://www.qtptutorial.net/wp-login.php?loggedout=true" Set_TextField_ByHtmlid "user_login", 0, "TestEnvironment" Set_TextField_ByHtmlid "user_pass", 0, "123456"

Click_Button "Log In", 0

'Prod Environment

Open_URL "https://www.qtptutorial.net/wp-login.php?loggedout=true" Set_TextField_ByHtmlid "user_login", 0, "ProdEnvironemnt" Set_TextField_ByHtmlid "user_pass", 0, "678900"

Click_Button "Log In", 0

Notice how these two scripts are exactly the same except for the login credentials depeneding on the type of enevironment. We will consolidate this into one script which will save us a lot of time now and in future maintenance.

2. I will create two global variables. One for the username and one for the password. Click file -->New-->Function Library and name the Library TestEnvironment. Save it to the C Directory for now. I just want to show you how this works and you can organize it later. Since this is our test environment, our global variables will be:

strUserName = "TestEnvironment"

strPassword = "123456"

3. Now create another function library for the production environment and call it ProdEnvironment. Also save it to the C drive. It should look like this:

strUserName = "TestEnvironment"

strPassword = "123456"

Notice how the variable names are the same between the two environments, the only difference is the value of each variable.

4. Now go back to the two scripts we created in the beginning. Let's consolidate them and have them use our newly created environments. Now it should look like this: Open_URL "https://www.qtptutorial.net/wp-login.php?loggedout=true" Set_TextField_ByHtmlid "user_login", 0, strUsername

Set_TextField_ByHtmlid "user_pass", 0, strPassword

Click_Button "Log In", 0

I only have one script which includes the two variables we created instead of the user name and password.

5. Right click on the test environment function library tab, and associate it with the test. Run your script and you will see it log into the test environment. Cool right? Now right click on this function library and disassociate it from this test.


 

 

 

 

 

6. Now add the ProdEnvironment function library to this test. Run the same test and you should see it login to the prod environment. Awesome right?

You can see that we're using the same script, but we're testing two different environments!

 

Practice by creating your own script on our site. Post your questions and comments below!

Pin It on Pinterest

Clef two-factor authentication