UFT中C#编写多API测试EXCEL

上一篇 / 下一篇  2014-07-22 08:49:55 / 精华(1) / 个人分类:测试脚本

namespace Script
{
    using System;
    using System.Xml;
    using System.Xml.Schema;
    using HP.ST.Ext.BasicActivities;
    using HP.ST.Fwk.RunTimeFWK;
    using HP.ST.Fwk.RunTimeFWK.ActivityFWK;
    using HP.ST.Fwk.RunTimeFWK.Utilities;
    using HP.ST.Fwk.RunTimeFWK.CompositeActivities;
    using HP.ST.Ext.CustomDataProviders.Extensions;
    using HP.ST.Ext.CustomDataProviders.ExcelFileArguments;
    using System.IO;
    using System.Reflection;
    using System.Data;
    using Microsoft.Office.Interop.Excel;
    using System.Text;
    using System.Collections.Generic;
   
   public class addto
   {
    public static double Sum(double x,double y)
        {
           
            double sum=0.0;
            sum=x+y;
            return sum;
        }//
加法函数
       public static double Sub(double x,double y)
        {
           double sub=0.0;
           sub=x-y;
           return sub;
        }//
减法函数 
          public static double Mult(double x,double y)
            {
               double mult=0.0;
               mult=x*y;
               return mult;
           }//
乘法函数
           public static double Div(double x,double y)
           {
               double div=0.0;
               div=x/y;
               return div;
           }//
除法函数
   }//
测试API函数所在类
   
     [Serializable()]
    public class TestUserCode : TestEntities
    {
//              public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet ws, int x, int y, object value)
//             {
//               ws.Cells[x, y] = value; 
//              }
//
设置单元格中内容方法
        
