Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Creating First Test in Selenium IDE using FireFox

Starting working with Selenium IDE:

1. Before Working with Selenium IDE, we will need to have mozilla firefox installed in the machine.
2. Also plugins and add-ons for using Selenium needs to be installed from selenium official webSite.
3. Once the add-ons are properly installed, To Open Selenium IDE, go to Web Developer>Selenium IDE or Press "Ctrl+Alt+S"


Understanding Selenium IDE

Below Figure explains various components of Selenium IDE



File Menu - File Menu provides the options to create a new test case, test suite, and the test suite can be exported to Ruby/Java/c# Web Driver or Remote Control.
Recording - Recording starts automatically on launching the IDE.
Test Run - Icon to run  all tests or individual tests is available on Selenium IDE
Test Execution Speed - Test execution script can be set as fast or slow as shown  in figure 1.


Creating First Test in Selenium IDE

Recording starts automatically once we launch Selenium IDE, and perform some action on the Firefox browser.Once some action is performed, the action is displayed is command , e.g : Click , Open, etc. Target displays the property of object on which command is to executed and value is value to be set in input.

Assertions and Verification can not be done during recording and needs to be added manually.Difference between assert and verify Commands is test execution stops once assert statement fails but test execution continues to next step if we execute the Verify command, although the test status fails in both cases.

Tips for adding command: 
User is provided with all the possible commands on a object when Selenium IDE is open. 

Steps:
1. Right Click on an object in Firefox browser.
2. Click on Show All Available Commands>Select the required Command.
3. Command will be added in the test.


Adding command to selenium test

How to compare 2 dictionary object and report the differences in QTP results

This post discusses how to compare two dictionary objects and display the difference between the values in two dictionary objects in table format in QTP results using Reporter.logevent.


''function to compare two Dictionaries 

Public Function Comfunct_CompareDictionary(ObjDictBase, ObjDictGen)

Comfunct_CompareDictionary = True 
''Check if both dictionary objects have the same number of items
If ObjDictBase.Count <> ObjDictGen.Count Then
Reporter.reportevent micFail,"Data in Dictionary 1 and 2","Data mismatch in the dictionary. there are " & objDictBase.Count & "records in dictionary 1 compared to " & objDictGen.Count & "records in dictionary 2" 
Comfunct_CompareDictionary = False
Exit Function
End If
''Compare keys and values
arrKeys = ObjDictBase.Keys
''Create a table for the differences
strTable = "<table border=""""2""""><tr><td>Key Name</td><td><Value in Dictionary 1</td><td><Value in Dictionary 2</td></tr>"
For i = 0 to uBound(arrKeys)
''Compare key names in both dictionaries and validate keys in one dictionary exists in other dictionary
  If Not ObjDictGen.Exists(arrKeys(i)) Then
''ObjDictBase has a key which ObjDictGen doesn't have, hence exiting the function
Comfunct_CompareDictionary = False
Reporter.ReportEvent micFail, "Key " & arrKeys(i) & " exists in dictionary 1 but not in dictionary 2, " Key difference in both dictionaries"
Exit Function 
  End If
  ''Compare value of each keys in both dictionary and display the difference between 2 dictionary in Qtp results 
  If ObjDictBase(arrKeys(i)) <> ObjDictGen(arrKeys(i)) Then
''ObjDictBase value for arrKeys(i) differs from ObjDictGen, then log the difference in the table created above
Comfunct_CompareDictionary = False
strTable = strTable & "<tr><td>" & arrkeys(i) & "</td><td>" & ObjDictBase(arrKeys(i)) & "</td><td>" & ObjDictGen(arrKeys(i)) & "/td></tr>" 
  End If
Next
If Comfunct_CompareDictionary = True Then
Reporter.ReportEvent micPass, "Comparision of dictionary 1 and dictionary 2","Both dictionary matches"
Else
strTable = strTable & "</Table>"
''Compare value of each keys in both dictionary and display the difference between 2 dictionary in Qtp results 
Call reportinQTPhtmlContent(strTable)
End If 
End Function


''Function to report in QTP result in table format

