Automation object model in QTP (AOM)

Automation object model is nothing but structural representation of objects,methods and properties which are used to perform quicktest operations.Automation enables software packages to expose their unique features to scripting tools and other applications. You can use the QuickTest Professional Automation Object Model to write programs that automate your QuickTest operations.

QuickTest Professional is COM Server and its different methods and properties are exposed by its COM interface which can be accessed by other applications and scripting tools to control it from outside.

To create the automation object, we use “CreateObject” function. Once the object has been created, we can access all other objects, methods and properties of the QuickTest . CreateObject creates and returns the reference to an automation object.


You can write a script that modifies the test object description properties in the Object Identification dialog box and performs an update run on all tests in a specified file folder.

You can define your settings for a test in QuickTest, then click “Generate Script” in the Generate tab of the Test Settings dialog box to generate an automation script based on the current test settings. You can then apply those same settings automatically to multiple tests using the whole automation script or excerpts from the generated file.

Generating an automation script for QuickTest Professional options:

• Navigate to Tools > Options > General and click on the “Generate Scripts” buttons
• Navigate to File > Settings>Properties and click on the “Generate Scripts” buttons
• Navigate to Tools > Object Identification and click on the “Generate Scripts” buttons

Example:
Launch QTP, open an existing test and Run the Test and Store Run Results in Specified Folder:

Dim qtApp
Dim qtTest
Dim qtResultsOpt

'Create the QTP Application object
Set qtApp = CreateObject("QuickTest.Application")

'If QTP is notopen then open it
If  qtApp.launched <> True then

qtApp.Launch

End If

'Make the QuickTest application visible
qtApp.Visible = True

'Set QuickTest run options
qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
qtApp.Options.Run.RunMode = "Fast"
qtApp.Options.Run.ViewResults = False

'Open the test in read-only mode
qtApp.Open "C:\Program Files\HP\QuickTest Professional\Tests\AOMExamp;e", True

'set run settings for the test
Set qtTest = qtApp.Test

'Instruct QuickTest to perform next step when error occurs
qtTest.Settings.Run.OnError = "NextStep"

'Create the Run Results Options object
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")

'Set the results location
qtResultsOpt.ResultsLocation = "D:\AOMTestResult"

' Run the test
qtTest.Run qtResultsOpt

'Check the results of the test run
MsgBox qtTest.LastRunResults.Status

'Close the test
qtTest.Close

'Close QTP
qtApp.quit

'Release Object
Set qtResultsOpt = nothing
Set qtTest = Nothing
Set qtApp = Nothing

HTML reports in QTP

To get html reports after every execution follow the below steps which is one time configuration

1. Open registry
Start > Run > Regedit and click enter

2. Registry will be opened

3. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Mercury Interactive\QuickTest Professional\Logger\Media\Log

4. Change the active DWORD from 0 to 1.

5. Open QTP and run a test. After the execution, the HTML report will be generated and stored in the results folder with name "log"

Associating function library in QTP

Function library can be associated in design time or run-time depending on the framework used.

To associate the function library at design time

navigate to ‘File > Settings > Resources > Associate Function Library’ option in QTP. Click on '+' button and browse for the file and add it to the Test

To associate the function library at run time, you can use any of the following

1) Automation Object Model (AOM)

'Open QTP
Set objQTP = CreateObject("QuickTest.Application")
objQTP.Launch
objQTP.Visible = True

'Open a test
objQTP.Open "D:\Automation\Test1", False, False
Set objFL = objQTP.Test.Settings.Resources.Libraries

'check if it is already associated, else associate it
If objFL.Find("D:\Automation\FunctionLibrary.vbs") = -1 Then
  objFL.Add "D:\Automation\FunctionLibrary.vbs", 1
End


2) using ExecuteFile method.

ExecuteFile "D:\Automation\FunctionLibrary.vbs"

3) using LoadFunctionLibrary method.

LoadFunctionLibrary "D:\Automation\FunctionLibrary.vbs"

Action template in QTP

Action Template is a feature in QTP that allows you to include predefined template for every new actions by default. The template will be included automatically every time you create a new action.

This is a good practice to track few things like purpose, create by, revision history etc.

Example: If the below template is to be set as an action template,


'=========================================================================
'Action Name -
'Purpose -
'Created by -
'Date -

'Change History
'- - - - - - - - - - - - - -
'Date Changed by Comments
'
'=========================================================================

