jhc-systems / DelphiUIAutomation

Delphi classes the wrap the MS UIAutomation library
Apache License 2.0
108 stars 43 forks source link

TAutomationDesktop.GetDesktopWindow crashes because rootElement is nil #44

Closed swanitzek closed 7 years ago

swanitzek commented 8 years ago

I created a Delphi test-project (DUnit) and included the DelphiUIAutomation-library. As soon as I call TAutomationDesktop.GetDesktopWindow the test-application crashes because the UDelphiUIAutomation-library accesses the global variable rootElement which is nil.

My code:

unit TestMainFormU;

interface

uses
  TestFramework,
  DelphiUIAutomation.Automation,
  DelphiUIAutomation.Window ,
  DelphiUIAutomation.Client,
  DelphiUIAutomation.Desktop,
  UIAutoWrapper;

type
  TestMainForm = class(TTestCase)
  strict private
    FApplication: IAutomationApplication;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure Example;
  end;

implementation

{ TestIAutomationBase }

procedure TestMainForm.Example;
var
  MainWindow: IAutomationWindow;
begin
  MainWindow := TAutomationDesktop.GetDesktopWindow('DemoApp', 5000); // crash

  MainWindow.GetButton('OK').Click;
end;

procedure TestMainForm.SetUp;
begin
  inherited;

  FApplication := TAutomationApplication.LaunchOrAttach('..\Binary\DemoApp.exe', '');

  FApplication.WaitWhileBusy;
end;

procedure TestMainForm.TearDown;
begin
  inherited;

  FApplication.Kill;
end;

initialization

RegisterTest(TestMainForm.Suite);

end.

The line where is crashes (DelphiUIAutomation.Desktop:158):

rootElement.FindAll(TreeScope_Children, condition.getCondition, collection);

Any suggestions? Is there something I am doing wrong or is there a known issue? The call to TAutomationApplication.LaunchOrAttach() works just fine and starts the application.

mmarquee commented 7 years ago

Hi Stefan First, thanks for using the library. I have written a version of your tests above, and have duplicated the issue. Once I know what the issue is, I will push both the fix and the test project. Mark H

mmarquee commented 7 years ago

Hi, I think I have a fix, and it is probably a documentation / example issue. We recently refactored so that the library could be called from a DLL, as well as directly. In that refactoring we seem to have moved something that automatically happened to something that had to be manually called. If you making sure that the UIAuto instance has been created, either by adding TUIAuto.CreateUIAuto; in the setup mechanism, or in the Initialization of the unit (in this case the test), this loads the library, initializes COM and sets all of the underlying stuff in the library. Let me know if this fixes your issue. I've pushed a more complete set of examples, and will change the documentation to reflect the necessary change

  initialization
    TUIAuto.CreateUIAuto;   // Initialise the library
    RegisterTest(TestIAutomationBase.Suite);

Thanks Mark

swanitzek commented 7 years ago

Thank you. Adding a call to TUIAuto.CreateUIAuto resolved the issue for me!

markhumphreysjhc commented 7 years ago

Great, I've pushed the improved examples and documentation, and added a DUnit test application that is the equivalent of you test above. I'll close the issue, thanks for reporting it,