Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.GetWindow() *very* slow

I have the following two methods that I call in sequence (with appropriate class level field in sequence)

public const string ProcessName = "This is"
public const string WindowTitle = "somewhat proprietary."

public Application App { get; set; }

public void Launch()
{
    var theProcess = Process.GetProcesses().First(p => p.ProcessName.Contains(ProcessName))
     App = Application.Attach(theProcess);
}

public void Select()
{ 
    var window = App.GetWindow(WindowTitle);

    var textBox = window.Get<TextBox>();
    //etc, do more stuff in the window
}

When I run it, the call to App.GetWindow() takes a REALLY long time. Like more than a minute. The application and window are both open and running.

I've tried experimenting with the overloads of GetWindow() and also tried calls to Application.GetWindows() and Application.Find(), but with the same result.

Does anyone have any thoughts as to how I could cut down on this time, or at least pinpoint what is taking so long? I'm not married to the implementation I have by any stretch - whatever gets me that window object is fine with me.

Update:

To address the comments so far, I modified the code to try to eliminate as many other concerns as possible.

public void Select()
{
    var processes = Process.GetProcesses().ToList();
    var process = processes.First(p => p.ProcessName.ToLower().Contains("notepad"));
    App = Application.Attach(process);
    var window = App.GetWindow("Untitled - Notepad");
}

I threw in the enumerable evaluation to eliminate any deferred execution as well. And, I tried it with both my app and notepad. The above code, for both my app and notepad, executes the first 3 lines immediately in the debugger, and then takes excessive time on the last one in both cases.

(It seems possible that White might internally defer executing Application.Attach, but I don't know very much about this tool, so that's very opaque to me.)

Second Update:

Here's the breakdown of the time spent in the GetWindow() method. The app spent around 10% of the time in GetWindow(), so more than half of that time is spent in WaitTillFound() and almost all of that in a Retry() method. Any thoughts on how to reduce that time (or to reduce the time spent in Window constructor after it is found)? Diagnostics

like image 387
Erik Dietrich Avatar asked Mar 17 '16 15:03

Erik Dietrich


1 Answers

It's not clear if target window is actually found after that waiting. If yes - that's very strange behaviour indeed. Anyhow, when you call GetWindow, White will try to find that window for a certain period, by default 30 seconds (with intervals of 200ms). So most likely that is what you experience - white for some reason cannot find window you target. You can configure this timeout via:

TestStack.White.Configuration.CoreAppXmlConfiguration.Instance.FindWindowTimeout = 0;

Where with 0 it will fail immediatly if window cannot be found. You can also use some other value lower than 30 seconds.

like image 98
Evk Avatar answered Oct 14 '22 10:10

Evk