0

In my project I want to test the ui of my application (written in C# .net-core-5.0/WPF) with appium and WinAppDriver.

First step was to implement a borderless design to my app, so I used the windowChrome functionality and built a custom title bar with minimize, restore and close button. For this buttons I now want to write testcases.

The test code should be straight forward.... Click minimize, maximize buttons with provided appium driver and test the windowState Property a Window object has. But I don't know how to extract the windowState property.

[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);
}

Who knows how to extract the property with appium/WinAppDriver? Or does anyone have another solution to this case?

Daimonion
  • 94
  • 8
  • Does this work for you ? https://stackoverflow.com/questions/11065026/get-window-state-of-another-process – Arkane Oct 06 '21 at 09:39
  • Okay, this works so far. But sometimes it happens that the Placement is determined not correctly... – Daimonion Oct 11 '21 at 13:56
  • to be honest, why do you test this? Do you have custom minimize button or some action should be triggered on maximize? Otherwise, this test seems irrelevant – Krzysztof Skowronek Oct 12 '21 at 14:50
  • One thing is that the minimize button is manually implemented (frameless window) and the other thing is, that every test i'm writing no matter if the test seems useful at first, helps me to check if my application works as intended. – Daimonion Oct 14 '21 at 13:44

1 Answers1

0

As suggested in the post from Arkane I created a handle to the process from which i can determine the state

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);
    }
}

In my Testclass I am now able to create a processobject from which i can get the placement:

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

    }

}

Draft for this solution comes from the source Arkane posted

Daimonion
  • 94
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '21 at 11:56