【问题标题】:Powershell automated testing code freezes when pop up window appears in GUI.当 GUI 中出现弹出窗口时,Powershell 自动化测试代码冻结。
【发布时间】:2017-03-16 22:19:31
【问题描述】:

我正在使用 Jenkins 对 Web 应用程序执行一些自动化 Web 测试。

我正在使用powershell自动填写表格,返回值等。但有时会在测试中出现一个弹出窗口。天气它的错误消息或需要填写的子表单。

但是,当这种情况发生时,我的代码会无限期冻结,我需要退出构建或自己手动关闭弹出窗口(违背了自动化测试的目的)。

理想情况下,我希望能够选择打开的弹出窗口作为对象并填写表单/单击其上的按钮,但我的代码只是冻结并且没有给我机会。

有人对如何克服这个问题有任何建议吗?

【问题讨论】:

    标签: powershell jenkins automation automated-tests


    【解决方案1】:

    我不知道 Jenkins,但这里有一个 PowerShell 函数,它显示一个消息框,该消息框将在指定的超时间隔(以毫秒为单位)后消失。不会阻止此弹出窗口。

    function Show-Messagebox
    {
        param([String]$Title, [String]$Message, [Int]$TimeOut=2000)
    
        $TypeDef = @'
    
        using System;
        using System.Windows.Forms;
        using System.Runtime.InteropServices;
    
        public class Win32API
        {
            private const UInt32 WM_CLOSE = 0x0010;
    
            [DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
            private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
    
            [DllImport("user32.Dll")]
            private static extern int PostMessage(IntPtr hWnd, UInt32 msg, int wParam, int lParam);
    
            public static void ShowMessageBox(string Message, string Caption, int TimeOut = 2000)
            {
                var timer = new System.Timers.Timer(TimeOut) { AutoReset = false };
                timer.Elapsed += delegate
                {
                   IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, Caption);
                   if (hWnd.ToInt32() != 0) PostMessage(hWnd, WM_CLOSE, 0, 0);
               };
    
               timer.Enabled = true;
               MessageBox.Show(Message, Caption);
           }
    
       }
    '@
    
        Add-Type -TypeDefinition $TypeDef -ReferencedAssemblies System.Windows.Forms
    
        [Win32API]::ShowMessageBox($Message, $Title, $TimeOut)
    }
    

    函数可以这样调用:

    Show-Messagebox -Title "The Title" -Message "The complete message"
    

    在 C# 代码中将类的整个类型定义作为 here 字符串放入函数中有点笨拙(此处字符串的结束标记 '@ 之前不允许有空格)但它仍然很简单复制和粘贴解决方案。

    如果您将 PoweShell 脚本中的 [System.Windows.Forms.MessageBox]::Show() 调用替换为此函数,则不应再出现阻塞消息框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2023-04-01
      • 2023-04-03
      • 2010-09-06
      • 2014-05-26
      • 1970-01-01
      相关资源
      最近更新 更多