How To Write Vbscript For Login Page In Qtp

Writing VBScript for a login page in QTP can seem intimidating at first, but with a little guidance and practice, you’ll be able to create a secure and efficient login system for your application. In this article, I’ll walk you through the steps of creating a login page using VBScript in QTP, sharing my personal insights and tips along the way.

Understanding the Login Page

Before we dive into writing the VBScript code, it’s important to understand the purpose and components of a login page. The login page is the first point of entry for users to access a secure system or application. It typically consists of two input fields – one for the username and another for the password – along with a login button.

In our example, we’ll create a login page that verifies the entered username and password against a predefined set of credentials. Upon successful authentication, the user will be granted access to the application, otherwise, an error message will be displayed.

Setting Up the QTP Environment

Before we start writing the VBScript code, we need to set up the QTP environment. Make sure you have QTP installed on your machine and launch the QTP IDE. Create a new test and save it with a suitable name, such as “LoginTest”.

Next, we need to add the required objects to our test. In the Object Repository, add the objects for the username input field, password input field, and login button by using the “Add Objects” feature. This step is crucial for QTP to recognize and interact with these elements during the test.

Writing the VBScript Code

Now that we have our test environment set up, let’s start writing the VBScript code for our login page. Begin by opening the “Actions” tab in the QTP IDE and create a new action. We’ll name this action “LoginAction”.

In the “LoginAction” action, we’ll write the code to handle the login functionality. Here’s an example of how the code might look:


' Start of LoginAction action
Username = "admin"
Password = "password"

Browser("Login Page").Page("Login Page").WebEdit("username").Set Username
Browser("Login Page").Page("Login Page").WebEdit("password").Set Password
Browser("Login Page").Page("Login Page").WebButton("login").Click

If Browser("Home Page").Exist Then
MsgBox "Login successful!"
Else
MsgBox "Invalid username or password!"
ExitTest
End If
' End of LoginAction action

In the above code, we first assign the username and password values to variables. Then, we use the Set method to enter these values into the respective input fields on the login page. After that, we click the login button using the Click method.

We then check if the home page exists as a verification step. If it does, we display a message box indicating successful login. If not, we display an error message and exit the test execution using the ExitTest statement.

Running the Test

Now that we have written the VBScript code for our login page, it’s time to run the test and see it in action. Click on the green play button in the QTP IDE to run the test.

If everything is set up correctly and the login credentials are valid, you should see the “Login successful!” message box. If the credentials are invalid, you’ll see the “Invalid username or password!” message box.

Conclusion

Writing VBScript for a login page in QTP may seem complex at first, but by breaking it down into manageable steps, it becomes much more achievable. Remember to set up your QTP environment, add the necessary objects to the Object Repository, and write the code to handle the login functionality.

With practice and experimentation, you can further enhance the login page by adding features like password encryption, account lockouts, and password reset options. The possibilities are endless!

So go ahead, give it a try, and unlock the power of VBScript and QTP to create secure and user-friendly login pages for your applications.