Class in oops

Class in oops

In this article, an object based technique of creating test code using VBScript classes will be shown. Usage of classes helps in keeping all methods and properties for a module intact, and if done correctly, this can simply maintenance greatly. This article serves as an introduction to Class usage in QTP. In the coming articles, we will discover advanced concepts in building highly stable and robust class modules.

Each modular class can contain one or more methods, which provide some functionality to a framework. Before adding the appropriate methods, we must create a modular class. In our demonstration, ‘clsLogin’ has been created, which as the name suggests, is a Login class.

Class clsLogin

End Class

The goal of the class we created above is to Login to the Mercury Tours website. Let’s name our method ‘Login’ and include the steps that the method will perform:

Class clsLogin

‘ Login

Public Function Login( sUserName, sPassword )

‘Page title

Dim sTitle: sTitle = “Welcome: Mercury Tours”

Login = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Set the userName

.WebEdit( “name:=userName” ).Set sUserName

‘Set the password

.WebEdit( “name:=password” ).SetSecure sPassword

‘Click SignIn

.Image( “name:=login” ).Click

End With

‘If the correct Page appears: Step Passed

If Browser( “title:=Find a Flight: Mercury Tours:” ).Exist( 5 ) Then

Login = True

End If

End Function

End Class

Let’s assume we have to check properties of the page banner, which is a part of our test-case. In reality, there may be multiple objects for which properties are being verified and they can be included within the same method. For our example though, we will verify property of a single object: page banner.

Class clsLogin

‘ CheckObjectProperties

Public Function CheckObjectProperties

‘Page title

Dim sTitle: sTitle = “Welcome: Mercury Tours”

CheckObjectProperties = False

With Browser(“title:=” & sTitle).Page(“title:=” & sTitle)

‘Check Page Banner

If .Image(“file name:=banner2.gif”).GetROProperty(“width”) = 488 Then

‘If the width equals the expected width, step passed

CheckObjectProperties = True

Reporter.ReportEvent micPass, “Banner”, “Size Correct.”

Else

Reporter.ReportEvent micFail, “Banner”, “Size Incorrect.”

End If

End With

End Function

‘ Login

Public Function Login( sUserName, sPassword )

Dim sTitle: sTitle = “Welcome: Mercury Tours”

Login = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Set the userName

.WebEdit( “name:=userName” ).Set sUserName

‘Set the password

.WebEdit( “name:=password” ).SetSecure sPassword

‘Click SignIn

.Image( “name:=login” ).Click

End With

Browser( “title:=” & sTitle ).Sync

‘If the correct Page appears: Step Passed.

If Browser( “title:=Find a Flight: Mercury Tours:” ).Exist( 5 ) Then

Login = True

End If

End Function

End Class

The 2 methods discussed above are quite common: we generally perform actions on objects and create checks to replace (manual) visual verifications. Now, before we can really perform the above 2 checks, we may also need to check whether we’re on the correct page. Let’s add another method for that:

Class clsLogin

‘ IsPageFound: Check if we’re on the correct page

Public Function IsPageFound

‘Page title

Dim sTitle: sTitle = “Welcome: Mercury Tours”

IsPageFound = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

If .Exist( 10 ) Then

IsPageFound = True

End If

End With

End Function

‘ CheckObjectProperties

Public Function CheckObjectProperties

Dim sTitle: sTitle = “Welcome: Mercury Tours”

CheckObjectProperties = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Check Page Banner

If .Image(“file name:=banner2.gif”).GetROProperty(“width”) = 488 Then

CheckObjectProperties = True

Reporter.ReportEvent micPass, “Banner”, “Size Correct.”

Else

Reporter.ReportEvent micFail, “Banner”, “Size Incorrect.”

End If

End With

End Function

‘ Login

Public Function Login( sUserName, sPassword )

Dim sTitle: sTitle = “Welcome: Mercury Tours”

Login = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Set the userName

.WebEdit( “name:=userName” ).Set sUserName

‘Set the password

.WebEdit( “name:=password” ).SetSecure sPassword

‘Click SignIn

