Create log file in QTP

Function LogintoFile(strCode, strMessage) Dim objFS Dim objFile Dim objFolder Dim strFileName Set objFS = CreateObject("Scripting.FileSystemObject") If Not objFS.FolderExists(objFS.GetAbsolutePathName(".") & "\log") Then Set objFolder = objFS.CreateFolder(objFS.GetAbsolutePathName(".") & "\log") End If strFileName = objFS.GetAbsolutePathName(".") & "\log\" & year(date) & month(date) & day(date) & ".log" Set objFile = objFS.OpenTextFile(strFileName, 8, True) On Error Resume Next objFile.Write Date & ", " & Time & ", " & strCode & ", " & strMessage & vbcrlf ' disable the on error statement On Error GoTo 0 objFile.Close Set objFS = Nothing End Function 'Function Calling : call LogintoFile("Fail","Invalid User Credentials")

Handling web table in Web driver


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;	
import org.openqa.selenium.support.ui.Select;

public class Kesineni {

    WebDriver driver;
    public void test() throws InterruptedException {
    	//launch the firefox browser
        driver=new FirefoxDriver();
        //enter URL Kesinenitravels application
        driver.get("http://www.kesinenitravels.com");
        //Select source 
        new Select(driver.findElement(By.id("ctl00_cpEndUserMain_ucSearchRoute_ddlSource"))).selectByVisibleText("CHENNAI");
        Thread.sleep(4000);
        //Select Destination
        new Select(driver.findElement(By.id("ctl00_cpEndUserMain_ucSearchRoute_ddlDestination"))).selectByVisibleText("HYDERABAD");
        Thread.sleep(4000);
        //Click on Search button
        driver.findElement(By.id("ctl00_cpEndUserMain_ucSearchRoute_ibtnSearch")).click();
        Thread.sleep(7000);
        //Get the text of ServiceNo's
        List ele=driver.findElements(By.xpath("//table[@id='ctl00_cpEndUserMain_ucOnwardJourney_gvServices']/tbody/tr/td[2]"));
        System.out.println(ele.size());
        //print text
       //Create one list
        List one = new ArrayList();
        for(int i=0;i ele1=driver.findElements(By.xpath("//table[@id='ctl00_cpEndUserMain_grdvCheckFare']/tbody/tr/td[2]"));
        List two = new ArrayList();
        for(int i=0;i

Selenium server start up problems: Failed to start: SocketListener0@0.0.0.0:4444

If the below appear while running the selenium scripts, Failed to start: SocketListener0@0.0.0.0:4444 you need to kill the process "javaw.exe" by going to the task manager If this is a routine case then you can write a reusable function and call it wherever required

Capture ToolTip from QTP


'for Web Elements
msgbox browser("browser").Page("pagename").WebElement("webelementname").Object.title

'for images
msgbox Browser("browser").Page("pagename").Image("imagename").GetROProperty("alt")

'for Links
x=Browser("browser").Page("page").Link("linkname").GetROProperty("OuterHtml")
temp=split(x,chr("34"))
ToolTip=temp(1)
msgbox ToolTip

Delete cookies using QTP

SystemUtil.Run "Control.exe","inetcpl.cpl"
Set objShell = CreateObject("Wscript.Shell")
Do Until delcookie = True
    delcookie = objShell.AppActivate("Internet Properties")
    Wait(1)
Loop
objShell.Sendkeys "%i"
Wait(1)
objShell.Sendkeys "{ENTER}"
Wait(1)
objShell.Sendkeys "%f"
Wait(1)
objShell.Sendkeys "%d"
Wait(1)
objShell.Sendkeys "{ENTER}"
Wait(4)
objShell.Sendkeys "{ENTER}"

Prevent system from locking while running QTP scripts

Use the below lines of code in a function that is called frequently

Example: If you have a function to report or function to start your test case etc

set WshShell=CreateObject("WScript.Shell")
WshShell.SendKeys ("^{HOME}")

Lock PC after suite execution

Set obj = CreateObject("WScript.Shell")
sCmnd = "%windir%\SYSTEM32\rundll32.exe user32.dll,LockWorkStation"
obj.Run sCmnd, 0, False

Post defect to QC

Set TDConnection = CreateObject("TDApiOle.TDConnection")
TDConnection.InitConnection "http://testdb/tdbin" 'DB URL
TDConnection.ConnectProject "projectname","username","password"
If TDConnection.Connected Then
 reporter.reportevent micPass,"Connection","Connection successful"
Else
 reporter.reportevent micFail,"Connection","Connection Failed"
End If
Set BugFactory = TDConnection.BugFactory
Set Bug = BugFactory.AddItem (Nothing)
Bug.Status = "New"
Bug.Summary = "Connecting to TD"
Bug.Priority = "4-Very High"
Bug.AssignedTo = "admin"
Bug.DetectedBy = "admin" 
Bug.Post

Batch Execution

Executing a group of Tests or series of tests at a time is known as Batch Execution. 

To perform Batch execution, QTP provides a separate Tool called Test Batch Runner. 

Steps for Batch Testing: 
1) Create Individual Tests and Run once. 
2) Open 'Test batch Runner' Tool and Form Batches
  • Launch Test Batch Runner. 
  • Navigation: Start>program>quicktest professional>Tools>Test Batch Runner>File>new>batch>add>browse path of the test (like this add number of tests)>save with MTB extension (Module test batche)>close test batch runner. 
3) Provide permission to 'Test batch Runner' to run tests 
4) Run or Execute Test Batches from Test Batch Runner
Navigation: File>open>browse path of the test batch>batch>run 
5) View Test wise Result in 'Test Result Viewer'

