需要帮助识别并关闭随机出现的警告

时间:2016-08-17 21:32:29

标签: qtp hp-uft

我有一个警告弹出窗口随机显示在我的Windows应用程序上(此警告是必要的),需要相关硬件来解决与警告相关的问题。在关闭之前,这不会让应用程序工作。

无论如何,我的问题是当我运行另一个脚本,验证应用程序上的某些内容时,我需要一种方法来查找此对话框并在脚本运行时关闭它。有没有办法/方法/关键字来做到这一点?

就像我说的那样,这个警告是随机发生的,所以我不能把它的逻辑放在脚本的一个地方并期望它能够工作,我也不能把它放在每一行上。关于如何处理这个问题的任何想法? TIA

2 个答案:

答案 0 :(得分:3)

在UFT中探索“恢复方案”。 他们就是出于这个原因 - 异常处理。

答案 1 :(得分:0)

请参阅VBScript to detect an open messagebox and close it

另一种解决方案

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Linq;

namespace HandleDynamicDialog
{
    public class Program
    {
        [DllImport("user32.dll")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        private const int WM_SYSCOMMAND = 0x0112;

        private const int SC_CLOSE = 0xF060;

        static void Main(string[] args)
        {
            if (args.Length == 2)
            {
                //Pass your dialog class and title as command line arguments from uft script.
                string targetDialogClass = args[0];
                string targetDialogTitle = args[1];
                //Verify UFT is launched 
                bool uftVisible = IsUftVisible();
                while (uftVisible)
                {
                    //Close the warning dialog
                    CloseDialog(targetDialogClass, targetDialogTitle);
                }
            }
        }

        /// <summary>
        /// Close dialog using API
        /// </summary>
        /// <param name="dialogClass"></param>
        /// <param name="dialogTitle"></param>
        public static void CloseDialog(string dialogClass, string dialogTitle)
        {
            try
            {
                int winHandle = FindWindow(dialogClass, dialogTitle);
                if (winHandle > 0)
                {
                    SendMessage(winHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
                }
                Thread.Sleep(5000);
            }
            catch (Exception exception)
            {
                //Exception
            }
        }

        /// <summary>
        /// Verify UFT is visible or not
        /// </summary>
        /// <returns></returns>
        public static bool IsUftVisible()
        {
            return Process.GetProcesses().Any(p => p.MainWindowTitle.Contains("HP Unified Functional Testing"));
        }
    }
}

来自UFT

Call HandleDynamicDialog("CalcFrame","Calculator")
Public Sub HandleDynamicDialog(ByVal dialogClass,ByVal dialogTitle)
    Dim objShell
    Dim strCommandLineString
    Set objShell = CreateObject("Wscript.Shell")    
    strCommandLineString = "C:\HandleWarningDialog.exe " & dialogClass & " " & dialogTitle
    SystemUtil.CloseProcessByName "HandleWarningDialog.exe"
    objShell.Exec(strCommandLineString)
    Set objShell = Nothing  
End Sub