2016-08-17 79 views
1

我的窗口應用程序中出現隨機出現的警告(需要此警告),需要關聯的硬件來解決與警告相關的問題。這將不會讓應用程序工作,直到它關閉。需要幫助識別和關閉隨機出現的警告

無論如何,我的問題是當我運行另一個腳本,驗證應用程序中的某些內容時,我需要一種方法來查看此對話框並在腳本運行時關閉它。有沒有任何方法/方法/關鍵字來做到這一點?

就像我說過的,這個警告是隨機發生的,所以我不能把它的邏輯放在腳本的一個地方,並期望它能工作,也不能把它放在每一行上。有關如何處理這個問題的任何想法? TIA

回答

3

探索UFT中的「恢復場景」。 他們正是出於這個原因 - 異常處理。

+0

這個工作,謝謝。 – user6620511

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