Note:

1. QTP doesn’t provide Batch wise result. 
2. Test Batch Runner launches QTP Tool, QTP runs Tests one by one. 
3. Allowing other products to Run Tests. Navigation: Tools>Options>Run>check allow other mercury products>apply & Ok 

Executing partial Test Batch: Open Test Batch Runner >open Test Batch>Select or deselect tests>run test batch.

Utility Objects

QTP Provides the several utility objects to enhance the power of scripting.Utility Objects are the reserved objects in qtp.These objects can be used in reporting preferences during run time. There are various Types of Utility Objects: 1)Crypt Object 2)DataTable Object 3)Description Object 4)DotNetFactory Object 5)DTParameter Object 6)DTSheet Object 7)Environment Object 8)Extern Object 9)LocalParameter Object 10)MercuryTimers Object (Collection) 11)MercuryTimer Object 12)Parameter Object 13)PathFinder Object 14)Properties Object (Collection) 15)QCUtil Object 16)RandomNumber Object 17)Recovery Object 18)Reporter Object 19)RepositoriesCollection Object 20)Repository Object 21)Services Object 22)Setting Object 23)SystemMonitor Object 24)TextUtil Object 25)TSLTest Object 26)XMLUtil Object

Mouse right click for context menu

The below code right clicks on a link in a page named “Document” and selects “CreateDocument” in the context menu. Each option in the menu belongs to WebElement class. 'set the path to an object Set pathObj = Browser(“objBrowserName”).Page(“objPageName”) 'Create an object of DeviceReplay class which helps us simulate mouse click operation Set deviceReplay = CreateObject(“Mercury.DeviceReplay”) 'Get the X-axis value of the link getX = pathObj.Link(“Document”).GetROProperty(“abs_x”) 'Get the Y-axis value of the link getY = pathObj.Link(“Document”).GetROProperty(“abs_y”) 'Simulates the right click operation over the link deviceReplay.MouseClick getX,getY,RIGHT_MOUSE_BUTTON 'Click on the “CreateDocument” option. pathObj.WebElement(“name:=CreateDocument”).Click Set deviceReplay = Nothing