[Lazarus] automated testing code

duilio foschi octopushole at gmail.com
Sun Oct 27 19:41:29 CET 2024


I'd like to control an external application from my Lazarus code.
I wrote a test applet which controls notepad.
Following instructions found online, I added package
LazMouseAndKeyboardInput to my project and unit MouseAndKeyInput to my uses
clause.

This code - to my surprise - does not work: the notepad window is shown,
but no text is written into it:

procedure TForm1.btNotepadClick(Sender: TObject);
var
   app: HWND;
begin
   WinExec('notepad.exe', SW_NORMAL);
   app := FindWindow(nil,'senza nome - blocco note di windows');
   if app = 0 then
      exit;
   sleep(100);
   ShowWindow(app, SW_SHOW);
   KeyInput.Press('some text');
end;

I found an alternate code online but it works too much :)
I end with multiple (usually 3) windows of notepad open, where an invisible
hand has written 'some text' into each of them.

procedure SendKeys(Buffer:String);
var
  I: Integer;
  W: Word;
begin
  For I := 1 to Length(Buffer) do
  begin
    W := VkKeyScan(Buffer[i]);
    keybd_event(w,0,0,0);
    keybd_event(w,0,KEYEVENTF_KEYUP,0);
  end;
end;

procedure TForm1.btNotepadClick(Sender: TObject);
var
   app: HWND;
begin
   WinExec('notepad.exe', SW_NORMAL);
   app := FindWindow(nil,'senza nome - blocco note di windows');
   if app = 0 then
      exit;
   sleep(100);
   ShowWindow(app, SW_SHOW);
   sleep(100);
   SendKeys('some text');
end;

Has somebody a fix for my code?

Probably it is not important, but when I compile my applet, I get the
following warnings:

Messages, Hints: 12
Note: Duplicate unit "winmouseinput" in "p201827", orphaned ppu
"C:\lazarus_projects\automation\lib\i386-win32\winmouseinput.ppu"
Note: Duplicate unit "winmouseinput" in "LazMouseAndKeyInput 0.9",
ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\winmouseinput.ppu",
source="C:\lazarus\components\mouseandkeyinput\winmouseinput.pas"
Note: Duplicate unit "winkeyinput" in "p201827", orphaned ppu
"C:\lazarus_projects\automation\lib\i386-win32\winkeyinput.ppu"
Note: Duplicate unit "winkeyinput" in "LazMouseAndKeyInput 0.9",
ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\winkeyinput.ppu",
source="C:\lazarus\components\mouseandkeyinput\winkeyinput.pas"
Note: Duplicate unit "mouseinputintf" in "p201827", orphaned ppu
"C:\lazarus_projects\automation\lib\i386-win32\mouseinputintf.ppu"
Note: Duplicate unit "mouseinputintf" in "LazMouseAndKeyInput 0.9",
ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\mouseinputintf.ppu",
source="C:\lazarus\components\mouseandkeyinput\mouseinputintf.pas"
Note: Duplicate unit "mouseandkeyinput" in "p201827", orphaned ppu
"C:\lazarus_projects\automation\lib\i386-win32\mouseandkeyinput.ppu"
Note: Duplicate unit "mouseandkeyinput" in "LazMouseAndKeyInput 0.9",
ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\mouseandkeyinput.ppu",
source="C:\lazarus\components\mouseandkeyinput\mouseandkeyinput.pas"
Note: Duplicate unit "lazmouseandkeyinput" in "p201827", orphaned ppu
"C:\lazarus_projects\automation\lib\i386-win32\lazmouseandkeyinput.ppu"
Note: Duplicate unit "lazmouseandkeyinput" in "LazMouseAndKeyInput 0.9",
ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\lazmouseandkeyinput.ppu",
source="C:\lazarus\components\mouseandkeyinput\lazmouseandkeyinput.pas"
Note: Duplicate unit "keyinputintf" in "p201827", orphaned ppu
"C:\lazarus_projects\automation\lib\i386-win32\keyinputintf.ppu"
Note: Duplicate unit "keyinputintf" in "LazMouseAndKeyInput 0.9",
ppu="C:\lazarus\components\mouseandkeyinput\lib\i386-win32\win32\keyinputintf.ppu",
source="C:\lazarus\components\mouseandkeyinput\keyinputintf.pas"

Why?

Thank you

Peppe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20241027/59b29d95/attachment.htm>


More information about the lazarus mailing list