Attach to existing just opened WPF window

5 posts, 1 answers
  1. Stanislav
    Stanislav avatar
    1 posts
    Member since:
    May 2012

    Posted 06 Jun 2012 Link to this post

    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.
  2. Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 07 Jun 2012 Link to this post

    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
  3. Stanislav
    Stanislav avatar
    1 posts
    Member since:
    May 2012

    Posted 06 Jul 2012 Link to this post

    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.
  4. Stanislav
    Stanislav avatar
    1 posts
    Member since:
    May 2012

    Posted 06 Jul 2012 Link to this post

    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

  5. Answer
    Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 09 Jul 2012 Link to this post

    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
Back to Top