• QTP关于AOM的Javascript启动方式


    QTP的AOM模型想必大家都很熟悉了,平时常用的就是通过VBS脚本的方式编写启动程序(也是我现在用的方法)。其实,还有很多其他的方式,如Java,C#,JS,这些语言都是通过调用QTObjectModel.dll文件来实现AOM,该文件位于QTP安装目录bin文件夹下。

    简单的启动代码

    下面简单的介绍下通过JS的方式来调用AOM,主要工具是ActiveXObject对象。假设已完成自动化脚本编写并保存为D:QTPjsaom,代码如下

    <html>
    <head>
        <title>JS启动QTP</title>
        <meta http-equiv="Content-type" content="text/html" charset="utf-8">
    </head>
    <body>
        <input type="button" value="运行脚本" onclick="run();">
        <script type="text/javascript" language="javascript">
        function run(){
    
            // 创建AOM对象
            var qtpauto=new ActiveXObject("QuickTest.Application");
            //用于结束AOM进程
            var task_kill=new ActiveXObject("WScript.Shell");
            //启动QTP
            qtpauto.Launch();
            //设置可见
            qtpauto.Visible=true;
            //打开脚本文件
            qtpauto.open('D:\QTP\jsaom');
            //运行脚本
            qtpauto.Test.Run();
            //退出脚本
            qtpauto.Quit();
            //关闭AOM进程
            task_kill.Run("taskkill /f /im QTAutomationAgent.exe");
        }
        </script>
    </body>
    </html>

    WScript.Shell对象用于关闭QTAutomationAgent.exe进程,在调用AOM是系统会自动生成此进程在脚本运行运行完毕后并不会自动退出,当我们调试代码时有时会报错,为了方便每次执行完毕kill就好了。

    我们还可以在代码中指定运行时的参数和结果文件的保存路径

     1 <html>
     2 <head>
     3     <title>JS启动QTP</title>
     4     <meta http-equiv="Content-type" content="text/html" charset="utf-8">
     5 </head>
     6 <body>
     7     <input type="button" value="运行脚本" onclick="run();">
     8     <script type="text/javascript" language="javascript">
     9     function run(){
    10 
    11         // 创建AOM对象
    12         var qtpauto=new ActiveXObject("QuickTest.Application");
    13         //结果对象
    14         var qtpresult=new ActiveXObject("QuickTest.RunResultsOptions");
    15         //用于结束AOM进程
    16         var task_kill=new ActiveXObject("WScript.Shell");
    17 
    18         //启动QTP
    19         qtpauto.Launch();
    20         //设置可见
    21         qtpauto.Visible=true;
    22         //打开脚本文件
    23         qtpauto.open('D:\QTP\jsaom');
    24         //设置脚本只迭代一次
    25         qtpauto.Test.Settings.Run.IterationMode = "oneIteration";
    26         //设置等待对象出现的时间为20000毫秒即20秒
    27         qtpauto.Test.Settings.Run.ObjectSyncTimeOut = "20000";
    28         //出错时继续执行下一步
    29         qtpauto.Test.Settings.Run.OnError = "NextStep";
    30         //设置结果保存路径
    31         qtpresult.ResultsLocation = "D:\QTP\Res"
    32         //运行脚本
    33         qtpauto.Test.Run(qtpresult);
    34         //退出脚本
    35         qtpauto.Quit();
    36         //关闭AOM进程
    37         task_kill.Run("taskkill /f /im QTAutomationAgent.exe");
    38     }
    39     </script>
    40 </body>
    41 </html>

    有了这些功能就可以用复选框来选择每次想要运行的脚本,为此我写了一个简单的例子,界面没怎么调,请轻喷。

     1 <html>
     2 <head>
     3     <title> js启动QTP</title>
     4     <meta http-equiv="Content-type" content="text/html" charset="utf-8">
     5     <link rel="stylesheet" href="qtp.css" type="text/css" >
     6 </head>
     7 <body> 
     8   <h1>JavaScript启动QTP</h1>
     9 
    10     <div>
    11         <form name="select_testscript" method="post" action="">
    12             <table>
    13                 <tr style="background-color: #6fcf6f">
    14                     <td style="background-color: #0066CC;font-size:16px;color: #fff0f5;"><input type="checkbox" name="select_all" value="select_all" onclick="pick_all(this)">全选</td>
    15                     <th colspan="4">请选择回归脚本</th>
    16                 </tr>
    17                 <tr>
    18                     <td style="background-color: #6fcf6f"><input type="checkbox" name="test_normal_all" value="test_normal_all" onclick="line_pick_all(this)">测试脚本正常</td>
    19                     <td style="background-color: #6fc6ff"><input type="checkbox" name="normal" value="演示脚本_正常_1">演示脚本_正常_1</td>
    20                     <td style="background-color: #6fc6ff"><input type="checkbox" name="normal" value="演示脚本_正常_2">演示脚本_正常_2</td>
    21                     <td style="background-color: #6fc6ff"><input type="checkbox" name="normal" value="演示脚本_正常_3">演示脚本_正常_3</td>
    22                     <td style="background-color: #6fc6ff"><input type="checkbox" name="normal" value="演示脚本_正常_4">演示脚本_正常_4</td>
    23                 </tr>
    24                 <tr>
    25                     <td style="background-color: #6fcf6f" rowspan="2"><input type="checkbox" name="test_exception_all" value="test_exception_all" onclick="line_pick_all(this)">测试脚本异常</td>
    26                     <td style="background-color: #f6c6ff"><input type="checkbox" name="exception" value="演示脚本_异常_1">演示脚本_异常_1</td>
    27                     <td style="background-color: #f6c6ff"><input type="checkbox" name="exception" value="演示脚本_异常_2">演示脚本_异常_2</td>
    28                     <td style="background-color: #f6c6ff"><input type="checkbox" name="exception" value="演示脚本_异常_3">演示脚本_异常_3</td>
    29                     <td style="background-color: #f6c6ff"><input type="checkbox" name="exception" value="演示脚本_异常_4">演示脚本_异常_4</td>
    30                 </tr>
    31             </table>
    32                 <br>
    33                 <input type="button" value="运行脚本" onclick="run();" class="button">
    34 
    35         </form>
    36     </div>
     1 <script type="text/javascript" language="javascript">
     2     /*全选函数*/
     3     function pick_all(obj){
     4       var em_number=document.forms.select_testscript.elements;
     5       for(var i=0;i<em_number.length;i++){
     6         if(em_number[i].type=="checkbox"){
     7           if(obj.checked==true)
     8             em_number[i].checked=true;
     9           else
    10             em_number[i].checked=false;
    11         }
    12       }
    13     }
    14     /*每个类型全选函数*/
    15     function line_pick_all(obj){
    16       var objvalue=obj.value;
    17       objvalue=objvalue.substring(0,objvalue.length-4);
    18       var objname=document.getElementsByName(objvalue);
    19       if(obj.checked==true){
    20         for(var i=0;i<objname.length;i++)
    21           objname[i].checked=true;
    22       }else{
    23         for(var i=0;i<objname.length;i++)
    24           objname[i].checked=false;
    25       }
    26     }
    27     /*运行所选脚本*/
    28     function run(){
    29 
    30       var em_number=document.forms.select_testscript.elements;
    31       var script_name=new Array();
    32       var qtpauto=new ActiveXObject("QuickTest.Application");
    33       var qtpresult=new ActiveXObject("QuickTest.RunResultsOptions");
    34       // 获取已选中的复选框
    35       for(var i=0;i<em_number.length;i++){
    36         if(em_number[i].type=="checkbox"){
    37           if(em_number[i].checked==true && em_number[i].value.indexOf("_all")<0)
    38             script_name.push(em_number[i].value)
    39         }
    40       }
    41       qtpauto.Launch();
    42       qtpauto.Visible=true;
    43       // 运行选中脚本
    44       for(var j=0;j<script_name.length;j++){
    45         try{
    46           //打开脚本文件
    47           qtpauto.open('D:\QTP\'+script_name[j]);
    48           //设置脚本只迭代一次
    49           qtpauto.Test.Settings.Run.IterationMode = "oneIteration";
    50           //设置等待对象出现的时间为20000毫秒即20秒
    51           qtpauto.Test.Settings.Run.ObjectSyncTimeOut = "20000";
    52           //出错时继续执行下一步
    53           qtpauto.Test.Settings.Run.OnError = "NextStep";
    54           //设置结果保存路径
    55           qtpresult.ResultsLocation = "D:\QTP\Res"+script_name[j];
    56           qtpauto.Test.Run(qtpauto);
    57         }catch(err){
    58           alert("err.description");
    59         }
    60       }
    61       // 关闭QTPAOM进程
    62       qtpauto.Quit();
    63       var task_kill=new ActiveXObject("WScript.Shell");
    64       task_kill.Run("taskkill /f /im QTAutomationAgent.exe");
    65     }
    66 
    67   </script>
    68 </body>
    69 </html>

    CSS文件

     1 body{
     2       text-align: center;
     3       margin:0; 
     4       padding:0;
     5       font-family: Verdana, Geneva, sans-serif;
     6       background-color:#FFF;
     7       background-size: 100% 100%;
     8 
     9     }
    10 table{  
    11       padding:0;   
    12       margin:0 auto;  
    13       font-size: 16px;
    14       font-weight: bold;
    15       line-height: 1.4em;
    16       font-style: normal;
    17       border-collapse:separate;
    18     }  
    19     table th{
    20       padding:12px;
    21       border:1px solid #93CE37;
    22       border-bottom:3px solid #9ED929;
    23       font-size:20px;
    24     } 
    25     td{  
    26       background: #fff;  
    27       font-size:14px; 
    28       font-family: "微软雅黑",sans-serif;  
    29       color: black;  
    30       padding:8px;
    31       text-align:left;
    32       border: .5px solid #E7EFE0;
    33       border-radius:2px;
    34       text-shadow:1px 1px 1px #fff;
    35     }
    36   .button {
    37       background-color: #6fcf6f;
    38       border: none;
    39       border-radius: .25em;
    40       color: #444;
    41       cursor: pointer;
    42       display: inline-block;
    43       font-family: "微软雅黑",sans-serif;
    44       font-size: 1.25em;
    45       font-weight: bold;
    46       line-height: 1.5;
    47       margin: 0 -21.5em 5em;
    48       padding: .5em .3em .5em;
    49       position: relative;
    50       text-decoration: none;
    51       vertical-align: middle;
    52   }
    53   .button:hover {
    54     outline: none;
    55   }
    56   h1 {
    57     background-color: #333;
    58     color: #fff0f5;
    59     font-size: 3em;
    60     font-weight: bold;
    61     line-height: 1;
    62     margin:0;
    63     padding: 0.5em;
    64     text-align: center;
    65   }
    66   .attribution {
    67     color: #888;
    68     margin-top: 1em;
    69     text-align: right;
    70     width: 100%;
    71   }
  • 相关阅读:
    System.Reflection.ParameterModifier.cs
    System.Reflection.CallingConvention.cs
    System.Reflection.ParameterAttributes.cs
    System.Reflection.ParameterInfo.cs
    System.Reflection.MethodImplAttributes.cs
    System.Reflection.ExceptionHandlingClause.cs
    System.Reflection.LocalVariableInfo.cs
    交换分区设置
    PYCURL ERROR 22
    Could not reliably determine the server's fully qualified domain name
  • 原文地址:https://www.cnblogs.com/michaelle/p/4023320.html
Copyright © 2020-2023  润新知