Public Function reportinQTPhtmlContent(strTable)
Set objdict = CreateObject("Scripting.dictionary")
objdict.Add "Status", micdone
objdict.Add "NodeName", "Table Data"
objdict.Add ("StepHtmlInfo"), strTable
Reporter.LogEvent "User", objdict, Reporter.GetContext
End function


How to Configure Eclipse to work with Selenium

Before starting working with Selenium using Eclipse IDE, we did some configuration that needs to be done for starting and understanding Selenium. This post will explain step wise, how to start working with selenium using Eclipse:


Pre- Conditions: We will require following before beginning the configuration:

1. Java needs to be installed in the machine.

2. Eclipse IDE needs to be installed .

3. Standalone libraries of selenium, at the time of writing this post, I am using selenium-server-standalone-2.33.0.jar

4. Verify in Path variable in User defined variable for machine, is set to the path where standalone libraries is placed as shown below




Configuration Steps: 




Step 1: In eclipse, create a new java project as shown below from File>New Java Project


Create a new Java Project

Step 2: Once a java project is created, right click on project and select option "Build Path>Configure build path

Configure build path of project

Step 3: Click on Add External JAR's and add the selenium standalone jar as shown below.

 Step 4: Now in src for the project, Add a new package, and add a new class as shown below.


Select methods stubs to create as public static void main(String[] args)


                                         

Step 5: Write the code in the class created above and execute. This will open Google in internet explorer . Write selenium in search and click on search.


Code to be placed in void main:


WebDriver ieDriver = new InternetExplorerDriver(); ieDriver.get("http://www.google.com"); WebElement element = ieDriver.findElement(By.name("q")); element.sendKeys("selenium"); element.submit();


Manipulating Array with examples using vbscript in QTP

In this post we discusses examples working with arrays and manipulating them in vbscript:

1.How to count number of elements in an array


intcount = ubound(arrayname)

2. How to join all the values in an array to a string and display the value in messagebox


dl=Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
daysinWeek = join(dl, " ")
msgbox "days in weeks " & daysinWeek


3. How to split values in a string in an array based on delimiter


dl="Sunday>Monday>Tuesday>Wednesday>Thursday>Friday>Saturday"
daysinWeek = split(dl, ">")

This method can be used on looping as shown in example below:

On error resume next

Set objDesc= Description.create

'Set of Property values are delimited by "," 

If (instr(1,strDescObj,",") >0) Then

''Split multiple links based on delimiter.

arrDesc = split(strDescObj,",")

intPropSet = Ubound(arrDesc)

''Loop through each of the link and click the required link

for i = 0 to intPropSet

strDesc = split(arrDesc(i),"|")

objDesc(strDesc(0)).value = strDesc(1)

Next

End If



Understanding Date time function: Using VBScript in QTP

Below are some of the useful function for working with date/time in vb script. We will use the functions and illustrate where the functions can be used.


1. Date - This returns the current date of the system.


2. Now - This returns the current Date and time of the system.So now contains time part as well as date part while date function returns only date.


3. Time - This returns the current time of the system. So in terms of relation between date , time, and now function 


Now = date + time

4. Year, Month, Day, hour, minute, second - These functions return year, month,day,hour,minute,second part for the expression.


example : day(now) or  minute(now)

Above functions discussed above are useful for timestamp , and also for generating a unique number based on the function used.


5. Timer - Timer returns the number of seconds from 12:00a.m. This function is very useful for evaluating time spent in performing an activity by taking timer at the start and end of the activity and subtracting the time between two.


6. DatePart - returns specific part of the date .Possible values or part of date , that can be extracted as shown below:

  • yyyy - Year
  • q - Quarter
  • m - Month
  • y - Day of year
  • d - Day
  • w - Weekday
  • ww - Week of year
  • h - Hour
  • n - Minute
  • s - Second
Syntax of the function is : DatePart("m",d))where d is the date as "2013-06-09" and m will return the month of the date.

7. DateDiff - Returns the difference between two dates: 

Syntax : DateDiff(interval,date1,date2). Interval can be yyyy,q,m, y,d,.... as discussed earlier.


8. DateAdd - Add specified date to the date.

Syntax : DateAdd(interval,number,date)


VB Script functions to manipulate strings in QTP

