QTP加载第三方DLL(C#)实现清除IE缓存(转)

由于QTP的默认编程语言是VBS, 而VBS是一种相对来说功能比较局限的脚本语言,因此我们在编写自动化测试脚本时会有很多功能无法很好的实现。 相对来说c#是一种高级编程语言, 可以实现大多数windows环境下的功能。 所以我们可以借助C#来实现在VBS下无法实现或者实现起来麻烦的功能。 

本篇文章以清除IE缓存为例, 介绍QTP如何与.Net framework集成。

1, 创建c# dll.

在Visual studio 中新建项目, 选择Class library. 命名为: Automation

2, 在项目中新建一个类, 命名为:BrowserManager , 在这个类中定义了2个方法分别实现清理IE cache和cookie 。以下是具体代码:

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using System.Diagnostics;   
  7.   
  8. namespace Automation  
  9. {  
  10.     public class BrowserManager  
  11.     {  
  12.          
  13.         /* 
  14.         Temporary Internet Files  (Internet临时文件) 
  15.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 
  16.  
  17.         Cookies 
  18.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 
  19.  
  20.         History (历史记录) 
  21.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1 
  22.  
  23.         Form. Data (表单数据) 
  24.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16 
  25.  
  26.         Passwords (密码) 
  27.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32 
  28.  
  29.         Delete All  (全部删除) 
  30.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 
  31.  
  32.         Delete All - "Also delete files and settings stored by add-ons" 
  33.         RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351 
  34.         */  
  35.         public void ClearIECookie()  
  36.         {  
  37.             Process process = new Process();  
  38.             process.StartInfo.FileName = "RunDll32.exe";  
  39.             process.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 2";  
  40.             process.StartInfo.UseShellExecute = false;  
  41.             process.StartInfo.RedirectStandardInput = true;  
  42.             process.StartInfo.RedirectStandardOutput = true;  
  43.             process.StartInfo.RedirectStandardError = true;  
  44.             process.StartInfo.CreateNoWindow = true;  
  45.             process.Start();  
  46.             process.WaitForExit();  
  47.         }  
  48.   
  49.         public void ClearIECache()  
  50.         {  
  51.             Process process = new Process();  
  52.             process.StartInfo.FileName = "RunDll32.exe";  
  53.             process.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 8";  
  54.             process.StartInfo.UseShellExecute = false;  
  55.             process.StartInfo.RedirectStandardInput = true;  
  56.             process.StartInfo.RedirectStandardOutput = true;  
  57.             process.StartInfo.RedirectStandardError = true;  
  58.             process.StartInfo.CreateNoWindow = true;  
  59.             process.Start();  
  60.             process.WaitForExit();  
  61.         }  
  62.     }  
  63. }  


3, 将类通过编译, 并在项目工程文件夹 bindebug目录下找到Automation.dll. 将这个文件复制到你想要存放的目录下。 例如c:automation.dll

4, 打开QTP,实现调用:

    1. Function CleanIE_Cache_and_Cookie     
    2.     Dim BrowserManager  
    3.     set BrowserManager = Dotnetfactory.CreateInstance("Automation.BrowserManager","c:Automation.dll")  
    4.     BrowserManager.ClearIECache()  
    5.     BrowserManager.ClearIECookie()  
    6.     Set BrowserManager = nothing  
    7. End Function  
原文地址:https://www.cnblogs.com/ellie-test/p/4517690.html