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 。以下是具体代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Diagnostics;
- namespace Automation
- {
- public class BrowserManager
- {
- /*
- Temporary Internet Files (Internet临时文件)
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
- Cookies
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
- History (历史记录)
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
- Form. Data (表单数据)
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
- Passwords (密码)
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
- Delete All (全部删除)
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
- Delete All - "Also delete files and settings stored by add-ons"
- RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351
- */
- public void ClearIECookie()
- {
- Process process = new Process();
- process.StartInfo.FileName = "RunDll32.exe";
- process.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 2";
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- process.StartInfo.CreateNoWindow = true;
- process.Start();
- process.WaitForExit();
- }
- public void ClearIECache()
- {
- Process process = new Process();
- process.StartInfo.FileName = "RunDll32.exe";
- process.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 8";
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- process.StartInfo.CreateNoWindow = true;
- process.Start();
- process.WaitForExit();
- }
- }
- }
3, 将类通过编译, 并在项目工程文件夹 bin\debug目录下找到Automation.dll. 将这个文件复制到你想要存放的目录下。 例如c:\automation.dll
4, 打开QTP,实现调用:
- Function CleanIE_Cache_and_Cookie
- Dim BrowserManager
- set BrowserManager = Dotnetfactory.CreateInstance("Automation.BrowserManager","c:\Automation.dll")
- BrowserManager.ClearIECache()
- BrowserManager.ClearIECookie()
- Set BrowserManager = nothing
- End Function