We should know the useful vb script function to manipulate strings. In this post, we will discuss some of the vb script function to manipulate strings and how they can be used in codes:

1. Instr searches for a string within another another string and returns the location of occurrence of one string within another string.It returns the position of first occurrence of the string.This function is used if we need to verify if a particular text appears in a string.


Syntax : InStr([start,]string1,string2[,compare])


Example - Report and loop in case a particular text in not found in the string :

If (Instr(1, string1, string2)>0) Then
    Reporter.ReportEvent micpass , "string 2 is found in string 1","As expected"
Else
    Reporter.ReportEvent micfail , "string 2 is  found in string 1","string 2 is not found in string1"
End If

2. InstrRev is similar to instr and can be used for similar purposes, with only difference search starts from the end of string, although count returned is from start of string.


InStrRev(string1,string2[,start[,compare]])


3. Trim , lTrim, Rtrim removes characters from both left and right, only left and only right respectively blank spaces.


4. Left , Right, and Mid returns a specified character from left, right, and middle of the string. 


Mid(string,start[,length])

5. len returns number of characters in the string.


6. Split splits a string based on the delimiter in the function.


Example - How to find the total number of occurrence of a string in another string.By default, string2 has value space if not provided.

arrString = split(string1,string2)
totOccurence = ubound(arrString)

Example- Total number of words in a string.

arrString = split(string1)
totOccurence = ubound(arrString)




What are different activities in Testing Process

Testing Process includes following activities that should be followed sincerely for proper testing.



1. Test Planning


Before beginning with testing, we need to define test plan based on which we perform testing for the rest of testing life cycle. We should develop a test plan based on following key points to be considered in testing life cycle.

  • Test Objectives should be measurable, prioritized, and should be signed off between stakeholders before moving forward with test planning. Acceptance Criteria should also be defined during objective settings to validate if the test objective has been accompanied.
  • Test Matrix should be defined on how to test the test objectives and to validate all the requirements are covered in testing.
  • Test Schedule should be defined, how milestones in testing will be covered and timeframe in which milestones will be delivered
  • Budgeting and resources planing should be defined.
  • Testing material including environment ans software/availability should be defined.
  • Training required for testing should be defined.


2. Test Control Activities

Once Test plan is created and signed off by stakeholder, Activities in test plan should be compared with actual and any deviation from the projected plan should be reported to stakeholders as risk.


3. Test Analysis

In this phase, test objectives defined in test planning are converted into test scenarios and test cases.Below are the main task in this phase:

  • Reviewing the test artifacts like business requirements, design, and test objectives.
  • Identifying and prioritizing test scenarios and test cases based on business requirements.
  • Identification of test data for execution of test conditions
  • Test environment availability


4. Test implementation

Below are activities in test implementation

  • Developing and prioritizing test cases with test steps
  • Preparation of test data for execution of test scripts.
  • Environment set up for testing.
  • Development of automation test harness.
  • Preparation of test suites for regression, smoke testing, and execution of similar tests in batch.


5. Test Execution and Reporting

  • Execution of test scripts either manual or automated.
  • Reporting defects found during test script execution.
  • Regression testing and retesting of fixed defects. 
  • Reporting of test execution status to stakeholders.


6.  Exit criteria

  • Evaluating Testing status against the exit criteria specified in test planning.
  • Test Status should validate that all acceptance criteria are verified.

7. Test Closure Activities

  • Delivering all the planned test artifacts to the client. For e.g : Automated Test Scripts,test scripts, test data for further reuse.
  • Handover of artifacts and knowledge transfer to maintenance team
  • Analysis of lesson learnt during testing process.

How to create a database checkpoint in QTP

A database checkpoint compares the values returned by SQL query during checkpoint execution. Below are the steps to create a database checkpoint in QTP


  • Navigate to Insert>Checkpoint>Database Checkpoint.


  • Define whether to define SQL statement manually or using Microsoft Query


  • Select the database or query if previously defined as shown below.We select QT_Flight_32 database.Click on OK.

  • Select the required columns/tables to be selected in SQL and click on next button.


  • Define conditions for selected columns in the above step and define sorting criteria. Click Next>Next


  • Click on finish



  • Change the properties of database checkpoint and select whether to insert the checkpoint before or after current step and clcik on OK. A database checkpoint is created and inserted in the test.