.Image( “name:=login” ).Click

End With

‘If the correct Page appears: Step Passed.

If Browser( “title:=Find a Flight: Mercury Tours:” ).Exist( 5 ) Then

Login = True

End If

End Function

End Class

If we observe the code above, the variable “sTitle” is present in each method. This is quite redundant. We can simply replace this with a readOnly property:

Class clsLogin

‘ Property sTitle

Private Property Get sTitle ‘ReadOnly Property

sTitle = “Welcome: Mercury Tours”

End Property

‘ IsPageFound

Public Function IsPageFound

IsPageFound = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

If .Exist( 10 ) Then

IsPageFound = True

End If

End With

End Function

‘ CheckObjectProperties

Public Function CheckObjectProperties

CheckObjectProperties = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Check Page Banner

If .Image(“file name:=banner2.gif”).GetROProperty(“width”) = 488 Then

CheckObjectProperties = True

Reporter.ReportEvent micPass, “Banner”, “Size Correct.”

Else

Reporter.ReportEvent micFail, “Banner”, “Size Incorrect.”

End If

End With

End Function

‘ Login

Public Function Login( sUserName, sPassword )

Login = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Set the userName

.WebEdit( “name:=userName” ).Set sUserName

‘Set the password

.WebEdit( “name:=password” ).SetSecure sPassword

‘Click SignIn

.Image( “name:=login” ).Click

End With

‘If the correct Page appears: Step Passed.

If Browser( “title:=Find a Flight: Mercury Tours:” ).Exist( 5 ) Then

Login = True

End If

End Function

End Class

Even though some methods of a class are crucial, they may only perform background operations. In our demonstration, IsPageFound and CheckObjectProperties are such methods. Both methods verify objects, and in the automation world, they can be very reasonable prerequisites to the main action, which is to Login to the application. In the demonstration below, IsPageFound and CheckObjectProperties have been marked as private. Both methods are included as a prerequisite to setting the userName and password for logging to the website. To reiterate, both IsPageFound and CheckObjectProperties can be made private methods of the class:

Class clsLogin

‘ IsPageFound: Private Function

Private Function IsPageFound

IsPageFound = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

If .Exist( 10 ) Then

IsPageFound = True

End If

End With

End Function

‘ CheckObjectProperties: Private Function

Private Function CheckObjectProperties

CheckObjectProperties = False

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Check Page Banner

If .Image( “file name:=banner2.gif” ).GetROProperty( “width” ) = 488 Then

CheckObjectProperties = True

Reporter.ReportEvent micPass, “Banner”, “Size Correct.”

Else

Reporter.ReportEvent micFail, “Banner”, “Size Incorrect.”

End If

End With

End Function

‘ Login: Public Function

Public Function Login( sUserName, sPassword )

Login = False

If IsPageFound Then

If CheckObjectProperties Then

With Browser( “title:=” & sTitle ).Page( “title:=” & sTitle )

‘Set the userName

.WebEdit( “name:=userName” ).Set sUserName

‘Set the password

.WebEdit( “name:=password” ).SetSecure sPassword

‘Click SignIn

.Image( “name:=login” ).Click

End With

End If

End If

‘If the Page with the title ‘Find a Flight: Mercury Tours:’ appears: Step Passed.

If Browser( “title:=Find a Flight: Mercury Tours:” ).Exist( 5 ) Then

Login = True

End If

End Function

‘ Property sTitle: Private Property

Private Property Get sTitle

sTitle = “Welcome: Mercury Tours”

End Property

End Class

If you notice the code above, you will see that both private methods have become a part of the Login method. They’re both prerequisites to logging in to the application. Only once both are True, will the code perform the code-action of the class. We can implement the class above using the following snippet:

Public Function Login( sUserName, sPassword )

Dim oAction: Set oAction = New clsLogin

Login = oAction.Login( sUserName, sPassword )

End Function

If you would like to keep ‘IsPageFound’ and ‘CheckObjectProperties’ as public methods of the class, then you can even do this:

Public Function Login( sUserName, sPassword )

Dim oAction: Set oAction = New clsLogin

