Don’t Assume that Smart Is Always Smart

Smart identification is way by which QTP identifies an object when it is not found in the object repository during the run session. But it is not advisable to use this feature. Because QTP assumes and finds the object during run session, it is not a good idea to assume any think in software testing.

Let us see a simple example how QTP smart identification fails.

Scenario

I want to verify a simple web page which comes, says after I perform some transactions in a website. The web page has a simple text message “Your transaction is successful” after a successful transaction and “Your transaction is not successful” after an unsuccessful transaction. Here you are automating a scenario to check a successful transaction page exists or not after a successful transaction.

The code to check that is

If browser (“Smart is not Smart”).Page (“Smart is not Smart”).WebElement(“Your transaction is successful”).Exist Then

reporter.ReportEvent micPass,”Pass”,”Result Pass”

Else

reporter.ReportEvent micFail,”Fail”,”Result Fail”

End If

This is the sample of that page

sucessful

And the result is

pass

But now consider that the transaction is not successful and you got a message “Your transaction is not successful”

Here is the sample page

unsuccessful

But if you run the above code, the result will be still passing.

pass-warning

What cause this problem? Mr. Smart Identification

The only difference between those web element in successful and unsuccessful transaction is the text. So QTP assumes both the object is same and says pass. Yes, it gives a warning on using smart identification but it is visible only in the QTP result window. If you are using some other external means to document the result, this will never get noticed.

So it is always better to disable smart identification, the simplest way to do is go to File –>Setting–>Run tab and check “Disable Smart Identification during Run Session”

If you want to disable smart identification in you Quicktest Automation Object Model, the script is

Dim App ‘As Application

Set App = CreateObject(“QuickTest.Application”)

App.Test.Settings.Run.DisableSmartIdentification = True

Here is the result after disabling the smart identification.

fail

Advertisements

Count Number of links in a Web Page

In this let us see a script to count number of links in a web page.This will help to understand the descriptive programing better and will help to solve many practical requirements like finding the broken links.

Let us count the number of links in google’s home page

Set mygooglesearch = Description.Create()

Then you declare the property name and value pair for that object,here since we are counting the link,we have to use micclass property  of type link.

mygooglesearch(”micclass”).value =”Link”

Now use childobjects to get the properties into link_count objects,this will give all the child objects of that page of type link.

Set link_count = Browser(“name:=google”).Page(“title:=google”).childobjects(mygooglesearch)

Now we get the object count using count method.

msgbox link_count.count

Simple Parametrization in QTP

Parameterization is used when we need to handle run time data in scripts, consider a scenario of verification of user login to gmail. We verify the user name displayed at the top right corner to make sure that the user logged is correct. But the name displayed depends on the login id used and is not static, so we need to parameterize the value.
Parameterization may be done by passing values as variable or through data sheet. It is always a good practice to user data sheet, so that editing the data during play back and maintenance will be easier.
Below is the sample code for parameterization through variables
Id = “<emailid@gmail.com>”
Pwd= “<password>”
Browser(“Google”).Page(“Google”).Link(“Sign in”).Click
Browser(“Google”).Page(“Google Accounts”).WebEdit(“Email”).Set(Id)
Browser(“Google”).Page(“Google Accounts”).WebEdit(“Passwd”).SetSecure(Pwd)
Browser(“Google”).Page(“Google Accounts”).WebButton(“Sign in”).Click
‘ Verification point verifies whether used has successfully logged in by checking the email displayed after logging in
Login = Browser(“Google”).Page(“iGoogle”).WebElement(“innertext:=”&Id).GetTOProperty(“innertext”)
msgbox(Login)
If Login=Id Then
Browser(“Google”).Page(“Google”).Link(“iGoogle”).Click
Browser(“Google”).Page(“iGoogle”).Link(“innertext:=Sign Out”).Click
Else
msgbox(“Login Failed”)
End If
Below is the sample code for parameterization through datasheet
Browser(“Google”).Page(“Google”).Link(“Sign in”).Click
Browser(“Google”).Page(“Google Accounts”).WebEdit(“Email”).Set(DataTable(“EmailID”, dtGlobalSheet))
Browser(“Google”).Page(“Google Accounts”).WebEdit(“Passwd”).SetSecure(DataTable(“Password”, dtGlobalSheet))
Browser(“Google”).Page(“Google Accounts”).WebButton(“Sign in”).Click
‘ Verification point verifies whether used has successfully logged in by checking the email displayed after logging in
Login = Browser(“Google”).Page(“iGoogle”).WebElement(“innertext:=”&DataTable(“EmailID “, dtGlobalSheet)).GetROProperty(“innertext”)
msgbox(DataTable(“EmailID”, dtGlobalSheet))
If Login=DataTable(“EmailID”, dtGlobalSheet) Then
Browser(“Google”).Page(“Google”).Link(“iGoogle”).Click
Browser(“Google”).Page(“iGoogle”).Link(“innertext:=Sign Out”).Click
Else
Message = Browser(“Google”).Page(“Google Accounts”).Webelement(“html id:=errormsg_0_Passwd”).GetROProperty(“innertext”)
msgbox(Message)
If Message = “Username and password do not match” Then
msgbox(“Login Failed”)
Else
msgbox(“Invalid Error message”)
End If
End If

