【问题标题】:WPF: HowTo test if application is minimized in AppiumWPF:如何测试应用程序是否在 Appium 中最小化
【发布时间】:2021-11-26 11:12:42
【问题描述】:

在我的项目中,我想使用 appium 和 WinAppDriver 测试我的应用程序的 ui(用 C# .net-core-5.0/WPF 编写)。

第一步是为我的应用实现无边框设计,因此我使用了 windowChrome 功能并构建了一个带有最小化、恢复和关闭按钮的自定义标题栏。 对于这个按钮,我现在想编写测试用例。

测试代码应该是直截了当的...... 使用提供的 appium 驱动程序单击最小化、最大化按钮并测试 Window 对象具有的 windowState 属性。 但我不知道如何提取 windowState 属性。

[TestMethod]
public void TestMinimizeWindow()
{
    session.FindElementByAccessibilityId("MinimizeButton").Click();
    //how to extract the information whether the app is minmized, maximized or normal
    var actualWindowState = ...
    Assert.AreEqual(WindowState.Minimized,actualWindowState);
}

谁知道如何用appium/WinAppDriver提取属性? 或者有人对此案有其他解决方案吗?

【问题讨论】:

  • 好的,到目前为止有效。但有时会发生 Placement 确定不正确...
  • 说实话,你为什么要测试这个?您是否有自定义最小化按钮或应该在最大化时触发某些操作?否则,这个测试似乎无关紧要
  • 一件事是最小化按钮是手动实现的(无框窗口),另一件事是,我正在编写的每个测试,无论测试一开始是否有用,都可以帮助我检查是否我的应用程序按预期工作。

标签: c# wpf appium


【解决方案1】:

正如Arkane 的帖子中所建议的,我创建了一个进程句柄,我可以从中确定状态

namespace codedui_test
{
    public enum ShowWindowCommands : int
    {
        Hide = 0,
        Normal = 1,
        Minimized = 2,
        Maximized = 3,
    }

    [Serializable]
    [StructLayout(LayoutKind.Sequential)]
    public struct WINDOWPLACEMENT
    {
        public int length;
        public int flags;
        public ShowWindowCommands showCmd;
        public System.Drawing.Point ptMinPosition;
        public System.Drawing.Point ptMaxPosition;
        public System.Drawing.Rectangle rcNormalPosition;
    }

    public class ProcessManager
    {
        protected Process process;

        public ProcessManager(string processToWatch)
        {
            
            //get Handle of window to extract it's position and styles
            Process[] processes = Process.GetProcesses();
            foreach (Process proc in processes)
            {
                if (proc.ProcessName == processToWatch)
                {
                    process = proc;
                    break;
                }
            }
        }

        public WINDOWPLACEMENT GetPlacement()
        {
            IntPtr hwnd = process.MainWindowHandle;
            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
            placement.length = Marshal.SizeOf(placement);
            GetWindowPlacement(hwnd, ref placement);
            return placement;
        }

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
    }
}

在我的 Testclass 中,我现在可以创建一个 processobject,我可以从中获取位置:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;

namespace codedui_test
{
    [TestClass]
    public class MainWindowTest
    {
        protected static ProcessManager dSProcess;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            //Setup Handle to Window to be able to determine it's size
            dSProcess = new ProcessManager("ProgramName");
        }

        [TestMethod]
        public void TestWindowControl()
        {
            session.FindElementByAccessibilityId("MaximizeButton").Click();
            var placement = dSProcess.GetPlacement();
            Assert.AreEqual("Maximized", placement.showCmd.ToString());
            session.FindElementByAccessibilityId("RestoreButton").Click();
            placement = dSProcess.GetPlacement();
            Assert.AreEqual("Normal", placement.showCmd.ToString());
            session.FindElementByAccessibilityId("MinimizeButton").Click();
            placement = dSProcess.GetPlacement();
            Assert.AreEqual("Minimized", placement.showCmd.ToString());
        }

        ....further methods

    }

}

此解决方案的草稿来自the source Arkane posted

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2015-12-20
  • 1970-01-01
相关资源
最近更新 更多