Login = False

With oAction

If .IsPageFound Then

If .CheckObjectProperties Then

Login = .Login( sUserName, sPassword )

End If

End If

End With

End Function

It is possible for a single function to do everything, but don’t you think its much easier to understand what each method of the class does with little effort? ‘IsPageFound’ simply checks if we’re on the correct page, ‘CheckObjectProperties’ checks if the object was loaded correctly, and ultimately, ‘Login’ performs to core-action by logging the user to the application. Moreover, all methods are grouped together in a class.

In practice, the entire test suite can be created on this principle with each module representing a core-functionality of the overall test. There are several ways to bind these modules together, and we must find an elegant solution for that as well. This is because, because of some major changes, we may need to completely remove a module, or even add one. Therefore, if done correctly, even such a big change can be implemented without much trouble. We will discuss such a technique in the next article in this series, which will be an advanced implementation of class usage in QTP.

I hope you guys find this article useful.

Introduction

This article will discuss about Polymorphism in object oriented programming.

What is Polymorphism?

  • Polymorphism is one of the primary characteristics (concept) of object-oriented programming.
  • Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details.
  • Polymorphism is the characteristic of being able to assign a different meaning specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
  • Polymorphism is the ability to process objects differently depending on their data types.
  • Polymorphism is the ability to redefine methods for derived classes.

Types of Polymorphism

  • Compile time Polymorphism
  • Run time Polymorphism

Compile time Polymorphism

  • Compile time Polymorphism also known as method overloading
  • Method overloading means having two or more methods with the same name but with different signatures
Example of Compile time polymorphism

clip_image001

Run time Polymorphism

  • Run time Polymorphism also known as method overriding
  • Method overriding means having two or more methods with the same name , same signature but with different implementation
Example of Run time Polymorphism

clip_image002

Define class in oops?

View Slide Show

  • clip_image003

View Slide Show

Related Questions

Answer:

A class is a collection of related data and functions which can serve to keep information organized and procedures grouped in a logical fashion.
Generally, a class defines an object or group of objects with similar features. It provides methods for creating, editing and destroying an instance of the object type defined by the class.
Remember the three key features of OOPs

  1. Abstraction – the abstract data-type representing all aspects of the object is entirely defined within the class. This is usually done by aggregating terminal data-types into records or other higher level structures that represent the idea of the object without using a specific instance of the object. The abstract data type is public.
  2. Encapsulation – everything needed to work with an object is located within it’s class. Functions to create, manipulate and dispose of any instance of an object are all hidden inside the class. the names of the functions and their parameters are public, however, their underlying logic and structural code are private.
  3. Polymorphism – a class is able to transform itself and the objects it represents depending on the parameters passed to it, e.g. a class that represents regular polygons requires only one parameter to determine which polygon to draw (n>=3). It requires a second parameter to tell it how long each side should be. The first parameter makes the class polymorphic.

To these three ideas several others have been added, being special cases of these three ideas or workflow paradigms from general programming practice: re-usable, overloading, inheritance.

  1. Re-usabilty means that the code can be used again in new work because it has already been tested and debugged – this comes form the idea of code libraries or repositories where a coder or company stores code that, having passed all testing phases and been successfully implemented in other projects, is deemed ready for re-use
  2. Overloading is a special case of polymorphism. It simply means using the parameters passed to the class to cause the operators within the class to behave differently.
  3. Inheritance – a class inherits features from superior classes and bequeaths features to lower classes. For example a class used for drawing geometric shapes to the screen might have as its base parameters a colour and a thickness/size of line/point. This class will probably inherit a set of features from the i/o classes. The class then has sub-classes to
    • draw points, requiring parameters that give it x, y and maybe z coordinates
    • draw lines, requiring parameters that give start and finish coordinates or start and angular direction coordinates and length of line.
    • draw shapes, requiring parameters that give the shape a type (number of sides), length of side, starting coordinate, whether the starting coordinate is the top-left corner of the shape or the geometric centre of the shape, etc.
    • draw other segments and shapes, such as circles, ellipses, arcs, etc.

Comments