        public void CodeActivity4_OnExecuteEvent(object sender, STActivityBaseEventArgs args)
        {
            Microsoft.Office.Interop.Excel.Application xlsx=new ApplicationClass();//
创建Application对象
            Microsoft.Office.Interop.Excel.Workbook xbook=xlsx.Workbooks._Open("E:/TestCase.xlsx");//
打开已有excel文件
            Microsoft.Office.Interop.Excel.Worksheet xsheet=(Microsoft.Office.Interop.Excel.Worksheet)xbook.Sheets[1];//
制定要操作的sheet
             int iRows=xsheet.UsedRange.Cells.Rows.Count;
            int iColums=xsheet.UsedRange.Cells.Columns.Count;
             double p=0.0,m=0.0,s=0.0,d=0.0;//
实际测试结果初始化
             int NumPass=0,NumFail=0,NumUnav=0;//
统计计数初始化
            for(int i=2;i<iRows+1;i++)
            {
                string  method=((Range)xsheet.Cells[i,2]).Text.ToString();//
读取method的值
               if(method=="plus")
                {                
                   p=addto.Sum(Convert.ToDouble(((Range)xsheet.Cells[i,3]).Text.ToString()),Convert.ToDouble(((Range)xsheet.Cells[i,4]).Text.ToString()));//
测试用例数据输入结果
                if(p==Convert.ToDouble(((Range)xsheet.Cells[i,5]).Text.ToString()))
                    {
                        xlsx.Cells[i,6]=p;
                          xlsx.Cells[i,7]="pass";    
                          ++NumPass;               
                       //SetCellValue(xsheet, k, 6, p);  
                       //SetCellValue(xsheet, k, 7, "pass");    
                       //
调用单元格设置内容方法进行单元格内容写入
                    }//
实际结果与预期结果相同
                    else
                    {
                        xlsx.Cells[i,6]=p;
                          xlsx.Cells[i,7]="fail";
                          ++NumFail;
                    } //
实际结果与预期结果不同
                 }//
加法API测试
              else if(method=="minus")
                {
                  s=addto.Sub(Convert.ToDouble(((Range)xsheet.Cells[i,3]).Text.ToString()),Convert.ToDouble(((Range)xsheet.Cells[i,4]).Text.ToString()));
                    if(s==Convert.ToDouble(((Range)xsheet.Cells[i,5]).Text.ToString()))
                       {
                        xlsx.Cells[i,6]=s;
                          xlsx.Cells[i,7]="pass";    
                          ++NumPass;
                    }
                    else
                    {
                        xlsx.Cells[i,6]=s;
                          xlsx.Cells[i,7]="fail";
                          ++NumFail;
                    }
                }//
减法API测试
                else if(method=="multip")
                {
                    m=addto.Mult(Convert.ToDouble(((Range)xsheet.Cells[i,3]).Text.ToString()),Convert.ToDouble(((Range)xsheet.Cells[i,4]).Text.ToString()));
                    if(m==Convert.ToDouble(((Range)xsheet.Cells[i,5]).Text.ToString()))
                    {
                        xlsx.Cells[i,6]=m;
                          xlsx.Cells[i,7]="pass";    
                          ++NumPass;
                    }
                    else
                    {
                        xlsx.Cells[i,6]=m;
                          xlsx.Cells[i,7]="fail";
                          ++NumFail;
                    }
                }//
乘法API测试
               else if(method=="div")
                {
                   d=addto.Div(Convert.ToDouble(((Range)xsheet.Cells[i,3]).Text.ToString()),Convert.ToDouble(((Range)xsheet.Cells[i,4]).Text.ToString()));
                    if(d==Convert.ToDouble(((Range)xsheet.Cells[i,5]).Text.ToString()))
                    {
                        xlsx.Cells[i,6]=d;
                          xlsx.Cells[i,7]="pass";    
                          ++NumPass;
                    }
                    else
                    {
                        xlsx.Cells[i,6]=d;
                          xlsx.Cells[i,7]="fail";
                          ++NumFail;
                    }
                  }//
除法API测试
                else
                {
                     xlsx.Cells[i,7]="Method is unaviable";
                     ++NumUnav;
                 }
                //
method不为需测试的方法中,则在结果栏标注方法不可用
                if(((Range)xsheet.Cells[i,7]).Text.ToString()=="pass")
                {
                    ((Range)xsheet.Cells[i,7]).Interior.ColorIndex=4;
                }
                //
若测试结果为pass,标注该单元格为绿色
                else if(((Range)xsheet.Cells[i,7]).Text.ToString()=="fail")
                {
                    ((Range)xsheet.Cells[i,7]).Interior.ColorIndex=3;
                }
                //
若测试结果为fail,标注该单元格为红色
                else
                {
                    ((Range)xsheet.Cells[i,7]).Interior.ColorIndex=6;
                }
                //
若测试结果为其他,标注该单元格为黄色
            }//
循环执行测试用例
            xlsx.Cells[iRows+1,1]="Total Pass:";
            xlsx.Cells[iRows+1,2]=NumPass;
            xlsx.Cells[iRows+2,1]="Total Fass:";
            xlsx.Cells[iRows+2,2]=NumFail;
            xlsx.Cells[iRows+3,1]="Total Pass:";
            xlsx.Cells[iRows+3,2]=NumUnav;
          //
测试结果统计
            xbook.Save();
            xlsx.Save();
            xlsx.Workbooks.Close();
            xlsx.Quit();
            }
     }
}

在上一个脚本的基础上,花了两天时间完成的脚本,确切说是一天半,将TXT文档换成了EXCEL,总觉得用TXT写用例不太方便,将测试的一个API变成了多个,想想这个现象应该也是有可能的。写的过程中唯一的困惑是如何调用和分离出Excel中的数据,以及EXcel中的一些调用方法,边搜资料边尝试,总算是完成了。

附件传上测试用例的文件。


TAG:

liuxueyi的个人空间 引用 删除 liuxueyi   /   2015-08-07 21:58:55

  太牛了
不过我的报错了
Error        18        The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) (CS0234)        TestUserCode.cs        C:\Users\luke\Documents\Unified Functional Testing\APITest1        APITest1
liuxueyi的个人空间 引用 删除 liuxueyi   /   2015-08-07 21:58:09
5
 

评分:0

我来说两句

日历

« 2024-04-19  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 8090
  • 日志数: 6
  • 建立时间: 2014-07-21
  • 更新时间: 2014-07-25

RSS订阅

Open Toolbar