自动化测试之自动卸载软件

在平常的 测试 工作中,经常要安装软件,卸载软件,  即繁琐又累。  安装和卸载完全可以做成自动化。 安装软件我们可以通过自动化框架,自动点击Next,来自动安装。  卸载软件我们可以通过msiexec命令行工具自动化卸载软件 阅读目录   用msiexec 命令来卸载软件   注册表中查找ProductCode   C#中自动卸载软件   C#查找注册表中的ProductCode   完整源代码下载 用msiexec 命令来卸载软件   平常我们手动卸载软件都是到控制面板中的”添加/删除”程序中去卸载软件, 或者通过程序自带的卸载软件来卸载。   我们可以通过 MsiExec.exe /X{ProductCode} 命令来卸载程序。   关于MsiExec.exe 请看 http://technet.microsoft.com/zh-cn/library/cc759262%28v=WS.10%29.aspx 注册表中查找ProductCode   ProductCode是 Windows 安装程序包的全局唯一标识符 (GUID), 我们可以通过注册表来获取ProductCode   实例:  用MsiExec.exe 自动卸载Xmarks.   Xmarks 是一个用来同步收藏夹的工具, 我平常用来同步IE,firefox,chrome的收藏夹。   先用注册表打开如下位置,   32位 操作系统: HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall   注意: 如果是64位操作系统:   64位的程序还在: HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionUninstall   32位的程序而是在: HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall   Uninstall下面的注册表子键很多, 你需要耐心地一个一个去查找”DisplayName”, 从而找到程序的ProductCode, 如下图。

自动化测试之自动卸载软件   在 自动化测试中,我们不想弹出这个对话框,而是希望直接卸载。同时也不希望系统重启 只要加个两个参数 /quiet /norestart 就可以了   现在的卸载的命令是: MsiExec.exe /X{C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF} /quiet

C#中卸载程序 C#的卸载代码比较简单, 当然你也可以用其他语言。

Process p = new Process(); p.StartInfo.FileName = “msiexec.exe”; p.StartInfo.Arguments = “/x {C56BBAC8-0DD2-4CE4-86E0-F2BDEABDD0CF} /quiet /norestart”; p.Start();

C#查找注册表中的ProductCode 最麻烦的在于,如何到注册表中获取ProductCode。 如果做非Web程序的自动化测试,经常需要跟注册表打交道。 代码为:

public static string GetProductCode(string displayName) { string productCode = string.Empty; // 如果是32位操作系统,(或者系统是64位,程序也是64位) string bit32 = @”SOFTWAREMicrosoftWindowsCurrentVersionUninstall”; // 如果操作系统是64位并且程序是32位的 string bit64 = @”SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall”; RegistryKey localMachine = Registry.LocalMachine; RegistryKey Uninstall = localMachine.OpenSubKey(bit32, true); foreach (string subkey in Uninstall.GetSubKeyNames()) { RegistryKey productcode = Uninstall.OpenSubKey(subkey); try { string displayname = productcode.GetValue(“DisplayName”).ToString(); if (displayname == displayName) { string uninstallString = productcode.GetValue(“UninstallString”).ToString(); string[] strs = uninstallString.Split(new char[2] { ‘{‘, ‘}’ }); productCode = strs[1]; return productCode; } } catch { } } return productCode; }


最新内容请见作者的GitHub页:http://qaseven.github.io/

相关资源:截取软件够累的-其它工具类资源-CSDN文库

来源:weixin_34319999

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年6月2日
下一篇 2017年6月2日

相关推荐