QTP加载第三方DLL实现清除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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值