Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application.TryAttach can throw uncaught exceptions #184

Open
jv42 opened this issue Mar 19, 2024 · 0 comments · May be fixed by #185
Open

Application.TryAttach can throw uncaught exceptions #184

jv42 opened this issue Mar 19, 2024 · 0 comments · May be fixed by #185

Comments

@jv42
Copy link

jv42 commented Mar 19, 2024

I'm using the SetUp method in a very similar way to the example in README.md:

[SetUp]
public void SetUp()
{
    if (Application.TryAttach(ExeFileName, out var app))
    {
        using (app)
        {
            app.MainWindow.FindButton("Reset").Invoke();
        }
    }
}

Basically, I've only removed the argument that I'm not using.

Locally, this works just fine, but on a build machine, I got an exception in TryAttach()

System.InvalidOperationException : Process was not started by this object, so requested information cannot be determined.
          at System.Diagnostics.Process.get_StartInfo()
           at Gu.Wpf.UiAutomation.Application.<>c__DisplayClass33_0.<TryAttach>b__0(Process x)
           at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
           at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
           at Gu.Wpf.UiAutomation.Application.TryAttach(ProcessStartInfo processStartInfo, OnDispose onDispose, Application& result)
           at Gu.Wpf.UiAutomation.Application.TryAttach(String exeFileName, Application& result)
           at *MyTestApp.MyTestClass*.Setup() in *MyTestClass file* 
           at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
           at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

It matches this line

.FirstOrDefault(x => x.StartInfo.Arguments == processStartInfo.Arguments);

Accessing the StartInfo property is not allowed for all processes [depends on privileges] and should probably be wrapped in a try/catch in that case, if it fails, we can skip it because it's not a valid process to attach to anyway.

jv42 added a commit to jv42/Gu.Wpf.UiAutomation that referenced this issue Mar 26, 2024
Fixes GuOrg#184

Catch the exception in TryAttach so it behaves more like a "Try" method.

This code is only reached when the process we're looking for is not part of the Launched collection, so this change won't affect the main path of application re-use.
@jv42 jv42 linked a pull request Mar 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant