How can I test a function that contains a "waitFor" function or popup dialogs without requiring external user input?

4 vues (au cours des 30 derniers jours)
I am testing a GUI function in my code. Within my test class, I use Java functions to simulate mouse clicks, which trigger a popup dialog. However, the function execution pauses at the waitFor line, requiring me to manually click a button to close the dialog before the code can proceed. How can I automate this process entirely?
  1 commentaire
Walter Roberson
Walter Roberson le 18 Nov 2024
I think I once saw someone simulate pressing buttons through MATLAB Tester... but I cannot seem to find any way to do that at the moment.

Connectez-vous pour commenter.

Réponse acceptée

Jianfei
Jianfei le 23 Jan 2025
Modifié(e) : Jianfei le 31 Jan 2025
I found a perfect solution to this issue. I've made this answer the accepted one as it can be implemented across all MATLAB versions.
In the test function:
function testcase1(testCase)
t = timer;
t.StartDelay = 2;
t.TimerFcn = @pressESC;
start(t)
% Call your test function below that will trigger a dialogue. Code execution
% will be stuck inside that function if without external input.
end
You can control mouse and keyboard in the timer callback. The example is pressing ESC.
function pressESC(~, ~)
disp('Pressing ESC')
r = java.awt.Robot;
r.keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
pause(0.1);
r.keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
end
If you need to click a button, set a tag in your button.
function clickBtn(tagName)
% Get Java button object
hFig = findall(0, 'tag', tagName);
jBtn = findjobj(hBtn);
% Get central point of the button
p = jBtn.getLocationOnScreen;
x = p.x + jBtn.getWidth / 2;
y = p.y + jBtn.getHeight / 2;
% Mouse left click
r = java.awt.Robot;
r.mouseMove(x, y);
pause(0.1)
r.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)
pause(0.1)
r.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
end
If you have a sequence of dialogs, create another timer within the timer callback function to trigger another action. Use disp to confirm whether you have set the correct waiting time for timers and make sure all actions are in the correct order.

Plus de réponses (2)

Madheswaran
Madheswaran le 18 Nov 2024
Hi,
Starting from MATLAB R2024b, you can programmatically interact with alert and confirmation dialog boxes using 'chooseDialog' and 'dismissDialog' methods. They work with both modal and non-modal dialogs and can automatically select options or close dialogs without manual intervention.
For more information and examples, refer to the following documentations:
  1. dismissDialog - https://mathworks.com/help/matlab/ref/matlab.uitest.testcase.dismissdialog.html
  2. chooseDialog - https://mathworks.com/help/matlab/ref/matlab.uitest.testcase.choosedialog.html
Hope this helps!

埃博拉酱
埃博拉酱 le 18 Nov 2024
Possible ideas:
  1. Create a waitfor function for testing in the current directory before testing to see if it can mask the waitfor of MATLAB.
  2. Create an environment or a global variable with a special name (such as YourAppName_Debug) before the test, check whether the environment variable exists in the test objective function, and skip the dialog box if it exists.
  3. Add an undocumented optional parameter to the function. This parameter is only offered by the developer in the test environment.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by