1. Open notepad and copy the above code to it.

2. Save the notepad as "ActionTemplate.mst"

NOTE: Use the double quote while saving the file as this wouldnt add .txt for the file extension.

3. Go to <dat> folder inside QTP installation folder.

Normally it can be found under C:Program Files\Mercury Interactive\QuickTest Professional\dat for versions of QTP below 11 and under C:Program Files\HP\Unified Functional Testing\dat for UFT 11.0 or above.

4. Add the ActionTemplate.mst file in the dat folder.

5. Open QTP and insert a new action. The new action should display the statements that you had saved in the Action Template.

Action Template in QTP




Hnadling webtable using web driver



//Handling WebTable with verifications

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WebTable_SalesForce {
      
       WebDriver driver;
       @Test
       public void test() {
              driver=new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
              driver.get("http://www.salesforce.com");
              driver.findElement(By.id("button-login")).click();
              driver.findElement(By.id("username")).sendKeys("manu.immadi7373@gmail.com");
              driver.findElement(By.id("password")).sendKeys("selenium@123");
              driver.findElement(By.id("password")).sendKeys(Keys.ENTER);
              driver.findElement(By.xpath("//ul[@id='tabBar']/li[5]/a")).click();
              driver.findElement(By.xpath("//form[@id='hotlist']/table/tbody/tr/td[2]/input")).click();
              String firstName="Pavan";
              String lastName="Kalyan";
              String company="Tollywood";
              driver.findElement(By.id("name_firstlea2")).sendKeys(firstName);
              driver.findElement(By.id("name_lastlea2")).sendKeys(lastName);
              driver.findElement(By.id("lea3")).sendKeys(company);
              driver.findElement(By.xpath("//td[@id='bottomButtonRow']/input")).click();
              driver.findElement(By.xpath("//ul[@id='tabBar']/li[5]/a")).click();
             
              driver.findElement(By.xpath("//input[@name='go' and @value='Run Report']")).click();
              String xpath_Start="//*[@id='fchArea']/table/tbody/tr[";
              String xpath_End="]/td[1]";
              boolean recordfound=false;
              int i=2;
              while(isElementPresent(xpath_Start+i+xpath_End))
              {
                     String fname=driver.findElement(By.xpath(xpath_Start+i+xpath_End)).getText();
                     if(fname.equals(firstName)) {
                           //check the lastname
                           String lastnamecol=xpath_Start + i + xpath_End.replace("td[1]","td[2]");
                           String lname=driver.findElement(By.xpath(lastnamecol)).getText();
                           if(lname.equals(lastName)) {
                                  System.out.println("Both are same " +firstName + lastName);
                                  recordfound=true;
                                  i++;
                                  break;
                           }
                     }
              }
       Assert.assertTrue(recordfound, "Record NotAvilable");
              }
       public boolean isElementPresent(String objxpath) {
              int count = driver.findElements(By.xpath(objxpath)).size();
              if(count==0)
                     return false;
              else
                     return true;
       }
      
}

Verify broken links using Web driver




import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
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;

public class BrokenLinks {
    public static int invalidLink;
    String currentLink;
    String temp;
    public static void main(String[] args) throws IOException {
       //Launch The Browser
        WebDriver driver=new FirefoxDriver();
        //Enter Url
        driver.get("http://www.excellenceq.com");
        //Get all the links url
        List<WebElement> ele=driver.findElements(By.tagName("a"));
        System.out.println("size:" +ele.size() );
        for(int i=0;i<ele.size();i++) {
            System.out.println(ele.get(i).getAttribute("href"));
            int statusCode=0;
            try{
            statusCode=getResponseCode(ele.get(i).getAttribute("href"));
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            if(statusCode==404) {
                System.out.println("InvalidLink ********** :"+ele.get(i).getAttribute("href"));
            }else {
                System.out.println(":ValidLinks:"+ele.get(i).getAttribute("href"));
            }
        }


    }
    //The below script will identify all the broken links(if any) in excelenceq.com and store the 404 URLs in List. It verifies the server status, any links giving 404 status.
    public static int getResponseCode(String urlString) throws IOException{
        URL u = new URL(urlString);
        HttpURLConnection h =  (HttpURLConnection)  u.openConnection();
        h.setRequestMethod("GET");
        h.connect();
        return h.getResponseCode();
    }

}

Manoj Kumar
manojqa5006@gmail.com