Swicth windows in a WPF applications

1 Answer 54 Views
Menu Window
Hamza
Top achievements
Rank 1
Hamza asked on 10 May 2024, 11:37 AM
Hi , I'm trying to automate the testing of a WPF program by writing code. I can perform actions on the main window of the program, but I can't do it on a different window. I think the reason for this is that it doesn't recognize that window. However, I couldn't find out how to switch to a different window.Can you help me ?. Here's an example of the code I've written:
        [CodedStep(@"Type 'XXX' into ServerNameWatermarkTextbox")]
        public void ConfirmInstalledDate_CodedStep()
        {
            string formattedDate = DateTime.Now.ToString("yyyy-MM-dd");
            ActiveApplication.MainWindow.Find.ByName("serverNameWatermark").User.TypeText("XXX",2);
            ActiveApplication.MainWindow.Find.ByName("usernameWaterMark").User.TypeText("logadmin",2);         
            ActiveApplication.MainWindow.Find.ByName("usernameWaterMark").KeyPress(ArtOfTest.WebAii.Win32.KeyBoard.KeysFromString("Tab"),1,1,1);
            ActiveApplication.MainWindow.Find.ByName("pwdBox").User.TypeText("XXX",2);
            ActiveApplication.MainWindow.Find.ByName<Button>("btnSignIn").User.Click();
            System.Threading.Thread.Sleep(5000);
            ActiveApplication.MainWindow.Find.ByName("imgData").User.Click();
            ActiveApplication.MainWindow.Find.ByTextContent("Status").User.Click(MouseClickType.RightClick);
            ActiveApplication.MainWindow.Find.ByTextContent("Select columns").User.Click();
            ActiveApplication.GetWindow("Select columns");
            ActiveApplication.GetWindow("Select columns").Find.ByName("txtSearch").User.TypeText("Date",2); 
            System.Threading.Thread.Sleep(5000);
            ActiveApplication.GetWindow("Select columns").Find.ByName("txtSearch").User.KeyPress(System.Windows.Forms.Keys.Enter,1);
            ActiveApplication.MainWindow.Find.ByTextContent("^"+formattedDate).User.Click();
            System.Threading.Thread.Sleep(2000);
        }
    }

1 Answer, 1 is accepted

Sort by
0
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
answered on 10 May 2024, 07:47 PM

I think that the window you are trying to access needs to be created. Or you need to create an instance of that window in order to access it.

Window2 MyWindow = new Window2();

Something along those lines. Maybe do this on application start and see if you are able to access the second window.

Hamza
Top achievements
Rank 1
commented on 13 May 2024, 11:05 AM

Thank for your reply.
I might have misrepresented myself. To elaborate on the issue, if I need to delve into the details: When I attempt to access a specific window, let's say 'Select Columns,' within the Window of a WPF application, it's akin to a pop-up window that appears within the program. However, despite using the code snippet 'ActiveApplication.MainWindow.Find.ByName("XXX").User.TypeText("XXX",2);' to interact with this 'Select Columns' window, it seems to prioritize the Main Window, neglecting any actions intended for the 'Select Column' window
Tags
Menu Window
Asked by
Hamza
Top achievements
Rank 1
Answers by
Patrick
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or