How to add result as HTML table format in QTP test results

We can use reporter.logevent method as explained in code/functions below to add results in  tabular format in QTP test results. Below is an example on how to use reporter.logevent


Public Function func_createHTMLTableFromExcel(strDataFile,strWorksheetName)

Set objXlHandle = Createobject("Excel.Application")

 objXlHandle.visible = false

Set objExcelWB = objXlHandle.Workbooks.Open(strDataFile)

Set objExcelWS = objExcelWB.Worksheets(strWorksheetName)

'Getting column and row count 

 strColumnCount = objExcelWS.UsedRange.Columns.Count

 strTotRows = objExcelWS.UsedRange.Rows.Count

strTable = "<table border=""""2"""">"

'Creating a html table based on content of the excel file

For j=1 to strTotRows

strTable = strTable & "<tr>"

For i=1 to strColumnCount

 strData = Trim(objExcelWS.Cells(j,i))

 strTable= strTable & "<td>"& strData &"</td>"

Next

strTable =strTable & "</tr>"

Next

strTable = strTable & "</table>"

Call reportinQTPhtmlContent(strTable)

'Closing the workbook

 objExcelWB.Close

End Function


Public Function reportinQTPhtmlContent(strTable)

Set objdict = CreateObject("Scripting.dictionary")

objdict.Add "Status", micdone

objdict.Add "NodeName", "Table Data"

objdict.Add ("StepHtmlInfo"), strTable

Reporter.LogEvent "User", objdict, Reporter.GetContext

End function

QTP Script to convert content of excel into a html document

Suppose I have some data in excel file and I want to create a html table from the excel file content. Below code can create a html table based on the content of an excel file.We can understand how to create a html file using scripting.filesystemObject in QTP. 


Example of how to call the function:

func_createHTMLTableFromExcel "E:/test.xlsx","Sheet1","e:/ht.htm"

Code for the function:

Public Function func_createHTMLFromExcel(strDataFile,strWorksheetName,strHTMLFile)

Set objXlHandle = Createobject("Excel.Application")

 objXlHandle.visible = false

Set objExcelWB = objXlHandle.Workbooks.Open(strDataFile)

Set objExcelWS = objExcelWB.Worksheets(strWorksheetName)

'Getting column and row count 

 strColumnCount = objExcelWS.UsedRange.Columns.Count

 strTotRows = objExcelWS.UsedRange.Rows.Count

strTable = "<table border=""""2"""">"

'Creating a html table based on content of the excel file

For j=1 to strTotRows

strTable = strTable & "<tr>"

For i=1 to strColumnCount

 strData = Trim(objExcelWS.Cells(j,i))

 strTable= strTable & "<td>"& strData &"</td>"

Next

strTable =strTable & "</tr>"

Next

strTable = strTable & "</table>"

set objFSO=Createobject("scripting.FileSystemObject")

set objtxt = objFSO.createTextFile(strHTMLFile)

    objtxt.write(strTable)

'Closing the workbook

 objExcelWB.Close

 set objFSO =nothing

End Function

How to add Attachments in QC during test script execution in QTP

Using below code, we can add attachment to QC during test script execution:

''This function will add the required attachment in QC, for Location defined by argument strLocationinQC

Function func_AddAttachmentToQC(FilePath,strLocationinQC)



On Error Resume next

If (ucase(strLocationinQC) = "TEST") Then

Set objCurrentTest = QCUtil.CurrentTest.Attachments

Set objAttach = objCurrentTest.AddItem(Null)

ElseIf(ucase(strLocationinQC) = "TESTSET") Then

Set objCurrentTest = QCUtil.CurrentTestSet.Attachments

Set objAttach = objCurrentTest.AddItem(Null)

ElseIf(ucase(strLocationinQC) = "TESTRUN") Then

Set objCurrentTest = QCUtil.CurrentRun.Attachments

Set objAttach = objCurrentTest.AddItem(Null)

End If

objAttach.FileName = strFilePath

objAttach.Type = 1

objAttach.Post

objAttach.Refresh

End Function