Parameterization is used when we need to handle run time data in scripts, consider a scenario of verification of user login to gmail. We verify the user name displayed at the top right corner to make sure that the user logged is correct. But the name displayed depends on the login id used and is not static, so we need to parameterize the value.

Parameterization may be done by passing values as variable or through data sheet. It is always a good practice to user data sheet, so that editing the data during play back and maintenance will be easier.

Below is the sample code for parameterization through variables

Id = “<emailid@gmail.com>”

Pwd= “<password>”

Browser(“Google”).Page(“Google”).Link(“Sign in”).Click

Browser(“Google”).Page(“Google Accounts”).WebEdit(“Email”).Set(Id)

Browser(“Google”).Page(“Google Accounts”).WebEdit(“Passwd”).SetSecure(Pwd)

Browser(“Google”).Page(“Google Accounts”).WebButton(“Sign in”).Click

‘ Verification point verifies whether used has successfully logged in by checking the email displayed after logging in

Login = Browser(“Google”).Page(“iGoogle”).WebElement(“innertext:=”&Id).GetTOProperty(“innertext”)

msgbox(Login)

If Login=Id Then

Browser(“Google”).Page(“Google”).Link(“iGoogle”).Click

Browser(“Google”).Page(“iGoogle”).Link(“innertext:=Sign Out”).Click

Else

msgbox(“Login Failed”)

End If

Below is the sample code for parameterization through datasheet

Browser(“Google”).Page(“Google”).Link(“Sign in”).Click

Browser(“Google”).Page(“Google Accounts”).WebEdit(“Email”).Set(DataTable(“EmailID”, dtGlobalSheet))

Browser(“Google”).Page(“Google Accounts”).WebEdit(“Passwd”).SetSecure(DataTable(“Password”, dtGlobalSheet))

Browser(“Google”).Page(“Google Accounts”).WebButton(“Sign in”).Click

‘ Verification point verifies whether used has successfully logged in by checking the email displayed after logging in

Login = Browser(“Google”).Page(“iGoogle”).WebElement(“innertext:=”&DataTable(“EmailID “, dtGlobalSheet)).GetROProperty(“innertext”)

msgbox(DataTable(“EmailID”, dtGlobalSheet))

If Login=DataTable(“EmailID”, dtGlobalSheet) Then

Browser(“Google”).Page(“Google”).Link(“iGoogle”).Click

Browser(“Google”).Page(“iGoogle”).Link(“innertext:=Sign Out”).Click

Else

Message = Browser(“Google”).Page(“Google Accounts”).Webelement(“html id:=errormsg_0_Passwd”).GetROProperty(“innertext”)

msgbox(Message)

If Message = “Username and password do not match” Then

msgbox(“Login Failed”)

Else

msgbox(“Invalid Error message”)

End If

End If

Close All Opened Browser

Methods Used : Descriptive programming,Regular Expression and Ordinal Identifier Properties.

Call Closebrowser()

Function Closebrowser

While Browser(“CreationTime:=.*”).Exist
Browser(“CreationTime:=.*”).Close
Wend
End Functio
n

The above code will close all the browser that are opened.

Here instead of creation time other ordinal identifier can also be used.