This is a migrated thread and some comments may be shown as answers.

Attach to existing just opened WPF window

4 Answers 358 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stanislav
Top achievements
Rank 1
Stanislav asked on 06 Jun 2012, 11:44 AM
Hi,

Is there any possibility to attach to just opened new WPF window in the main WPF application?

Manager manager = new Manager(false);
 
manager.Start();
             
WpfApplication wpfApplication = manager.LaunchNewApplication(@"Wpf.Application1.exe");
WpfWindow wpfWindow = wpfApplication.GetWindow("MainWindow");
 
wpfWindow.Find.ByName<Button>("button2").User.Click(); //The new WPF window is shown, how can I attach to it?

I would be really grateful if you could provide me with some C# examples.

Thank you in advance.

Stas.

4 Answers, 1 is accepted

Sort by
0
Anthony
Telerik team
answered on 07 Jun 2012, 03:48 PM
Hello Stanislav,

You can connect to any window in the WPF application by its title, provided it's unique (different from other window titles):

WpfApplication app = Manager.LaunchNewApplication(@"C:\WPFHelloWorld.exe");
ArtOfTest.WebAii.Wpf.WpfWindow newWin = app.GetWindow("TestStudio WPF Example");
 
TextBox tb = newWin.Find.ByName<TextBox>("txtName");
tb.SetText(true, "TELERIK", 10, 100, false);

If your window title has a dynamic portion, you can also use the tilde (~) character to indicate a partial match:

ArtOfTest.WebAii.Wpf.WpfWindow newWin = app.GetWindow("~WPF Example");

Kind regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Stanislav
Top achievements
Rank 1
answered on 06 Jul 2012, 10:50 PM
Hello Anthony,

Unfortunately, provided code doesn't work. I tried it several times but all in vain. I tried to modified my application and show two wpf windows simultaneously after starting the application. In this case, GetWindow works perfectly and in manager two windows are tracked. I can't understand the source of issue.

The code is just simple in test application. After clicking on the button, new window is displayed:
About about = new About();
about.show();

When I added this code to the MainWindow() constructor of the first window, everything works fine.

I look forward to hearing from you. Thank you in advance.
0
Stanislav
Top achievements
Rank 1
answered on 06 Jul 2012, 10:58 PM
I have just checked the same code but added Thread.Sleep(5000); after clicking on the button of the first form to wait for another window. And it works. The question is why there is no appropriate method to wait for another window?

Like:
WpfWindow about = app.GetWindow("About", 15); //Wait about 15 seconds the window is displayed or throw an exception

0
Accepted
Anthony
Telerik team
answered on 09 Jul 2012, 05:34 PM
Hello Stanislav,

You can use WaitForWindow instead to achieve that scenario:

WpfWindow newWin = app.WaitForWindow("TestStudio WPF Example", 15000);

Note that the int timeout is in milliseconds.


Kind regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Stanislav
Top achievements
Rank 1
Answers by
Anthony
Telerik team
Stanislav
Top achievements
Rank 1
Share this question
or